导出/绘制随机森林决策树/'RandomForestClassifier' 对象没有属性 'tree_'
Export / Plot Random Forest Decision Tree / 'RandomForestClassifier' object has no attribute 'tree_'
大家晚上好
来自这个post的objective就是能够从随机决策树过程中绘制出决策树。在 运行 不同的选项之后我总是得到下一个错误: 'RandomForestClassifier' object has no attribute 'tree_'
非常感谢能够解决这种情况的任何帮助/代码示例/想法或链接。
在下一组代码中,我是如何绘制常规/正常决策树的。
clf_SMOTE1 = DecisionTreeClassifier(criterion='entropy',max_depth=4, min_samples_leaf=7)
clf_SMOTE1
clf_SMOTE1 = clf_SMOTE1.fit(X_train, Y_train)
a = df.columns[6:]
dot_data = tree.export_graphviz(clf_SMOTE1, out_file=None, filled=False, feature_names= a)
graphviz.Source(dot_data)
下一行是我尝试过但没有结果的不同尝试。
clf_SMOTE2 = RandomForestClassifier(criterion='entropy', bootstrap = True, max_depth=4, min_samples_leaf=7)
clf_SMOTE2
clf_SMOTE2 = clf_SMOTE2.fit(X_train, Y_train)
a = df.columns[6:]
dot_data_2 = tree.export_graphviz (clf_SMOTE2, out_file=None, feature_names = a, precision = 2, filled = False)
graphviz.Source(dot_data_2)
选项 2:
clf_SMOTE2 = RandomForestClassifier(criterion='entropy', bootstrap = True, max_depth=4, min_samples_leaf=7)
clf_SMOTE2
clf_SMOTE2 = clf_SMOTE2.fit(X_train, Y_train)
a = df.columns[6:]
dot_data_2 = tree.plot_tree(clf_SMOTE2, model.estimators_[5], feature_names= a)
graphviz.Source(dot_data_2)
来自help page:
A random forest is a meta estimator that fits a number of decision
tree classifiers on various sub-samples of the dataset
所以你不能在 RandomForestClassifier
对象上应用 export_graphviz
。您需要访问存储在 estimators_
下的决策树之一:
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
from sklearn.tree import export_graphviz
import pandas as pd
X,y = make_classification()
X = pd.DataFrame(X,columns = ['col'+str(i) for i in range(X.shape[1])])
clf = RandomForestClassifier(criterion='entropy', bootstrap = True,
max_depth=4, min_samples_leaf=7)
clf = clf.fit(X, y)
这里我们只访问 100 个决策树中的第一个:
len(clf.estimators_)
100
dot_data_2 = export_graphviz(clf.estimators_[0], out_file=None,
feature_names = X.columns, precision = 2, filled = False)
大家晚上好
来自这个post的objective就是能够从随机决策树过程中绘制出决策树。在 运行 不同的选项之后我总是得到下一个错误: 'RandomForestClassifier' object has no attribute 'tree_'
非常感谢能够解决这种情况的任何帮助/代码示例/想法或链接。
在下一组代码中,我是如何绘制常规/正常决策树的。
clf_SMOTE1 = DecisionTreeClassifier(criterion='entropy',max_depth=4, min_samples_leaf=7)
clf_SMOTE1
clf_SMOTE1 = clf_SMOTE1.fit(X_train, Y_train)
a = df.columns[6:]
dot_data = tree.export_graphviz(clf_SMOTE1, out_file=None, filled=False, feature_names= a)
graphviz.Source(dot_data)
下一行是我尝试过但没有结果的不同尝试。
clf_SMOTE2 = RandomForestClassifier(criterion='entropy', bootstrap = True, max_depth=4, min_samples_leaf=7)
clf_SMOTE2
clf_SMOTE2 = clf_SMOTE2.fit(X_train, Y_train)
a = df.columns[6:]
dot_data_2 = tree.export_graphviz (clf_SMOTE2, out_file=None, feature_names = a, precision = 2, filled = False)
graphviz.Source(dot_data_2)
选项 2:
clf_SMOTE2 = RandomForestClassifier(criterion='entropy', bootstrap = True, max_depth=4, min_samples_leaf=7)
clf_SMOTE2
clf_SMOTE2 = clf_SMOTE2.fit(X_train, Y_train)
a = df.columns[6:]
dot_data_2 = tree.plot_tree(clf_SMOTE2, model.estimators_[5], feature_names= a)
graphviz.Source(dot_data_2)
来自help page:
A random forest is a meta estimator that fits a number of decision tree classifiers on various sub-samples of the dataset
所以你不能在 RandomForestClassifier
对象上应用 export_graphviz
。您需要访问存储在 estimators_
下的决策树之一:
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
from sklearn.tree import export_graphviz
import pandas as pd
X,y = make_classification()
X = pd.DataFrame(X,columns = ['col'+str(i) for i in range(X.shape[1])])
clf = RandomForestClassifier(criterion='entropy', bootstrap = True,
max_depth=4, min_samples_leaf=7)
clf = clf.fit(X, y)
这里我们只访问 100 个决策树中的第一个:
len(clf.estimators_)
100
dot_data_2 = export_graphviz(clf.estimators_[0], out_file=None,
feature_names = X.columns, precision = 2, filled = False)