表題の件で質問お願い致します
回帰問題について決定木のモデルを作成し、その結果を出力させようとした際に以下のエラーが発生しており、エラーで検索しても答えが見つけられず困っています
python
1NotFittedError Traceback (most recent call last) 2<ipython-input-51-8598195d41d5> in <module>() 3 4 feature_names=X_train1.columns, 4 5 impurity=False, 5----> 6 filled=True) 6 7 7 8 with open(r'C:\Users\Desktop\tree.dot') as f: 8 9~\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\tree\export.py in export_graphviz(decision_tree, out_file, max_depth, feature_names, class_names, label, filled, leaves_parallel, impurity, node_ids, proportion, rotate, rounded, special_characters, precision) 10 390 out_file.write('%d -> %d ;\n' % (parent, node_id)) 11 391 12--> 392 check_is_fitted(decision_tree, 'tree_') 13 393 own_file = False 14 394 return_string = False 15 16~\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\utils\validation.py in check_is_fitted(estimator, attributes, msg, all_or_any) 17 766 18 767 if not all_or_any([hasattr(estimator, attr) for attr in attributes]): 19--> 768 raise NotFittedError(msg % {'name': type(estimator).__name__}) 20 769 21 770 22 23NotFittedError: This GridSearchCV instance is not fitted yet. Call 'fit' with appropriate arguments before using this method.
以下実際のコードなのですが、graphvizを使用する前にモデルをfitメソッドを使って学習させているのでどこが問題かわかりません・・・
初歩的な質問ですがアドバイス宜しくお願い致します
python
1import pandas as pd 2from sklearn.tree import DecisionTreeRegressor 3from sklearn.ensemble import RandomForestRegressor 4from sklearn.model_selection import train_test_split 5from sklearn.tree import export_graphviz 6import graphviz 7import matplotlib.pyplot as plt 8import numpy as np 9from sklearn.model_selection import GridSearchCV 10import time 11from sklearn.metrics import roc_curve, auc, accuracy_score 12 13df = pd.read_csv(r'C:\Users\Desktop\test3.csv', engine='python') 14%time tmp = pd.read_csv(r'C:\Users\Desktop\test3.csv', engine='python') 15df.describe() 16 17drop_columns = [除去する列(多いので割愛します)] 18 19 20X = df.drop(drop_columns, axis=1) 21Y = df['結果'] 22X_train1, X_test1, y_train1, y_test1 = train_test_split(X, Y, random_state=42) 23 24 25 26dr_param_grid = {'max_depth':[3, 4, 5]} 27 28dr = DecisionTreeRegressor(random_state=42) 29dr_cv = GridSearchCV(dr, dr_param_grid, cv=5) 30dr_cv.fit(X_train1, y_train1) 31 32print('Training Data Score: {:.2f}'.format(dr_cv.score(X_train1, y_train1))) 33print('Test Data Score: {:.2f}'.format(dr_cv.score(X_test1, y_test1))) 34 35print('Best parameters: {}'.format(dr_cv.best_params_)) 36print('Best Cross Validation score: {:.2f}'.format(dr_cv.best_score_)) 37 38 39 40export_graphviz(dr_cv, out_file=r'C:\Users\Desktop\tree.dot', 41 feature_names=X_train1.columns, 42 impurity=False, 43 filled=True) 44 45with open(r'C:\Users\Desktop\tree.dot') as f: 46 dot_graph = f.read() 47 48graphviz.Source(dot_graph)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/08/09 04:46