【问题标题】:Add probability of x and conversion %添加 x 的概率和转换百分比
【发布时间】:2018-06-06 19:09:23
【问题描述】:

这是当前数据的样子:

id testers_time stage_1_to_2_time activated_time stage_2_to_3_time engaged_time
a  10           30                40             30                70
b  30               
c  15           30                45        
d       

dict = {'id': ['a','b','c','d'], 'testers_time': [10, 30, 15, None], 'stage_1_to_2_time': [30, None, 30, None], 'activated_time' : [40, None, 45, None],'stage_2_to_3_time' : [30, None, None, None],'engaged_time' : [70, None, None, None]} 
df = pd.DataFrame(dict, columns=['id', 'testers_time', 'stage_1_to_2_time', 'activated_time', 'stage_2_to_3_time', 'engaged_time']) 

我有一张 testers_time 与 CDF 累积概率的图:

def ecdf(df):
    n = len(df)
    x = np.sort(df)
    y = np.arange(1.0, n+1) / n
    return x, y

df = df['testers_time'].dropna().sort_values()
print(df)

x, y = ecdf(df)

plt.plot(x, y, marker='.', linestyle='none') 

plt.axvline(x.mean(), color='gray', linestyle='dashed', linewidth=2) #Add mean 

x_m = int(x.mean()) 
y_m = stats.percentileofscore(df, x.mean())/100.0 

plt.annotate('(%s,%s)' % (x_m,int(y_m*100)) , xy=(x_m,y_m), xytext=(10,-5), textcoords='offset points') 

percentiles= np.array([0,25,50,75,100]) 
x_p = np.percentile(df, percentiles) 
y_p = percentiles/100.0 

plt.plot(x_p, y_p, marker='D', color='red', linestyle='none') # Overlay quartiles 

for x,y in zip(x_p, y_p): 
    plt.annotate('%s' % int(x), xy=(x,y), xytext=(10,-5), textcoords='offset points') 

我想要做的是将testers_time 与:

1) 它的非累积概率,如果绘制成图形,它应该看起来像一种 PDF

2) 其累积转化百分比,其中转化是具有填充(非空白或空)testers_time 的任何id。所以 id a(4 个 id 中的 1 个)转化,即 25%,id b 转化,即 50%(因为累积),id c 转化,即 75%,而 id d 不转化,所以 75 % 转化率是最大值,在 30 天 testers_time

您能否协助将以上内容添加到df 的列中,或绘制图表?谢谢。

【问题讨论】:

  • 我很确定 ecdf(df) 没有按预期工作。例如你分配给df['x'],然后返回x。此外,我认为如果您提供了a minimal example(您的df 似乎有不相关的列)和预期的输出,这将有所帮助。我不清楚“最大是 30 天 75% 的转化率”是什么意思。
  • @abukaj 我编辑了代码,请按原样运行。我还对您的问题进行了解释。非常感谢您的帮助。如果在激励方面有任何帮助,仍然有 21 小时的时间来了解赏金:)
  • 看看.rank()对象的.rank()方法。
  • @abukaj 所以按照:df['rank'] = df['testers_time'].rank(ascending=1)/len(df) 进行转换。我想我可以解决这个问题,谢谢。 Q1 呢?
  • 我试试numpy.unique(),它有计数选项。

标签: python python-2.7 matplotlib


【解决方案1】:

A1:df['prob'] = df['testers_time'].map(df.testers_time.value_counts(normalize=True))

A2:df['conv'] = df['testers_time'].rank(ascending=1)/len(df)

【讨论】:

    猜你喜欢
    • 2013-12-24
    • 2015-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-31
    • 1970-01-01
    • 2022-01-09
    相关资源
    最近更新 更多