列表索引超出范围-卡方检验

时间:2019-07-20 12:26:03

标签: python python-3.x random jupyter-notebook

enter code here我创建了LCG生成器,并且试图进行单数卡方检验。

我从生成器中选择100个随机数:

np.seterr(over='ignore')

a = np.uint32(1664525)
c = np.uint32(1013904223)
seed = np.uint32(1)

rng = LCG(seed, a, c)
q = [rng.next() for _ in range(0, 100)]
print(q)
data_set = q 

接下来,我尝试以这种方式进行卡方:

def chi_square_uniformity_test():

chi_sq_value = 0.0
num_samples = 10000
degrees_of_freedom = num_samples - 1
data_set
observed_val = 1 

expected_val = num_samples/10

for observed_val in data_set:

    chi_sq_value += ( pow((expected_val - data_set[observed_val]), 2)/expected_val )

return chi_sq_value

出现以下错误-

IndexError                                Traceback (most recent call last)
<ipython-input-43-675902c0a85e> in <module>
----> 1 chi_square_uniformity_test()

<ipython-input-42-3960c5593af3> in chi_square_uniformity_test()
     30     for observed_val in data_set:
     31         #print "Observed value is: " + observed_val
---> 32         chi_sq_value += ( pow((expected_val - data_set[observed_val]), 2)/expected_val )
     33 
     34     # Coming out of this loop, we'll have a chi-squared test statistic

IndexError: list index out of range

1 个答案:

答案 0 :(得分:1)

for observed_val in data_set:遍历data_set单个元素,而不是它们的索引,因此data_set[observed_val]尝试索引{{1 }}和 data_set 的元素之一,并且这样的索引可能不存在。

例如:

data_set

您根本不需要>>> for x in [1000]: ... print(x) # x == 1000 ... print([1000][x]) # index 1000 clearly doesn't exist ... 1000 Traceback (most recent call last): File "<stdin>", line 3, in <module> IndexError: list index out of range 。您可以这样做:

data_set[observed_val]