【问题标题】:Test Data showing 100% accuracy测试数据显示 100% 准确度
【发布时间】:2020-01-17 17:31:52
【问题描述】:
from sklearn.utils import shuffle
df_concat = shuffle(df_concat)
df = df_concat

X = df.loc[:, df.columns != 'NEWACCT_NO']
X = X.loc[:, X.columns != 'CURRENT_MTH_CHURN']
X = X.values
y = df.CURRENT_MTH_CHURN.values # Target variable


from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.33, random_state = 1)

#Train the model with the help of DecisionTreeClassifie
clf = DecisionTreeClassifier(class_weight="balanced")
clf = clf.fit(X_train,y_train)

#At last we need to make prediction. It can be done with the help of following script −
y_pred = clf.predict(X_test)


#Next, we can get the accuracy score, confusion matrix and classification report as follows −
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
result = confusion_matrix(y_test, y_pred)
print("Confusion Matrix:")
print(result)
result1 = classification_report(y_test, y_pred)
print("Classification Report:",)
print (result1)
result2 = accuracy_score(y_test,y_pred)
print("Accuracy:",result2)

输出:

Confusion Matrix:
[[8238    0]
 [   0 1066]]
Classification Report:
              precision    recall  f1-score   support

           0       1.00      1.00      1.00      8238
           1       1.00      1.00      1.00      1066

    accuracy                           1.00      9304
   macro avg       1.00      1.00      1.00      9304
weighted avg       1.00      1.00      1.00      9304

Accuracy: 1.0

尽管 train_test_split 随机划分训练和测试数据,除此之外,我还使用了 sklearn.utils shuffle,但我仍然获得了 100% 的测试数据准确率。

无法识别错误。

另外,尝试去掉class_weight="balanced" 参数,但结果是一样的。

请专家建议。

【问题讨论】:

  • 你有完美的结果,你在抱怨什么?
  • 您是否尝试过使用不同的 random_state 来查看您是否始终获得 100% 的准确率?否则,100% 准确率的常见解释是,您使用了与您的目标直接相关且您不应该使用的功能
  • 在大多数真实场景中,保持集的 100% 准确率并不意味着结果是完美的,这意味着存在错误。我认为这是一个明智的(尽管不是 mcve)问题

标签: python pandas scikit-learn


【解决方案1】:

您将数据拆分为训练和测试,但显然您的所有特征生成都已在此代码的上游完成。因此,如果您的任何特征生成代码涉及以任何方式使用您的因变量(例如,mean_churn_per_account_type 之类的变量),这意味着您的训练集特征包含来自测试集因变量的信息。这在 ML 中被称为“数据泄露”——您通过在训练测试拆分之前创建的功能将测试集数据泄露到训练集中。

要解决此问题,您需要将训练测试拆分移到任何涉及因变量的特征生成步骤的上游。将特征生成步骤以相同方式应用于训练集和测试集可能会导致错误 - 但使用 sklearn pipeline 会有所帮助。

免责声明:这都是猜测,因为我们实际上无法看到您的特征生成代码。但根据我的经验,这是最有可能的来源。

【讨论】:

    【解决方案2】:

    啊,按照建议,我检查了相关矩阵,发现有一个名为“IMAGE”的变量与 CHURN 变量的相关性为 -0.86。

    一旦我删除它,就能达到令人满意的效果:

    print(classification_report(test_output_smote, test_prediction_upsampled_smote))
                  precision    recall  f1-score   support
    
               N       0.74      0.84      0.79      8233
               Y       0.81      0.71      0.76      8233
    
        accuracy                           0.78     16466
       macro avg       0.78      0.78      0.77     16466
    weighted avg       0.78      0.78      0.77     16466
    

    感谢大家的帮助和支持。

    【讨论】:

      猜你喜欢
      • 2018-10-08
      • 1970-01-01
      • 2020-04-02
      • 1970-01-01
      • 1970-01-01
      • 2021-04-23
      • 2019-07-30
      • 2020-02-09
      • 2023-02-26
      相关资源
      最近更新 更多