![](/img/trans.png)
[英]p-value and null hypothesis for the alternatives of scipy.stats.mannwhitneyu
[英]P-Value to test hypothesis shows NaN
在尝试测试 null 假设时,我得到了 p.value 的 NaN,即电话计划冲浪的收入平均值与最终计划的收入平均值相同。 我不明白我做错了什么。 我假设它可能与我的 DataFrame call_plan_merge
。 在monthly_revenue
列中有一些NaN 值(在我在这里发布的内容中不可见)。 这可能是原因吗? 但同时计算的平均值(我们可以看到是在忽略monthly_revenue
列中的NaN 的情况下正确计算的)已经在用于检验假设的变量中,所以我不明白会为p 值生成NaN .
这是我的代码:
#The average revenue from users of Ultimate and Surf calling plans differs.
average_rev_surf = call_plan_merge.query('tariff == "surf"')
average_rev_surf = average_rev_surf['monthly_revenue'].mean()
average_rev_ultimate = call_plan_merge.query('tariff == "ultimate"')
average_rev_ultimate = average_rev_ultimate['monthly_revenue'].mean()
alpha = 0.05 # critical statistical significance
results = st.ttest_1samp(average_rev_surf, average_rev_ultimate)
print('p-value:', results.pvalue)
if results.pvalue < alpha:
print('We reject the null hypothesis')
else:
print("We can't reject the null hypothesis")
print('Average revenue for the surf plan is: {:.2f}$'.format(average_rev_surf))
print('Average revenue for the ultimate plan is: {:.2f}$'.format(average_rev_ultimate))
Output:
p-value: nan
We can't reject the null hypothesis
Average revenue for the surf plan is: 35.77$
Average revenue for the ultimate plan is: 36.32$
这就是call_plan_merge
的样子:
user_id call_month total_calls duration tariff reg_month churn_month state monthly_revenue
0 1000.0 12.0 16.0 124.0 ultimate 12 13.0 GA 70.00
1 1001.0 8.0 27.0 182.0 surf 8 13.0 WA 20.00
2 1001.0 9.0 49.0 315.0 surf 8 13.0 WA 20.00
3 1001.0 10.0 65.0 393.0 surf 8 13.0 WA 90.09
4 1001.0 11.0 64.0 426.0 surf 8 13.0 WA 60.00
5 1001.0 12.0 56.0 412.0 surf 8 13.0 WA 60.00
6 1002.0 10.0 11.0 59.0 surf 10 13.0 NV 20.00
7 1002.0 11.0 55.0 386.0 surf 10 13.0 NV 60.00
8 1002.0 12.0 47.0 384.0 surf 10 13.0 NV 20.00
9 1003.0 12.0 149.0 1104.0 surf 1 13.0 OK 158.12
非常感谢你的帮助!
您的错误是由average_rev_surf = average_rev_surf['monthly_revenue'].mean()
引起的。 此外,您不是在与单个组打交道。 您正在处理两个独立的组,因此您使用了错误的 function。
ttest_1samp()
必须接收类似数组的结构作为a
和在 null 假设下作为popmean
的总体均值。 通过传递a=average_rev_surf
,您正在使 function 计算自由度为1 - 1 = 0的t统计量,这显然为统计量和 p 值返回NaN
。
看起来你有很多不同的用户,每个用户都有自己的资费。 为了测试他们的收入是否不同,您应该使用scipy.stats.ttest_ind()
因为您的样本是独立的。
尝试以下方式:
# Monthly revs of surf users
surf = average_rev_surf.loc[average_rev_surf['tariff'].eq('surf'), 'monthly_revenue']
# Monthly revs of ultimate users
ulti = average_rev_surf.loc[average_rev_surf['tariff'].eq('ultimate'), 'monthly_revenue']
# t-test for independent samples
results = st.ttest_ind(a=surf, b=ultimate)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.