How to make a python code for this def linearcorrelationcoe
How to make a python code for this : def linear_correlation_coefficient(x_values,y_values): \'\'\'Calculate the linear correlation coefficient for the given sample, round your answer to 3 decimal places. The input is given as paired lists, one containing all the x values, one containing all the y values. \'\'\'
Solution
Code:
import math
def listsum(numList):
theSum = 0
for i in numList:
theSum = theSum + i
return theSum
def matrix_multiplication(a,b):
ab = [a[i]*b[i] for i in range(len(a))]
return ab
def linear_correlation_coeffecient(a,b):
n = len(a)
A = n*listsum(matrix_multiplication(a,b)) - listsum(a)*listsum(b)
B = (math.sqrt((n*listsum(matrix_multiplication(a,a)))-(listsum(a))**2))*(math.sqrt((n*listsum(matrix_multiplication(b,b)))-(listsum(b))**2))
r = A/B
return r
a = [10,9,6,8,6,12]
b = [11,5,6,3,6,8]
print(\"Linear Correlation Coeffecient is : %.3f\"%linear_correlation_coeffecient(a,b))
Output:
