决策树遇到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:: 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:: 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:: 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:: DataConversionWarning: Data with input dtype int64 was converted to float64 by MinMaxScaler.
warnings.warn(msg, DataConversionWarning) 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 , in <module>
KNN F1 0.8593012275731823
main()
File "E:/python_project/ImoocDataAnalysisMiningModeling/第6章 挖掘建模/6-4~6-5 分类-朴素贝叶斯~分类-决策树.py", line , in main
hr_modeling(features, labels)
File "E:/python_project/ImoocDataAnalysisMiningModeling/第6章 挖掘建模/6-4~6-5 分类-朴素贝叶斯~分类-决策树.py", line , in hr_modeling
filled=True, rounded=True, special_characters=True)
File "C:\Python36\lib\site-packages\sklearn\tree\export.py", line , in export_graphviz
check_is_fitted(decision_tree, 'tree_')
File "C:\Python36\lib\site-packages\sklearn\utils\validation.py", line , 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
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([[, ], [, ], [, ]])
... 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=)))
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将其他数学模型和决策树模型分开以规避这种错误
决策树遇到sklearn.exceptions.NotFittedError: XXX instance is not fitted yet. Call 'fit' with appropriate arguments before using this method.的解决方案的更多相关文章
- 决策树在sklearn中的实现
1 概述 1.1 决策树是如何工作的 1.2 构建决策树 1.2.1 ID3算法构建决策树 1.2.2 简单实例 1.2.3 ID3的局限性 1.3 C4.5算法 & CART算法 1.3.1 ...
- CountVectorizer()类解析
主要可以参考下面几个链接: 1.sklearn文本特征提取 2.使用scikit-learn tfidf计算词语权重 3.sklearn官方中文文档 4.sklearn.feature_extra ...
- Python sklearn拆分训练集、测试集及预测导出评分 决策树
机器学习入门 (注:无基础可快速入门,想提高准确率还得多下功夫,文中各名词不做过多解释) Python语言.pandas包.sklearn包 建议在Jupyter环境操作 操作步骤 1.panda ...
- Sklearn库例子——决策树分类
Sklearn上关于决策树算法使用的介绍:http://scikit-learn.org/stable/modules/tree.html 1.关于决策树:决策树是一个非参数的监督式学习方法,主要用于 ...
- 机器学习实战 | SKLearn最全应用指南
作者:韩信子@ShowMeAI 教程地址:http://www.showmeai.tech/tutorials/41 本文地址:http://www.showmeai.tech/article-det ...
- SK-Learn 全家福
SK-Learn API 全家福 最近SK-Learn用的比较多, 以后也会经常用,将Sk-Learn 所有内容整理了一下,整理思路,并可以备查. (高清图片可以用鼠标右键在单独窗口打开,或者保存到本 ...
- 决策树 Decision Tree
决策树是一个类似于流程图的树结构:其中,每个内部结点表示在一个属性上的测试,每个分支代表一个属性输出,而每个树叶结点代表类或类分布.树的最顶层是根结点.  决策树的构建 想要构建一个决策树,那么咱们 ...
- Python简单实现决策树
__author__ = '糖衣豆豆' #决策树 import pandas as pda fname="~/coding/python/data/lesson.csv" data ...
- Checked Exceptions
记得当年在程序员杂志上看出这次访谈,10多年过去了, 这件事儿最近被重提了, 原因是 Kotlin. 1.对Checked Exceptions特性持保留态度 (译者注:在写一段程序时,如果没有用tr ...
随机推荐
- UTF8和UCS2
谈谈Unicode编码,简要解释UCS.UTF.BMP.BOM等名词 这是一篇程序员写给程序员的趣味读物.所谓趣味是指可以比较轻松地了解一些原来不清楚的概念,增进知识,类似于打RPG游戏的升级.整理这 ...
- 接口和抽象类的使用场景以及多类继承存在的问题(c#)
我们首先来看下抽象class能发挥优势的使用场景. 假设有一个Cars基类,具体型号的Car继承该基类,并实现自己独有的属性或方法. public class Cars { public string ...
- Neutron 是怎么实现虚拟三层网络的
Neutron 对虚拟三层网络的实现是通过其 L3 Agent (neutron-l3-agent).该 Agent 利用 Linux IP 栈.route 和 iptables 来实现内网内不同网络 ...
- python网络编程--socket,网络协议,TCP
一. 客户端/服务端架构(用到网络通信的地方) 我们使用qq.微信和别人聊天,通过浏览器来浏览页面.看京东的网站,通过优酷.快播(此处只是怀念一下)看片片啥的等等,通过无线打印机来打印一个word文档 ...
- MyBatis入门程序(基于XML配置)
创建一个简单的MyBatis入门程序,实现对学生信息的增删改查功能(基于XML配置) 一.新建一个Java工程,导入MyBatis核心jar包.日志相关的jar包以及连接Oracle数据库所需驱动包, ...
- OCP考试题库更新,052最新考题及答案整理-第8题
8.Which two are true about the Fast Recovery Area (FRA)? A) It should be larger than the database. B ...
- P4842 城市旅行
题目链接 题意分析 首先存在树上的删边连边操作 所以我们使用\(LCT\)维护 然后考虑怎么维护答案 可以发现 对于一条链 我们编号为\(1,2,3,...,n\) 那么期望就是 \[\frac{a_ ...
- 3.2 Multi-Master Replication
摘要: 出处:黑洞中的奇点 的博客 http://www.cnblogs.com/kelvin19840813/ 您的支持是对博主最大的鼓励,感谢您的认真阅读.本文版权归作者所有,欢迎转载,但请保留该 ...
- 【C#】自定义新建一个DataTable(3列),循环3维矩形数组往其填充数据
从中可以了解DataTable的新增行和列;矩形多维数组循环机制;新建了DataTable DataTable dt = new DataTable(); DataColumn dc1 = new D ...
- IDEA + SpringBoot + Java搭建Web项目
打开IDEA,选择Spring Initializr,默认配置,点击Next  添写GAV.(group.Artifact.Version)  选择Spring Boot版本,这里选2.1.4稳定 ...