gpt4 book ai didi

python - 为多列绘制 seaborn catplots

转载 作者:行者123 更新时间:2023-12-04 12:40:00 24 4
gpt4 key购买 nike

我有一个包含 93 个特征和 9 个类标签的数据框。我想用各自的类标签绘制每个特征的值,但是,我想生成一个包含 93 个图的子图,每个图代表数据集中的一个特征。我可以制作一个情节,它看起来像这样:

sns.catplot(x="feat_1", y="target", data=train)

enter image description here

现在我基本上想重复同样的事情,但是以刻面网格的形式重复 93 次。我尝试创建一个包含 5 列和 19 行的子图,然后在轴上循环,但失败了......感谢您的帮助,我的数据看起来像这样(93 个特征列和一个目标列):
    feat_1  feat_2  feat_3  feat_4  feat_5  feat_6  feat_7  feat_8  feat_9  feat_10 ... feat_85 feat_86 feat_87 feat_88 feat_89 feat_90 feat_91 feat_92 feat_93 target
id
32518 0 0 0 1 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 Class_6
31734 0 1 7 5 0 0 0 0 0 1 ... 0 0 0 1 2 0 1 4 0 Class_6
57027 0 0 0 0 0 0 0 2 0 0 ... 0 0 0 0 0 0 1 0 0 Class_9
31629 0 1 0 0 0 0 0 1 1 0 ... 0 0 0 1 2 0 0 0 0 Class_6
14216 2 0 0 0 0 0 0 0 0 0 ... 0 0 0 1 0 0 0 0 0 Class_2
17376 0 0 0 0 0 0 0 0 0 0 ... 0 2 0 1 0 0 0 0 0 Class_2
10520 1 0 0 0 0 0 0 0 0 0 ... 0 3 0 0 0 0 0 0 0 Class_2
7665 0 0 0 0 0 0 0 0 0 0 ... 0 2 0 3 0 0 0 0 0 Class_2
26692 0 0 0 0 0 0 0 0 0 0 ... 4 0 0 0 0 0 0 0 0 Class_4
36809 0 0 3 4 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 1 0 Class_6
47959 0 1 0 3 0 2 1 0 0 1 ... 6 0 0 0 1 1 0 0 1 Class_7
22649 0 0 0 0 1 0 0 0 0 1 ... 21 0 1 0 0 2 0 0 0 Class_3
34550 0 0 1 2 0 0 1 0 0 0 ... 0 0 1 0 0 1 1 1 1 Class_6
39943 3 0 0 0 0 0 0 0 0 0 ... 0 0 2 0 0 0 0 0 0 Class_6
38900 1 0 6 14 0 0 1 0 0 0 ... 0 0 1 0 0 0 0 0 0 Class_6
26333 0 0 1 0 0 0 1 1 0 0 ... 0 0 1 1 0 0 0 0 0 Class_4
16126 0 0 0 0 0 0 0 0 0 0 ... 0 0 1 10 0 0 0 0 0 Class_2
10490 0 0 0 0 0 0 0 1 0 0 ... 0 0 0 0 0 0 0 3 0 Class_2
58603 0 0 0 0 0 0 0 1 0 0 ... 0 0 0 0 0 0 28 0 1 Class_9
52668 0 0 1 2 0 0 0 4 0 0 ... 0 0 0 0 4 0 0 0 0 Class_8

最佳答案

利用seaborn的FacetGrid (由 catplot 使用)您需要将数据框从“宽”转换为“长”

# dummy dataframe
N=20
N_features = 10
N_classes = 5
df = pd.DataFrame({f'feat_{i+1}': np.random.random(size=(N,)) for i in range(N_features)})
df['target'] = np.random.choice([f'Class_{i+1}' for i in range(N_classes)], size=(N,))

# transform from wide to long, then plot using the column 'features' to facet
df2 = df.melt(id_vars=['target'], var_name='features')
sns.catplot(data=df2, x='value', y='target', col='features', col_wrap=5, height=3, aspect=0.5)

enter image description here

关于python - 为多列绘制 seaborn catplots,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60403300/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com