PYTHON
1from sklearn.ensemble import RandomForestRegressor 2import numpy as np 3import pandas as pd 4import matplotlib.pyplot as plt 5import seaborn as sns 6from sklearn.model_selection import train_test_split 7from sklearn.metrics import r2_score 8 9#データの読み込み&表示 10df = pd.read_csv("study.csv", encoding="utf-8") 11df = df.fillna(df.mean()) 12df = df.drop("data", axis=1) 13df 14 15#yを目的変数、xを説明変数として指定 16y = df.loc[:,"water_temperature"] 17x = df.loc[:,["airtemperature","precipitation","surface_pressure","u_component_of_wind_10m","water_level"]] 18 19#x、yを学習用、評価用に8:2で分割 20x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2, train_size = 0.8, shuffle = True) 21 22#学習、評価 23rf = RandomForestRegressor() 24rf.fit(x_train, y_train) 25y_pred = rf.predict(x_test) 26print("正解率=", r2_score(y_test, y_pred)) 27 28#RandomForestRegressorの可視化 29from dtreeviz.trees import dtreeviz 30viz = dtreeviz( 31 rf, 32 x, 33 y, 34 feature_names = x.columns.values, 35 target_name = 'water_temperature' 36) 37display(viz)
上記のようにRandomForestRegressorで学習、評価を終えたあとdtreevizを使用して可視化を試みているのですがエラーが出てしまいます。
どの部分を修正したらよいか教えていただけないでしょうか。
エラーは下記に記載してあります。
AttributeError Traceback (most recent call last) <ipython-input-60-0375a6f638be> in <module> 13 y, 14 feature_names = x.columns.values, ---> 15 target_name = 'water_temperature' 16 ) 17 display(viz) ~\anaconda3\lib\site-packages\dtreeviz\trees.py in dtreeviz(tree_model, x_data, y_data, feature_names, target_name, class_names, tree_index, precision, orientation, instance_orientation, show_root_edge_labels, show_node_labels, show_just_path, fancy, histtype, highlight_path, X, max_X_features_LR, max_X_features_TD, label_fontsize, ticks_fontsize, fontname, colors, scale) 752 highlight_path = [n.id for n in path] 753 --> 754 n_classes = shadow_tree.nclasses() 755 color_values = colors['classes'][n_classes] 756 AttributeError: 'NoneType' object has no attribute 'nclasses'
あなたの回答
tips
プレビュー