3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

shap の force plotを、グラフではなく、数値やデータとして取得したい。

Last updated at Posted at 2022-12-14

起きた問題

image.png
wsl上で、Shapの force_plotのグラフを出力しようとしたが、なんかもうレイアウトが崩れまくる。

特に特徴量の名前がぐっちゃぐちゃ。

やりたいこと

このままでは、どの特徴量の影響度が高いのかわからない。
グラフで出すのは良いんだけど、テキストで見やすく出力したい。

できれば、数値のデータも取得して自分で分析できるようにしたい。

解決策

force_plotmatplotlibFalseにすると、 shap.force_plotAdditiveForceVisualizer のオブジェクトを返す。

AdditiveForceVisualizer はこんな感じの構造になっている。

image.png

つまり、data に特徴量の名前(featureNames)や、影響度(effect)を持っている。

ので、こんな感じで出力してやると effect の大きい順に出力できる。

ret=shap.force_plot(explainer.expected_value[0], shap_values=shap_values[0], features=prediction_row,feature_names=fn, matplotlib=False,link="logit",show=True, contribution_threshold=0.001)
feature_array = []
for i in ret.data["features"].keys():
    featureName = ret.data["featureNames"][i]
    feature = ret.data["features"][i]
    feature["name"] = featureName
    feature_array.append(feature)

feature_array = sorted(feature_array, key=lambda x: x['effect'],reverse=True)

for feature in feature_array:
    print(feature)
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?