決策樹遇到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 <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:\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將其他數學模型和決策樹模型分開以規避這種錯誤

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM