在对一个GridSearchCV分类器执行Randomforest之后,我试图显示一个树图。我尝试了下面的代码,但是我得到了这个错误:
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)发布于 2022-06-29 09:59:18
您需要从网格搜索中选择最佳的随机森林模型。您需要更改最后一行代码:
_ = tree.plot_tree(clf.best_estimator_.estimators_[0], feature_names=X_train.columns, filled=True)https://stackoverflow.com/questions/72792974
复制相似问题