バギングしたランダムフォレストの決定木の中からテストスコアが一番良いものを表示したいです。
以下のようなコードを書きました。
clf=BaggingClassifier(RandomForestClassifier(max_depth=depth, random_state=0), n_estimators=100, random_state=0)
#モデルを構築
clf = clf.fit(X_train,y_train)
#決定木ごとに結果を表示
train_score=np.zeros(100)
test_score=np.zeros(100)
for i,val in enumerate(clf.estimators_):
model=clf.estimators_[i]
model=model.fit(X_train,y_train)
train_score[i]=model.score(X_train,y_train)
test_score[i]=model.score(X_test,y_test)
num=np.argmax(test_score)
#データを可視化
model=clf.estimators_[num]
model=model.fit(X_train,y_train)
dot_data = tree.export_graphviz(model,feature_names=["Tol"],class_names=["C1","good","C5,D5"],filled=True, rounded=True)
以下のようなエラーが出ます。
Traceback (most recent call last):
File "rf.py", line 79, in <module>
dot_data = tree.export_graphviz(model,feature_names=["Tol"],class_names=["C1","good","C5,D5"],filled=True, rounded=True)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sklearn/tree/export.py", line 757, in export_graphviz
check_is_fitted(decision_tree, 'tree_')
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sklearn/utils/validation.py", line 914, in check_is_fitted
raise NotFittedError(msg % {'name': type(estimator).__name__})
sklearn.exceptions.NotFittedError: This RandomForestClassifier instance is not fitted yet. Call 'fit' with appropriate arguments before using this method.