對于分類任務,我嘗試比較兩個模型(使用 SVM),其中模型之間的差異只是一個特征(即cult_distance 和 inst_dist)。即使在 HP 調整(使用 GridSearchCV)之后,兩個模型也顯示出完全相同的精度。當我嘗試在模型中同時包含cult_distance 和 inst_dist的第三個模型時,精度仍然完全相同。我的代碼在下面,在螢屏截圖中您可以看到分類報告。正如您在報告中看到的,沒有預測少數類。我認為我的模型過度擬合,但我不知道如何解決這個問題。注意:同樣,當我在沒有cult_distance 和 inst_dist之外的任何其他功能的情況下運行模型時,準確性仍然相同。
features1 = ['inst_dist', 'cult_distance', 'deal_numb', 'acq_sic', 'acq_stat', 'acq_form',
'tar_sic', 'tar_stat', 'tar_form', 'vendor_name', 'pay_method',
'advisor', 'stake', 'deal_value', 'days', 'sic_sim']
X = data.loc[:, features1]
y = data.loc[:, ['deal_status']]
### training the model
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1, train_size = .75)
# defining parameter range
rfc = svm.SVC(random_state = 0)
param_grid = {'C': [0.001],
'gamma': ['auto'],
'kernel': ['rbf'],
'degree':[1]}
grid = GridSearchCV(rfc, param_grid = param_grid, refit = True, verbose = 2,n_jobs=-1)
# fitting the model for grid search
grid.fit(X_train, y_train)
# print best parameter after tuning
grid_predictions = grid.predict(X_test)
# print classification report
print(classification_report(y_test, grid_predictions))

編輯:在有用的評論之后,我使用 class_weight 插入資料集中。然而,資料的準確性遠低于 0.76 的基線?

uj5u.com熱心網友回復:
這個問題似乎與不平衡的資料集有關。一個類在你的資料集中表現得更多,所以模型只學習預測那個類。有很多方法可以解決這個問題,最簡單的一種是調整class_weight模型中的引數。在您的情況下,它應該在附近class_weight = {0 : 3.2, 1 : 1},因為二等艙的受歡迎程度是后者的 3 倍。您可以使用compute_class_weightsklearn 進行計算。
希望這有幫助!
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/520802.html
上一篇:TypeError:只有整數、切片(`:`)、省略號(`...`)、tf.newaxis(`None`)和標量tf.int32/tf.int64張量是有效的索引
