我使用sklearn中的决策树分类器,但得到的分数是100%,我不知道哪里出了问题。我已经测试了SVM和KNN,它们的准确率在60%到80%之间,看起来还可以。以下是我的代码:
from sklearn.tree import DecisionTreeClassifier maxScore = 0 index = 0 Depths = [1, 5, 10, 20, 40] for i,d in enumerate(Depths): clf1 = DecisionTreeClassifier(max_depth=d) score = cross_val_score(clf1, X_train, Y_train, cv=10).mean() index = i if(score > maxScore) else index maxScore = max(score, maxScore) print('The cross val score for Decision Tree classifier (max_depth=' + str(d) + ') is ' + str(score)) d = Depths[index] print() print("So the best value for max_depth parameter is " + str(d)) print() # Classifying clf1 = DecisionTreeClassifier(max_depth=d) clf1.fit(X_train, Y_train) preds = clf1.predict(X_valid) print(" The accuracy obtained using Decision tree classifier is {0:.8f}%".format(100* (clf1.score(X_valid, Y_valid))))
这是输出结果:The cross val score for Decision Tree classifier (max_depth=1) is 1.0
The cross value score for Decision Tree classifier (max_depth=5) is 0.9996212121212121
The cross val score for Decision Tree classifier (max_depth=10) is 1.0
The cross val score for Decision Tree classifier (max_depth=20) is 1.0
The cross val score for Decision Tree classifier (max_depth=40) is 0.9996212121212121
So the best value for the max_depth parameter is 1
The accuracy obtained using Decision tree classifier is 100.00000000%
回答:
我认为有一个明显的结论:你的标签与某些特征有很高的相关性,或者至少与其中一个特征有关。也许你的数据质量不高。
无论如何,你可以检查决策树模型的单个特征分割对模型预测的影响。
使用model.feature_importances_
属性来查看某个特征对模型预测的重要性。
查看文档决策树分类器。
如果您仍然认为您的模型预测不够好,我建议您更换模型,使用不同方法的模型。至少如果您必须使用决策树,您可以尝试随机森林分类器。
它是一种集成模型。集成学习的基本思想是最终模型预测基于多个较弱模型的预测,弱学习器。查看创建集成模型的主要方法。
在随机森林分类器的情况下,弱学习器模型是深度较小的决策树。决策树使用少数特征进行预测,并且每次随机选择特征。选择的特征数量是一个超参数,需要进行调整。
查看链接和其他教程以获取更多信息。