bad*_*ddu 3 python statistics machine-learning data-analysis pandas
在下面关于统计的问题中,我正在尝试用 python 进行“双样本独立 t 检验”。
一家百货公司的分析师想要评估最近的信用卡促销活动。为此,随机抽取了 500 名持卡人。一半人收到了宣传未来三个月内购买利率降低的广告,一半人收到了标准季节性广告。促销是否能有效提高销量?下面是我的代码。我在编写代码时犯了一些错误,请帮忙。
from scipy import stats
std_promo = cust[(cust['insert'] == 'Standard')]
new_promo = cust[(cust['insert'] == 'New Promotion')]
print(std_promo.head(3))
print(new_promo.head(3))
id insert dollars
0 148 Standard 2232.771979
2 973 Standard 2327.092181
3 1096 Standard 1280.030541
id insert dollars
1 572 New Promotion 1403.807542
4 1541 New Promotion 1513.563200
5 1947 New Promotion 1729.627996
print (std_promo.mean())
print (new_promo.mean())
id 69003.000000
dollars 1566.389031
dtype: float64
id 64998.244000
dollars 1637.499983
dtype: float64
print (std_promo.std())
print (new_promo.std())
id 37753.106923
dollars 346.673047
dtype: float64
id 38508.218870
dollars 356.703169
dtype: float64
stats.ttest_ind(a= std_promo, b= new_promo, equal_var= True)
Run Code Online (Sandbox Code Playgroud)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-76-b40f7d9d7a3e> in <module>
1 stats.ttest_ind(a= std_promo,
----> 2 b= new_promo)
~\Anaconda3\lib\site-packages\scipy\stats\stats.py in ttest_ind(a, b, axis, equal_var, nan_policy)
4163 return Ttest_indResult(np.nan, np.nan)
4164
-> 4165 v1 = np.var(a, axis, ddof=1)
4166 v2 = np.var(b, axis, ddof=1)
4167 n1 = a.shape[axis]
~\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py in var(a, axis, dtype, out, ddof, keepdims)
3365
3366 return _methods._var(a, axis=axis, dtype=dtype, out=out, ddof=ddof,
-> 3367 **kwargs)
3368
3369
~\Anaconda3\lib\site-packages\numpy\core\_methods.py in _var(a, axis, dtype, out, ddof, keepdims)
108 if isinstance(arrmean, mu.ndarray):
109 arrmean = um.true_divide(
--> 110 arrmean, rcount, out=arrmean, casting='unsafe', subok=False)
111 else:
112 arrmean = arrmean.dtype.type(arrmean / rcount)
TypeError: unsupported operand type(s) for /: 'str' and 'int'
Run Code Online (Sandbox Code Playgroud)
我认为你需要改变:
stats.ttest_ind(a= std_promo, b= new_promo, equal_var= True)
Run Code Online (Sandbox Code Playgroud)
到
stats.ttest_ind(a= std_promo.dollars, b= new_promo.dollars, equal_var= True)
Run Code Online (Sandbox Code Playgroud)
我创建了一个与您类似的 DF,并运行它并且它使用美元工作:
Ttest_indResult(statistic=7.144078895160622, pvalue=9.765848295636031e-05)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
999 次 |
| 最近记录: |