Tiffany Morris
Tiffany Morris

Reputation: 323

Python 2.7, z score calculation

I am trying to calculate the z score for 2 rows of numbers in a file. I have opened the file in read mode, striped the data and separated the data by commas. Now I would like to use row 3 and row 4 to calculate the z score. The formula I am using for z score:

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

My current code:

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]))

Upvotes: 1

Views: 772

Answers (1)

shaktimaan
shaktimaan

Reputation: 1857

You have not implemented for loop for range correctly. Problem lies here:

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

According to this Since length_o == length_e. This loop will never run.

See range Documentation

This should help

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

Upvotes: 2

Related Questions