0

我正在尝试使用 Kfold 验证我的数据。

def printing_kfold_score(X,y):
fold = KFold(5,shuffle=False)
recall_accs=[]

for train_index, test_index in fold.split(X):
    X_train, X_test = X.iloc[train_index,:], X.iloc[test_index,:]
    y_train, y_test = y.iloc[train_index,:], y.iloc[test_index,:]

    # Call the logistic regression model with a certain C parameter
    lr = LogisticRegression(C = 0.01, penalty = 'l1',solver = 'liblinear')
    # Use the training data to fit the model. In this case, we use the portion of the fold to train the model
    lr.fit(X_train, y_train.values.ravel())

    # Predict values using the test indices in the training data
    y_pred_undersample = lr.predict(X_test)

    # Calculate the recall score and append it to a list for recall scores representing the current c_parameter
    recall_acc = recall_score(y_test,y_pred_undersample)
    recall_accs.append(recall_acc)
print(np.mean(recall_accs))

printing_kfold_score(X_undersample,y_undersample)

X_undersample 是一个数据框 (984,29)

y_undersample 是一个数据框 (984,1)

我收到以下警告:

0.5349321454470113
C:\Users\sudha\Anaconda3\lib\site-packages\sklearn\metrics\_classification.py:1272: UndefinedMetricWarning: Recall is ill-defined and being set to 0.0 due to no true samples. Use `zero_division` parameter to control this behavior.
  _warn_prf(average, modifier, msg_start, len(result))
C:\Users\sudha\Anaconda3\lib\site-packages\sklearn\metrics\_classification.py:1272: UndefinedMetricWarning: Recall is ill-defined and being set to 0.0 due to no true samples. Use `zero_division` parameter to control this behavior.
  _warn_prf(average, modifier, msg_start, len(result))

为什么我会收到这个警告,我的数据完全平衡(50/50)这个警告和低召回分数是意料之中的。你能告诉我我做错了什么吗?

我尝试打印 x_test 和 y_test 的值形状和值。

   x_train shape (788, 29) 
   x_test shape (196, 29) 
   y_train shape (788, 1) 
   y_test shape (196, 1) 

 x_test      V1        V2        V3  ...       V27       V28     normAmount
    541  -2.312227  1.951992 -1.609851  ...  0.261145 -0.143276   -0.353229
    623  -3.043541 -3.157307  1.088463  ... -0.252773  0.035764    1.761758
    4920 -2.303350  1.759247 -0.359745  ...  0.039566 -0.153029    0.606031

y_test         Class
38042       0
170554      0
16019       0

是因为第一列代表索引吗?

谢谢。

4

1 回答 1

1

您在评论中描述了这个问题:

y_test变化——有时全为 0,有时全为 1,等等。

这实际上是正在发生的事情:

>>> from sklearn.metrics import *
>>> recall_score([0,0], [1,0])

UndefinedMetricWarning:召回是不明确的,由于没有真正的样本而被设置为 0.0。使用zero_division参数来控制此行为。_warn_prf(平均值,修饰符,msg_start,len(结果))

您应该采取措施确保y_test始终有可用的正样本和负样本,以便更准确地评估分类器的性能。

于 2020-04-19T19:11:54.423 回答