下記のプログラムをVSCodeのJupyter Notebookで実行しようとしたが、以下のエラーメッセージが出ます。
dtreeviz,pyGraphvizもダウンロードしており、graphvizもダウンロード・pathも通しております
graphvizはversion 8.0.5です。
dtreevizはversion 2.2.1です。
アドバイスを頂けるとありがたいです。
エラーメッセージ:
C:\Users\jy132\AppData\Local\Temp\ipykernel_23164\404471695.py:4: DeprecationWarning: dtreeviz() function is deprecated starting from version 2.0.
For the same functionality, please use this code instead:
m = dtreeviz.model(...)
m.view()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[10], line 4
1 # ライブラリのインポート
2 from dtreeviz import *
----> 4 viz = dtreeviz(clf, df_x, df['inf1b'],
5 feature_names = df_x.columns,
6 target_name='メディアの使用頻度',
7 #class_names = list(data_target_names),
8 )
10 display(viz)
File c:\Users\jy132\AppData\Local\Programs\Python\Python310\lib\site-packages\dtreeviz\compatibility.py:254, in dtreeviz(tree_model, X_train, y_train, 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, depth_range_to_display, label_fontsize, ticks_fontsize, fontname, title, title_fontsize, colors, scale)
251 shadow_tree = ShadowDecTree.get_shadow_tree(tree_model, X_train, y_train, feature_names, target_name, class_names,
252 tree_index)
253 model = DTreeVizAPI(shadow_tree)
--> 254 return model.view(precision, orientation,
255 instance_orientation,
256 show_root_edge_labels, show_node_labels, show_just_path, fancy, histtype, highlight_path, X,
257 max_X_features_LR, max_X_features_TD, depth_range_to_display, label_fontsize, ticks_fontsize,
258 fontname, title, title_fontsize, colors=colors, scale=scale)
File c:\Users\jy132\AppData\Local\Programs\Python\Python310\lib\site-packages\dtreeviz\trees.py:519, in DTreeVizAPI.view(self, precision, orientation, instance_orientation, show_root_edge_labels, show_node_labels, show_just_path, fancy, histtype, leaftype, highlight_path, x, max_X_features_LR, max_X_features_TD, depth_range_to_display, label_fontsize, ticks_fontsize, fontname, title, title_fontsize, colors, scale)
517 tmp = tempfile.gettempdir()
518 if x is not None:
...
--> 335 if self.shouldGoLeftAtSplit(t.id, x[t.feature()]):
336 return walk(t.left, x, path)
337 return walk(t.right, x, path)
TypeError: 'int' object is not subscriptable
ソースコード:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#import japanize_matplotlib
#import pydot
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn import metrics, tree
df = pd.DataFrame(pd.read_csv('ds1.csv'))
df = df.drop(columns=df.columns[0])
df.head()
Col_x=["B_sub","ni_f","rcv_f","s","political","age"]
df_x = df[Col_x]
df_y = df["inf1b"]
train_x, test_x, train_y, test_y = train_test_split(df_x, df_y, random_state=1, test_size=0.15)
# モデルの作成
clf = DecisionTreeClassifier(max_depth=3)
# モデルの学習
clf.fit(train_x, train_y)
# 評価
clf.score(train_x, train_y)
clf.score(test_x, test_y)
# ライブラリのインポート
from dtreeviz import *
#ここの部分をimport dtreevizに変更
viz = dtreeviz(clf, df_x, df['inf1b'],
feature_names = df_x.columns,
target_name='メディアの使用頻度',
#class_names = list(data_target_names),
)
display(viz)
#上記の部分を以下のように変更したら正常に動作した。
viz = dtreeviz.model(clf,
X_train=df_x,
y_train=df['inf1b'],
feature_names = df_x.columns,
target_name='Media usage frequency',
class_names=["don't use much","Neither","use much"]
)
viz.view()