pandas 如何制作目标变量与每个预测变量的柱状图?

7kqas0il  于 2022-12-31  发布在  其他
关注(0)|答案(1)|浏览(124)

我有一个Pandasdf,看起来像这样(缩放后):

Age     blood_Press   golucse   Cholesterol
0   1.953859    -1.444088   -1.086684   -1.981315
1   0.357992    -0.123270   -0.585981   0.934929
2   0.997219    0.998712    2.005212    0.019169
3   2.589318    -0.528543   -1.123484   -1.299904
4   2.088141    0.792976    0.021526    -0.777959

和二元目标功能:

y
0   1.0
1   1.0
2   1.0
3   0.0
4   1.0

我想用y目标为每个预测性功能制作一个条形图。因此,y值将位于x轴上,也就是10,y轴上是预测性功能的值。例如,如下所示(忽略这里使用的特性,只是我需要的一个例子)。所以这里我用10代替malefemale ...

这个图的代码是这样的:

myPlot = sns.catplot(data = data, x = 'the y feature' , y = 'the x feature', kind = 'bar')
myPlot.fig.suptitle('title title', size=15, y=1.);
myPlot.set_ylabels('Y label whatever', fontsize=15, x=1.02)
myPlot.fig.set_size_inches(9,8);

但我不想对每一个特性都重复它,我相信它比这简单得多。但是怎么做呢?

ftf50wuq

ftf50wuq1#

设置

print(df)

        Age  blood_Press   golucse  Cholesterol    y
0  1.953859    -1.444088 -1.086684    -1.981315  1.0
1  0.357992    -0.123270 -0.585981     0.934929  1.0
2  0.997219     0.998712  2.005212     0.019169  1.0
3  2.589318    -0.528543 -1.123484    -1.299904  0.0
4  2.088141     0.792976  0.021526    -0.777959  1.0

Melt要从宽格式转换为长格式的 Dataframe

m = df.melt(id_vars=['y'], var_name='feature')
print(m)

#       y      feature     value
# 0   1.0          Age  1.953859
# 1   1.0          Age  0.357992
# 2   1.0          Age  0.997219
# 3   0.0          Age  2.589318
# 4   1.0          Age  2.088141
# 5   1.0  blood_Press -1.444088
# 6   1.0  blood_Press -0.123270
# 7   1.0  blood_Press  0.998712
# 8   0.0  blood_Press -0.528543
# 9   1.0  blood_Press  0.792976
# 10  1.0      golucse -1.086684
# 11  1.0      golucse -0.585981
# 12  1.0      golucse  2.005212
# 13  0.0      golucse -1.123484
# 14  1.0      golucse  0.021526
# 15  1.0  Cholesterol -1.981315
# 16  1.0  Cholesterol  0.934929
# 17  1.0  Cholesterol  0.019169
# 18  0.0  Cholesterol -1.299904
# 19  1.0  Cholesterol -0.777959

然后使用catplot方法并将col参数作为feature列传递

sns.catplot(data=m, x='y', y='value', col='feature', kind='bar', col_wrap=2)

相关问题