【Python】バイオリンプロット(Violin Plot)


1. 目的
2. データ(例:AIの予測値と正解値)
2.1. PVHs.csv
3. コード
4. 実行結果


1. 目的

  • Pythonのseabornを使ってバイオリンプロットを作成

2. データ(例:AIの予測値と正解値)

ここで使うデータは、AIが判定した予測値と正解値(0~3)。

  • y_true: 正解ラベル
  • y_pred: 予測したラベル

2.1. PVHs.csv

y_true y_pred
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 1
0 1
0 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 2
1 2
1 3
2 1
2 2
2 2
2 2
2 2
2 2
2 2
2 2
2 2
2 2
2 2
2 2
3 2
3 3
3 3
3 3
3 3
3 3
3 3
3 3
3 3
3 3

3. コード

import seaborn
import pandas as pd
from matplotlib import pyplot as plt
import matplotlib.ticker as ticker

disease='PVHs'
df = pd.read_csv('{}.csv'.format(disease))
# print(df)

plt.gca().get_yaxis().set_major_locator(ticker.MaxNLocator(integer=True))
seaborn.violinplot(x=df['y_true'], y=df['y_pred']) 
# seaborn.violinplot(x=df['y_true'], y=df['y_pred'],cut=0)  # 確率密度分布を制限して表示する場合

plt.savefig('{}.png'.format(disease))
plt.show()

4. 実行結果

作成したヴァイオリンプロットは以下。

本来は、ラベルは0から3であるが、確率密度分布が-1や4にまで広がっている。
それが気持ち悪い場合は、seaborn.violinplotの引数にcut=0を渡してやる。

Print Friendly, PDF & Email

コメントを残す

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください