决策树遇到sklearn.exceptions.NotFittedError: XXX instance is not fitted yet. Call 'fit' with appr

Posted 多读书,少说话

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了决策树遇到sklearn.exceptions.NotFittedError: XXX instance is not fitted yet. Call 'fit' with appr相关的知识,希望对你有一定的参考价值。

1.异常信息:

C:Python36python36.exe "E:/python_project/ImoocDataAnalysisMiningModeling/第6章 挖掘建模/6-4~6-5 分类-朴素贝叶斯~分类-决策树.py"
C:Python36libsite-packagessklearnutilsvalidation.py:595: DataConversionWarning: Data with input dtype int64 was converted to float64 by MinMaxScaler.
  warnings.warn(msg, DataConversionWarning)
C:Python36libsite-packagessklearnutilsvalidation.py:595: DataConversionWarning: Data with input dtype int64 was converted to float64 by MinMaxScaler.
  warnings.warn(msg, DataConversionWarning)
C:Python36libsite-packagessklearnutilsvalidation.py:595: DataConversionWarning: Data with input dtype int64 was converted to float64 by MinMaxScaler.
  warnings.warn(msg, DataConversionWarning)
C:Python36libsite-packagessklearnutilsvalidation.py:595: DataConversionWarning: Data with input dtype int64 was converted to float64 by MinMaxScaler.
  warnings.warn(msg, DataConversionWarning)
8999 3000 3000
0
Traceback (most recent call last):
KNN ACC: 0.9337704189354372
KNN REC: 0.8670795616960457
  File "E:/python_project/ImoocDataAnalysisMiningModeling/第6章 挖掘建模/6-4~6-5 分类-朴素贝叶斯~分类-决策树.py", line 130, in <module>
KNN F1 0.8593012275731823
    main()
  File "E:/python_project/ImoocDataAnalysisMiningModeling/第6章 挖掘建模/6-4~6-5 分类-朴素贝叶斯~分类-决策树.py", line 124, in main
    hr_modeling(features, labels)
  File "E:/python_project/ImoocDataAnalysisMiningModeling/第6章 挖掘建模/6-4~6-5 分类-朴素贝叶斯~分类-决策树.py", line 116, in hr_modeling
    filled=True, rounded=True, special_characters=True)
  File "C:Python36libsite-packagessklearn	reeexport.py", line 396, in export_graphviz
    check_is_fitted(decision_tree, tree_)
  File "C:Python36libsite-packagessklearnutilsvalidation.py", line 951, in check_is_fitted
    raise NotFittedError(msg % {name: type(estimator).__name__})
sklearn.exceptions.NotFittedError: This KNeighborsClassifier instance is not fitted yet. Call fit with appropriate arguments before using this method.

Process finished with exit code 1

 

2.错误成因:

2.1 表象原因

Exception class to raise if estimator is used before fitting.

This class inherits from both ValueError and AttributeError to help with exception handling and backward compatibility.

大意是在fitting之前使用了estimator

>>> from sklearn.svm import LinearSVC
>>> from sklearn.exceptions import NotFittedError
>>> try:
...     LinearSVC().predict([[1, 2], [2, 3], [3, 4]])
... except NotFittedError as e:
...     print(repr(e))
...                        
NotFittedError(This LinearSVC instance is not fitted yet...)

2.2 解决方案:

先调用fit方法再进行预测

clf = clf.fit(X_train, Y_train)
Y_pred = clf.predict(DecisionTreeClassifier())

2.3 根本原因

我在决策树碰到NotFittedError,是因为用到了list,存在多个数学模型,我的代码如下

models = []
    models.append(("KNN", KNeighborsClassifier(n_neighbors=3)))
    models.append(("GaussianNB", GaussianNB()))
    models.append(("BernoulliNB", BernoulliNB()))
    # 使用决策树要注释掉前者,否则报NotFittedError
    models.append(("DecisionTree", DecisionTreeClassifier()))
    models.append(("DecisionTreeEntropy", DecisionTreeClassifier(criterion="entropy")))

为什么会报NotFittedError?点击打开"C:Python36libsite-packagessklearn reeexport.py"这个文件,会看到

check_is_fitted(decision_tree, tree_)

我们可以知道,不是决策树模型就会返回False,因为第一个模型是KNN(K最近邻分类),不是决策树,所以返回False,返回True需要DecisionTreeClassifier()

这里可以看到,和NotFittedError并无太大关系

2.4 解决方案:

把models前面的模型注释掉,或者重新写一个models将其他数学模型和决策树模型分开以规避这种错误

 

以上是关于决策树遇到sklearn.exceptions.NotFittedError: XXX instance is not fitted yet. Call 'fit' with appr的主要内容,如果未能解决你的问题,请参考以下文章

Python 决策树分类器

决策树遇到sklearn.exceptions.NotFittedError: XXX instance is not fitted yet. Call 'fit' with appr

决策树回归背后的数学?

python中的鸢尾花决策树

决策树完整性和未分类数据

导出/绘制随机森林决策树/“RandomForestClassifier”对象没有属性“tree_”