質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

機械学習

機械学習は、データからパターンを自動的に発見し、そこから知能的な判断を下すためのコンピューターアルゴリズムを指します。人工知能における課題のひとつです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

3021閲覧

python 決定木をdotファイルで出力する際のエラー

---stax---

総合スコア148

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

機械学習

機械学習は、データからパターンを自動的に発見し、そこから知能的な判断を下すためのコンピューターアルゴリズムを指します。人工知能における課題のひとつです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2018/08/09 02:23

表題の件で質問お願い致します
回帰問題について決定木のモデルを作成し、その結果を出力させようとした際に以下のエラーが発生しており、エラーで検索しても答えが見つけられず困っています

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)

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

第一引数にはdecision tree classifierのインスタンスを渡せとドキュメントに書いてありますね。GridSearchCVのインスタンスは受け付けないようです。
(エラーメッセージが不親切というか、入力されたインスタンスの型を見て例外投げる処理とかちゃんとやってない雰囲気なのですが。svmとか放り込んでも同じメッセージが出ますよ)

sklearn.tree.export_graphviz — scikit-learn 0.19.2 documentation

ドキュメントに詳しい説明はまったくありませんが、一応回帰木自体は出力できるようです。

python

1from sklearn.datasets import load_boston 2from sklearn.tree import DecisionTreeRegressor 3from sklearn.tree import export_graphviz 4 5boston = load_boston() 6dtr = DecisionTreeRegressor(max_depth=3) 7dtr.fit(boston.data, boston.target) 8export_graphviz(dtr, feature_names=boston.feature_names, class_names=boston.target, filled=True) 9# shellで$ dot -Tpng tree.dot -o tree.pngして画像を出力 10

イメージ説明

GridSearchCVのインスタンスのbest_estimator_を渡してあげれば良いでしょう。

投稿2018/08/09 03:00

編集2018/08/09 03:30
hayataka2049

総合スコア30933

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

---stax---

2018/08/09 04:46

ありがとうございます。 探し切れていませんでした・・・。 グリッドサーチして出た結果をもとに描画用のモデルを作り直してたのですが、アドバイス頂いたとおりに追記すればGridSearchCVのモデルでも描画できました。 ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問