决策树遇到sklearn.exceptions.NotFittedError: XXX instance is not fitted yet. Call 'fit' with appr
博观而约取,厚积而薄发。这篇文章主要讲述决策树遇到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】
推荐阅读
- SpringMVC 常用applicationContext.xmlweb.xmlservlet-mvc.xml简单配置
- Android行驶证识别
- 标签@RequestMapping和@GetMapping@PostMapping的区别
- 广州易利购APP开发
- 安卓布局
- AndroidStudio项目制作SDK和打包成jar
- append 注意事项
- ipad安卓协议源码最新
- ipad安卓协议最新6.7.4