【发布时间】: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