使用决策树分类器实现100%准确率的sklearn方法

我使用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_属性来查看某个特征对模型预测的重要性。

查看文档决策树分类器

如果您仍然认为您的模型预测不够好,我建议您更换模型,使用不同方法的模型。至少如果您必须使用决策树,您可以尝试随机森林分类器

它是一种集成模型。集成学习的基本思想是最终模型预测基于多个较弱模型的预测,弱学习器。查看创建集成模型的主要方法。

在随机森林分类器的情况下,弱学习器模型是深度较小的决策树。决策树使用少数特征进行预测,并且每次随机选择特征。选择的特征数量是一个超参数,需要进行调整。

查看链接和其他教程以获取更多信息。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注