gpt4 book ai didi

python - Pandas:绘制从上到下的表现者

转载 作者:太空宇宙 更新时间:2023-11-03 15:22:15 25 4
gpt4 key购买 nike

我有一些数据显示每个人烤了多少个馅饼(平均)。我想绘制一个图表来显示:

average number of pies baked by top 10%, top 20%, ... top 100%:

enter image description here

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

baked_count = np.random.normal(10, scale = 3.0, size = 100)

df = pd.DataFrame(baked_count, columns = ['performance'])

df['performance'].hist()
plt.show()

points_x = []
points_y = []

x = 0
for index, row in df.sort_values('performance', ascending = False).iterrows():
y = df[df['performance'] >= row['performance']]['performance'].mean()

x += 1

points_x.append(x)
points_y.append(y)

points_x = np.array(points_x)
points_y = np.array(points_y)

plt.scatter(points_x, points_y)

plt.axvline(points_x.min(), color='g', linestyle='dashed', linewidth=1)
plt.axvline(points_x.max(), color='g', linestyle='dashed', linewidth=1)
plt.axhline(points_y.min(), color='g', linestyle='dashed', linewidth=1)
plt.axhline(points_y.max(), color='g', linestyle='dashed', linewidth=1)

plt.show()

是否有一些标准的 numpy/pyplot/pandas 方法可以做到这一点?

最佳答案

如果我理解正确的话,您想要计算排序性能系列的累积平均值。您可以通过将系列 cumsum() 除以累积计数来完成此操作。示例:

x = np.arange(1, df.shape[0]+1)
y = df.performance.sort_values(ascending=False).cumsum() / x
plt.scatter(x, y)

或者更优雅一点的 expanding意思是:

y = df.performance.sort_values(ascending=False).expanding().mean()

关于python - Pandas:绘制从上到下的表现者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43439965/

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