python中的Chi square和t测试错误

时间:2016-03-27 18:34:00

标签: python python-2.7 numpy scipy

我需要处理一些数据并编写这些简短的python脚本。但是,当我尝试运行它们时,我收到此错误:SyntaxError:无法分配给运算符。这是卡方检验和t检验的代码。

卡方检验:

import scipy.stats as stats
a = [5851, 72007]
b = [6927, 70802]
c = [5915, 71729]
d = [5660, 71491]
obs = [a, b, c, d]
chi2, p-value, dof, expected = stats.chi2_contingency(obs)
print 'p-value =', p-value

t test:

import scipy.stats as stats
a = [625, 480, 621, 633]
b = [647, 503, 559, 586]
t-statistic, p-value = stats.ttest_ind(a,b)
print 'p-value =', p-value

2 个答案:

答案 0 :(得分:1)

问题是-中的p-value会让解释器认为那里有一个运算符-。将其更改为p_value以避免此错误。

答案 1 :(得分:0)

除了使用','的其他答案外,对两个变量的赋值不起作用。

t_statistic, p_value = stats.ttest_ind(a,b)会导致错误。为防止这种使用:

t_statistic = p_value = stats.ttest_ind(a,b)代替。