Least Square Regression Line | Function to calculate b ; sum of array x ; sum of array y ; for sum of product of x and y ; sum of square of x ; Function to find the least regression line ; Finding b ; Calculating a ; Printing regression line ; Statistical data
def calculateB ( x , y , n ) : NEW_LINE INDENT sx = sum ( x ) NEW_LINE sy = sum ( y ) NEW_LINE sxsy = 0 NEW_LINE sx2 = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sxsy += x [ i ] * y [ i ] NEW_LINE sx2 += x [ i ] * x [ i ] NEW_LINE DEDENT b = ( n * sxsy - sx * sy ) / ( n * sx2 - sx * sx ) NEW_LINE return b NEW_LINE DEDENT def leastRegLine ( X , Y , n ) : NEW_LINE INDENT b = calculateB ( X , Y , n ) NEW_LINE meanX = int ( sum ( X ) / n ) NEW_LINE meanY = int ( sum ( Y ) / n ) NEW_LINE a = meanY - b * meanX NEW_LINE print ( " Regression ▁ line : " ) NEW_LINE print ( " Y ▁ = ▁ " , ' % .3f ' % a , " ▁ + ▁ " , ' % .3f ' % b , " * X " , sep = " " ) NEW_LINE DEDENT X = [ 95 , 85 , 80 , 70 , 60 ] NEW_LINE Y = [ 90 , 80 , 70 , 65 , 60 ] NEW_LINE n = len ( X ) NEW_LINE leastRegLine ( X , Y , n ) NEW_LINE