【问题标题】:Why am I getting TypeError: unsupported operand type(s) for /: 'str' and 'int'?为什么我收到 TypeError: unsupported operand type(s) for /: 'str' and 'int'?
【发布时间】:2020-04-14 06:01:33
【问题描述】:

在下面关于统计的问题中,我正在 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)
---------------------------------------------------------------------------
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'

【问题讨论】:

  • 对于stats.ttest_ind,参数 a 和 b 应该是列表/数组。在您的情况下,它们是数据框。请将参数更改为列表/数组并再次运行代码。请参考 scipy stats 网站。
  • 但是,我在这里传递 std_promo 和 new_promo 它们是熊猫系列,它是一维标记数组。谢谢你,我一定会推荐这个网站,因为我是新手,还在学习。

标签: python pandas machine-learning statistics data-analysis


【解决方案1】:

我认为你需要改变:

stats.ttest_ind(a= std_promo, b= new_promo, equal_var= True)

到

stats.ttest_ind(a= std_promo.dollars, b= new_promo.dollars, equal_var= True)

我创建了一个与您类似的 DF,然后运行它并使用美元:

Ttest_indResult(statistic=7.144078895160622, pvalue=9.765848295636031e-05)

【讨论】:

  • 感谢您的回复。在这个问题中,我刚刚添加了 equal_var = True 并且它适用于上述代码。因为得到了与你不同的测试统计数据。
猜你喜欢
  • 2021-03-27
  • 2020-04-04
  • 2021-12-25
  • 1970-01-01
  • 1970-01-01
  • 2016-08-30
  • 1970-01-01
  • 2016-04-29
  • 2023-04-02
相关资源
最近更新 更多