绘制 2 个数据点之间的关系,其中一个数据点是布尔值
数据挖掘
Python
熊猫
2022-02-11 06:49:24
1个回答
您可以预先计算存活率(概率)并绘制条形图:
import seaborn as sns
x = sns.load_dataset('titanic')
bins = np.linspace(0, 100, 11)
labels = bins[1:]
# let's group all ages by bins (10, 20, 30, ..., 100)
rpt = (x.groupby(pd.cut(x.age, bins, labels=labels))
.survived.mean()*100
).dropna().to_frame('survival_rate')
rpt.plot.bar(rot=0, width=0.8, alpha=0.5, figsize=(12, 10))
计算数据:
In [84]: bins
Out[84]: array([ 0., 10., 20., 30., 40., 50., 60., 70., 80., 90., 100.])
In [85]: labels
Out[85]: array([ 10., 20., 30., 40., 50., 60., 70., 80., 90., 100.])
In [86]: rpt
Out[86]:
survival_rate
age
10.0 59.375000
20.0 38.260870
30.0 36.521739
40.0 44.516129
50.0 38.372093
60.0 40.476190
70.0 23.529412
80.0 20.000000
其它你可能感兴趣的问题


