' GridSearchCV'对象没有属性'估算值_'使用dtreeviz
在Randomforest
分类器上执行GridSearchCV
之后,我正在尝试显示树图。我尝试了下面的代码,但是我得到了此错误:
AttributeError: 'GridSearchCV' object has no attribute 'estimators_'
您能告诉我如何修复此错误并获取树的视图吗?
这是我的分类器代码:
model = RandomForestClassifier()
parameter_space = {
'n_estimators': [10,50,100],
'criterion': ['gini', 'entropy'],
'max_depth': np.linspace(10,50,11),
}
clf = GridSearchCV(model, parameter_space, cv = 5, scoring = "accuracy", verbose = True) # model
clf.fit(X_train,y_train)
train_pred = clf.predict(X_train) # Train predict
test_pred = clf.predict(X_test) # Test predict
# Load packages
import pandas as pd
from sklearn import tree
from dtreeviz.trees import dtreeviz # will be used for tree visualization
from matplotlib import pyplot as plt
plt.rcParams.update({'figure.figsize': (12.0, 8.0)})
plt.rcParams.update({'font.size': 14})
plt.figure(figsize=(20,20))
_ = tree.plot_tree(clf.n_estimators_[0], feature_names=X_train.columns, filled=True)
After carrying out a GridSearchCV
on a Randomforest
classifer, I am attempting to display a tree plot. I tried the code below, but I get this error:
AttributeError: 'GridSearchCV' object has no attribute 'estimators_'
Can you tell me how to fix this error and get a view of a tree?
Here is my code from the classifier:
model = RandomForestClassifier()
parameter_space = {
'n_estimators': [10,50,100],
'criterion': ['gini', 'entropy'],
'max_depth': np.linspace(10,50,11),
}
clf = GridSearchCV(model, parameter_space, cv = 5, scoring = "accuracy", verbose = True) # model
clf.fit(X_train,y_train)
train_pred = clf.predict(X_train) # Train predict
test_pred = clf.predict(X_test) # Test predict
# Load packages
import pandas as pd
from sklearn import tree
from dtreeviz.trees import dtreeviz # will be used for tree visualization
from matplotlib import pyplot as plt
plt.rcParams.update({'figure.figsize': (12.0, 8.0)})
plt.rcParams.update({'font.size': 14})
plt.figure(figsize=(20,20))
_ = tree.plot_tree(clf.n_estimators_[0], feature_names=X_train.columns, filled=True)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要从网格搜索中选择最佳的随机森林模型。您需要更改最后一行的代码:
You need to select the best Random Forest model from the grid search. You need to change your last line of code :