You are viewing a single comment's thread. Return to all comments →
HERE'S MY CODE def mean(x): '''calculating mean of the list''' n = len(x) sum_x = sum(x) return sum_x/n
def squared_num(x): '''squaring each item in the list and redefine a new list''' lst = [] for i in x: lst.append(i**2) return lst
def mul_of_xy(x,y): '''multiplying corresponding items of each list''' gst = [] for i in range(len(x)): gst.append(x[i] * y[i]) return gst
def karl_pearson_corr(x,y): n= len(x) mean_x = mean(x) mean_y = mean(y) var_x = (((1/n)*sum(squared_num(x)))-(mean_x**2)) var_y = (((1/n)*sum(squared_num(y)))-(mean_y**2)) cov_xy = ((1/n)*sum(mul_of_xy(x,y)))-(mean_x * mean_y) num = cov_xy den = (var_x * var_y)**(1/2) r_xy = num/den return r_xy
x = [15,12,8,8,7,7,7,6,5,3] y = [10,25,17,11,13,17,20,13,9,15]
print(karl_pearson_corr(x,y))
Seems like cookies are disabled on this browser, please enable them to open this website
Correlation and Regression Lines - A Quick Recap #1
You are viewing a single comment's thread. Return to all comments →
HERE'S MY CODE def mean(x): '''calculating mean of the list''' n = len(x) sum_x = sum(x) return sum_x/n
defining x square
def squared_num(x): '''squaring each item in the list and redefine a new list''' lst = [] for i in x: lst.append(i**2) return lst
def mul_of_xy(x,y): '''multiplying corresponding items of each list''' gst = [] for i in range(len(x)): gst.append(x[i] * y[i]) return gst
def karl_pearson_corr(x,y): n= len(x) mean_x = mean(x) mean_y = mean(y) var_x = (((1/n)*sum(squared_num(x)))-(mean_x**2)) var_y = (((1/n)*sum(squared_num(y)))-(mean_y**2)) cov_xy = ((1/n)*sum(mul_of_xy(x,y)))-(mean_x * mean_y) num = cov_xy den = (var_x * var_y)**(1/2) r_xy = num/den return r_xy
x = [15,12,8,8,7,7,7,6,5,3] y = [10,25,17,11,13,17,20,13,9,15]
print(karl_pearson_corr(x,y))