• + 0 comments

    I am pretty sure this is correct but stdout is printing 'None' after I print out the coefficients. Additionally, computing statistics and machine learning related problems from stdin is silly. You simply do not manipulate data in this way in this kind of work, I do this type of work everyday; every question related to these topics should come with datasets that you load; not where you load each line from stdin or iterate over stdin, it is bizarre and irrelevant to this kind of work.

    *edit removing the print statements fixes the problem.

    import math
    N = int(raw_input())
    
    grades = []
    for _ in range(N):
        grades.append([int(b) for b in raw_input().split('\t')])
    
    m = [int(b[0]) for b in grades] 
    p = [int(b[1]) for b in grades]
    c = [int(b[2]) for b in grades]
    
    def pearson(A, B, N):
        sumA = sum(A)
        sumB = sum(B)
        AB = sum([x*y for x,y in zip(A, B)])
        A_sq = sum([x**2 for x in A])
        B_sq = sum([x**2 for x in B])
        numerator = (N*(AB) - sumA*sumB) 
        denominator =  (math.sqrt((N*A_sq - sumA**2) * (N*B_sq - sumB**2)))
        print round(numerator/denominator, 2)
        
    co1 = pearson(m, p, N)
    co2 = pearson(p, c, N)
    co3 = pearson(c, m, N)
    
    print co1
    print co2
    print co3