决策树旁边的黑色实心矩形是什么?

问题描述 投票:0回答:1

我改编自https://www.dasca.org/world-of-big-data/article/know-how-to-create-and-visualize-a-decision-tree-with-python的代码。

我删除了 DecisionTreeClassifier 构造函数的两个参数,min_impurity_split=None 和 presort=False,但除此之外,代码与我找到的代码相同。

import sklearn.datasets as datasets
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
#from sklearn.externals.six import StringIO  
from IPython.display import Image  
from sklearn.tree import export_graphviz
import pydotplus
from six import StringIO
iris=datasets.load_iris()
df=pd.DataFrame(iris.data, columns=iris.feature_names)
y=iris.target
dtree=DecisionTreeClassifier()
dtree.fit(df,y)

# Limit max depth
model = RandomForestClassifier(max_depth = 3, n_estimators=10)
# Train
model.fit(iris.data, iris.target)
# Extract single tree
estimator_limited = model.estimators_[5]
estimator_limited

# Removed  min_impurity_split=None and presort=False because they caused "unexpected keyword argument" errors
DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=3,
            max_features='auto', max_leaf_nodes=None,
            min_impurity_decrease=0.0,
            min_samples_leaf=1, min_samples_split=2,
            min_weight_fraction_leaf=0.0, 
            random_state=1538259045, splitter='best')
# No max depth
model = RandomForestClassifier(max_depth = None, n_estimators=10)
model.fit(iris.data, iris.target)
estimator_nonlimited = model.estimators_[5]

dot_data = StringIO()
export_graphviz(dtree, out_file=dot_data,  
                filled=True, rounded=True,
                special_characters=True)

graph = pydotplus.graph_from_dot_data(dot_data.getvalue())  
Image(graph.create_png())

决策树如下所示:

python graphviz decision-tree pydotplus
1个回答
0
投票

黑色矩形是 pydotplus 中的一个错误。您可以通过对代码中相应行进行以下修改来修复它:

graph = pydotplus.graph_from_dot_data(dot_data.getvalue()..replace(" ”,“”))

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.