[英]Trying to visualize ML with a grad-CAM heatmap but K.gradients returning zero values
[英]Heatmap to visualize percentage of values
我正在尋找可視化下面的結果,通過使用熱圖按列對我的數據進行分組。
數據
Classroom Subject Student
0 A Mathematics A.B.
1 B Computer Science G.M.
2 A Computer Science J.K.
3 B Literature S.R.
4 B Computer Science A.M.
5 A Literature S.R.
6 B Mathematics S.E.
7 C Literature S.T.
8 C Mathematics R.B.
9 A Mathematics B.K.
分組df.groupby(["Classroom", "Subject"]).size()
后,我有
Classroom Subject
A Mathematics 226
Literature 12
Computer Science 122
B Mathematics 1
Literature 14
Computer Science 19
History 22
Geography 238
C Mathematics 5
Literature 15
根據我在 Web 上找到的內容, Seaborn
可能是創建熱圖並顯示值百分比的最佳解決方案(如果我是對的,則為.sum()/len(df))*100)
。 此解決方案Python - 基於列值獲取百分比當然對我的問題有幫助,即使它不使用 seaborn 進行可視化。 這樣做
df.groupby(["Classroom", "Subject"]).size()/len(df)*100
我得到了值的百分比。 我還需要使用熱圖 plot 這些結果。 如果您能對此提供一些幫助,我將不勝感激。
Seaborn 的熱圖使用 dataframe 的列和索引。 Pandas 的pivot()
和pivot_table()
可以創建一個合適的 dataframe:
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
df = pd.DataFrame(
{'Classroom': np.random.choice(['A', 'B', 'C'], 1000),
'Subject': np.random.choice(['Mathematics', 'Literature', 'Computer Science', 'History', 'Geography'], 1000),
'Student': [''.join(np.random.choice([*'VWXYZ'], 7)) for _ in range(1000)]})
pivoted = pd.pivot_table(df, values='Student', index='Subject', columns='Classroom', aggfunc='count') / len(df) * 100
ax = sns.heatmap(data=pivoted, annot=True, fmt='.1f')
plt.tight_layout()
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.