一个空块总是出现在我的决策树上,由python创建

发布于 2025-01-21 09:37:08 字数 1129 浏览 2 评论 0 原文

imgur

就像图片中的情况一样,我的决策树总是有一个空的块。 我已经搜索了一段时间,但仍然找不到解决方案。 我的代码在下面列出,在jupyter笔记本中运行。 希望您的帮助。

from sklearn import tree 
from sklearn import datasets 
import pydotplus 

wine = datasets.load_wine() 
X = wine.data
Y = wine.target

from sklearn.model_selection import train_test_split
X_train,X_test,Y_train,Y_test = train_test_split(X,Y,test_size = 0.3)

clf = tree.DecisionTreeClassifier(criterion = 'gini').fit(X_train,Y_train)
clf.score(X_train,Y_train)

clf.predict(X_test)

feature_names = wine.feature_names
target_name = wine.target_names

import graphviz

dot_data = tree.export_graphviz(clf,
                                out_file = None,
                                feature_names = feature_na,
                                class_names = target_name,
                                filled = None,
                                rounded = True,)
dot_data = dot_data.replace('helvetica', 'Microsoft JhengHei')
graph = pydotplus.graph_from_dot_data(dot_data)
graph.write_pdf('wine.pdf')

Imgur

Just like the situation in the picture, my decision tree always has an empty block.
I have already searched for a while, but still can't find the solution.
My codes are listed below, running in jupyter notebook.
Hoping for your help.

from sklearn import tree 
from sklearn import datasets 
import pydotplus 

wine = datasets.load_wine() 
X = wine.data
Y = wine.target

from sklearn.model_selection import train_test_split
X_train,X_test,Y_train,Y_test = train_test_split(X,Y,test_size = 0.3)

clf = tree.DecisionTreeClassifier(criterion = 'gini').fit(X_train,Y_train)
clf.score(X_train,Y_train)

clf.predict(X_test)

feature_names = wine.feature_names
target_name = wine.target_names

import graphviz

dot_data = tree.export_graphviz(clf,
                                out_file = None,
                                feature_names = feature_na,
                                class_names = target_name,
                                filled = None,
                                rounded = True,)
dot_data = dot_data.replace('helvetica', 'Microsoft JhengHei')
graph = pydotplus.graph_from_dot_data(dot_data)
graph.write_pdf('wine.pdf')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如梦亦如幻 2025-01-28 09:37:08

我认为您的包裹经理很有可能搞砸了。您是否使用PIP安装软件包?尝试通过conda安装(我建议创建)。

另外,我认为您在第24行中有一个错别字:feature_names = feature_na(mes)。通过通过conda +修复错字并运行代码的软件包,我得到了以下树。

I think there is a very high chance that your package manager messed up. Have you used pip to install the packages? Try installing via conda (I recommend creating conda virtual environment).

Also I think you have a typo at line 24: feature_names = feature_na(mes). By installing packages via conda + fixing the typo and running your code I got the following tree.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文