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

决策树遇到sklearn.exceptions.NotFittedError: XXX instance is not fitted yet. Call 'fit' with appropriate arguments before using this method.的解决方案

1.异常信息:

C:\Python36\python36.exe "E:/python_project/ImoocDataAnalysisMiningModeling/第6章 挖掘建模/6-4~6-5 分类-朴素贝叶斯~分类-决策树.py"
C:\Python36\lib\site-packages\sklearn\utils\validation.py:595: DataConversionWarning: Data with input dtype int64 was converted to float64 by MinMaxScaler.
  warnings.warn(msg, DataConversionWarning)
C:\Python36\lib\site-packages\sklearn\utils\validation.py:595: DataConversionWarning: Data with input dtype int64 was converted to float64 by MinMaxScaler.
  warnings.warn(msg, DataConversionWarning)
C:\Python36\lib\site-packages\sklearn\utils\validation.py:595: DataConversionWarning: Data with input dtype int64 was converted to float64 by MinMaxScaler.
  warnings.warn(msg, DataConversionWarning)
C:\Python36\lib\site-packages\sklearn\utils\validation.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 
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:\Python36\lib\site-packages\sklearn\tree\export.py", line 396, in export_graphviz
    check_is_fitted(decision_tree, 'tree_')
  File "C:\Python36\lib\site-packages\sklearn\utils\validation.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:\Python36\lib\site-packages\sklearn\tree\export.py"这个文件,会看到

check_is_fitted(decision_tree, 'tree_')

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

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

2.4 解决方案:

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

 

posted @ 2018-12-19 10:23 Rest探路者 阅读( ...) 评论( ...) 编辑 收藏

你可能感兴趣的:(决策树遇到sklearn.exceptions.NotFittedError: XXX instance is not fitted yet. Call 'fit' with appropriate ...)