A user is using linear regression with the below code and got the following error

324    Asked by ranjan_6399 in Data Science , Asked on Jan 15, 2020
Answered by Ranjana Admin

from math import *

f=open("data_setshort.csv", "r")

data = f.readlines()

f.close()

xvalues=[]; yvalues=[]

    for line in data:

    x,y=line.strip().split(",")

    x=float(x.strip())

    y=float(y.strip())

    xvalues.append(x)

    yvalues.append(y)

def regression(x,y):

    n = len(x)

    X = sum(x)

    Y = sum(y)

    for i in x:

        A = sum(i**2)

        return A

    for i in x:

        for j in y:

            C = sum(x*y)

        return C

    return C

    D = (X**2)-nA

    m = (XY - nC)/D

    b = (CX - AY)/D

    return m,b

print "xvalues:", xvalues

print "yvalues:", yvalues

regression(xvalues,yvalues)

 The error is given below

line 23, in regression, A = sum (I**2). TypeError: 'float' object is not iterable.

We can't sum over a single float, but we can sum over lists. A = sum([xi**2 for xi in x]) to calculate Sum of each element in x to the power of 2.The corrected code is given below

def regression(x,y):

    n = len(x)

    X = sum(x)

    Y = sum(y)

    A = sum([xi**2 for xi in x])

    C = sum([xi*yi for xi, yi in zip(x,y)])

    D = X**2 - n*A

    m = (X*Y - n*C) / float(D)

    b = (C*X - A*Y) / float(D)

    return (m, b)



Your Answer

Interviews

Parent Categories