Python 2.7,z得分计算

时间:2015-06-23 16:49:36

标签: python python-2.7

我正在尝试计算文件中2行数字的z分数。我已经在读取模式下打开文件,对数据进行条带化并用逗号分隔数据。现在我想使用第3行和第4行来计算z得分。我用于z得分的公式:

Zi = (observed - expected)/ sqrt(expected)

我目前的代码:

import os
from math import sqrt

observed[]
expected[]
score = 0

for line in lines:
    row = line.split(',')
    observed.append(row[3].strip())
    expected.append(row[4].strip())
o = observed
e = expected
length_o = len(o)
length_e = len(e)
score = 0

for i in range(length_o,lenght_e):
    score += (o[i] - e[i])
    result = (score/(sqrt(e[i]))

1 个答案:

答案 0 :(得分:2)

您尚未正确实施for loop range。问题出在这里:

for i in range(length_o,lenght_e):
    score += (o[i] - e[i])
    result = (score/(sqrt(e[i]))

根据这个length_o == length_e。这个循环永远不会运行。

请参阅range Documentation

这应该有帮助

for i in range(length_o):
    score += (o[i] - e[i])
    result = (score/(sqrt(e[i]))