質問編集履歴

3

コードはコード枠に記入しました。

2022/06/10 12:47

投稿

hima2b4
hima2b4

スコア33

test CHANGED
File without changes
test CHANGED
@@ -3,13 +3,20 @@
3
3
  TreeExplainer、モデルはxgbです。
4
4
  force plotは、回帰と二値分類の場合、以下で描画。
5
5
 
6
+ ```Python
6
7
  shap.initjs()
7
8
  shap.force_plot(explainer.expected_value, shap_values, X_selected)
8
9
 
10
+ ```
11
+
12
+
9
13
  マルチ分類の場合、以下で描画。
10
14
 
15
+ ```Python
11
16
  shap.initjs()
12
17
  shap.force_plot(explainer.expected_value[1], shap_values[1], X_selected)
18
+
19
+ ```
13
20
 
14
21
  データセットのタイプ毎にコード操作は面倒なので、
15
22
 

2

コードはコード枠に記載しました。

2022/06/10 12:45

投稿

hima2b4
hima2b4

スコア33

test CHANGED
File without changes
test CHANGED
@@ -15,15 +15,21 @@
15
15
 
16
16
  マルチ分類データですよ等と事前に認識させて、
17
17
 
18
+ ```Python
18
19
  shap.initjs()
19
20
  if dataset == ‘multi’:
20
21
  shap.force_plot(explainer.expected_value[1], shap_values[1], X_selected)
21
22
  else:
22
23
   shap.force_plot(explainer.expected_value, shap_values, X_selected)
23
24
 
25
+ ```
26
+
27
+
24
28
  として実行しても表示されません。
25
29
 
26
30
  すこし強引ですが、以下を実行しても表示されませんでした。
31
+
32
+ ```Python
27
33
 
28
34
  shap.initjs()
29
35
  try:
@@ -36,13 +42,18 @@
36
42
  except:
37
43
   pass
38
44
 
45
+ ```
46
+
39
47
  何かうまい方法をご存知の方がおられましたら、ご教示お願いできませんでしょうか?
40
48
 
41
49
 
50
+
42
51
  ※以下が現在の実行コードです。以下の実行コードに上記の変更を加えてもうまくいかなかったということになります。
52
+
43
- </>
53
+ ```Python
54
+
44
55
  #@title **Classification**(分類)or **Regression**(回帰)
45
- 回帰か分類かによってXGB.XGBClassifierを実行するか、XGB.XGBRegressorを実行するかを変えるため、ココで区分。
56
+ #回帰か分類かによってXGB.XGBClassifierを実行するか、XGB.XGBRegressorを実行するかを変えるため、ココで区分。
46
57
 
47
58
  #@title **Classification**(分類)or **Regression**(回帰)
48
59
  dataset_type = 'Classification' #@param ["Classification", "Regression"]
@@ -87,7 +98,7 @@
87
98
  shap.initjs()
88
99
  shap.force_plot(explainer.expected_value, shap_values, X)
89
100
 
90
- </>
101
+ ```
91
102
 
92
103
 
93
104
 

1

元のコードを記入しました。

2022/06/10 10:28

投稿

hima2b4
hima2b4

スコア33

test CHANGED
File without changes
test CHANGED
@@ -37,3 +37,57 @@
37
37
   pass
38
38
 
39
39
  何かうまい方法をご存知の方がおられましたら、ご教示お願いできませんでしょうか?
40
+
41
+
42
+ ※以下が現在の実行コードです。以下の実行コードに上記の変更を加えてもうまくいかなかったということになります。
43
+ </>
44
+ #@title **Classification**(分類)or **Regression**(回帰)
45
+ #回帰か分類かによってXGB.XGBClassifierを実行するか、XGB.XGBRegressorを実行するかを変えるため、ココで区分。
46
+
47
+ #@title **Classification**(分類)or **Regression**(回帰)
48
+ dataset_type = 'Classification' #@param ["Classification", "Regression"]
49
+
50
+
51
+ #@title **Load Dataset**
52
+ if dataset =='Upload':
53
+ from google.colab import files
54
+ uploaded = files.upload()#Upload
55
+ target = list(uploaded.keys())[0]
56
+ df = pd.read_csv(target)
57
+
58
+ FEATURES = df.columns[:-1]
59
+ TARGET = df.columns[-1]
60
+ X = df.loc[:, FEATURES]
61
+ y = df.loc[:, TARGET]
62
+
63
+
64
+ #@title **SHAP summary plot**
65
+
66
+ import xgboost as XGB
67
+ import shap
68
+
69
+ #XGB
70
+ if dataset_type == 'Regression':
71
+ xgb = XGB.XGBRegressor(random_state=0)
72
+ else:
73
+ xgb = XGB.XGBClassifier(random_state=0)
74
+
75
+ xgb.fit(X, y)
76
+
77
+ #SHAP
78
+ explainer = shap.TreeExplainer(xgb)
79
+ shap_values = explainer.shap_values(X)
80
+
81
+
82
+ #@title **SHAP summary plot**
83
+ shap.summary_plot(shap_values,X)
84
+
85
+
86
+ #@title **SHAP force plot**
87
+ shap.initjs()
88
+ shap.force_plot(explainer.expected_value, shap_values, X)
89
+
90
+ </>
91
+
92
+
93
+