假设我有这个熊猫数据框,
pC Truth
0 0.601972 0
1 0.583300 0
2 0.595181 1
3 0.418910 1
4 0.691974 1
“ pC”是“真相”为1的概率。“真相”是二进制值。我想创建概率的直方图,并且每个bin的内部将是比例0与比例1。
我尝试了以下方法
df[['pC','Truth']].plot(kind='hist',stacked=True)
它只是将“真”值放在0到1之间。
可重现:
shape = 1000
df_t = pd.DataFrame({'pC': np.random.rand(shape),
'Truth':np.random.choice([0,1],size=shape)})
df_t['factor'] = pd.cut(df_t.pC,5)
我该怎么做呢?谢谢
解决了,
shape = 1000
df_t = pd.DataFrame({'pC': np.random.rand(shape),
'Truth':np.random.choice([0,1],size=shape)})
df_t['factor'] = pd.cut(df_t.pC,5)
df_p = (df_t[['factor','Truth']]
.pivot_table(columns='Truth',index='factor',aggfunc=len,fill_value=0)
.reset_index())
df_p[['factor',0,1]].plot(kind='bar',stacked=True,x='factor');
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句