Python 列表索引超出范围-卡方检验
Python 列表索引超出范围-卡方检验,python,python-3.x,random,jupyter-notebook,Python,Python 3.x,Random,Jupyter Notebook,在此处输入代码我创建了LCG生成器,并尝试对unimormity进行卡方检验 从生成器中,我选择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 接下来,我尝试用这种方
在此处输入代码
我创建了LCG生成器,并尝试对unimormity进行卡方检验
从生成器中,我选择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卡方均匀性试验()
卡方均匀性检验()
30对于数据集中的观测值:
31#打印“观察值为:“+观察值”
--->32 chi_sq_值+=(pow((预期值-数据集[观察值]),2)/预期值)
33
34#从这个循环中,我们将得到卡方检验统计数据
索引器:列表索引超出范围
对于数据集中的观察值:
迭代数据集
的各个元素,而不是它们的索引,因此数据集[观察值]
尝试使用数据集
的一个元素对数据集
进行索引,而该索引可能不存在
例如:
>>> 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
chi_sq_value += ( pow((expected_val - observed_val), 2)/expected_val )