From b5781f5d9e1513e36f0e9f2c98f8bf3620070a80 Mon Sep 17 00:00:00 2001 From: Ramesh Chandra Date: Fri, 3 Mar 2017 11:59:39 +0530 Subject: [PATCH] Correlation and Regression Lines - A quick recap#3 Solution Hackerrank --- ...and Regression Lines - A quick recap #3.py | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Correlation and Regression Lines - A quick recap #3.py diff --git a/Correlation and Regression Lines - A quick recap #3.py b/Correlation and Regression Lines - A quick recap #3.py new file mode 100644 index 0000000..bcf33af --- /dev/null +++ b/Correlation and Regression Lines - A quick recap #3.py @@ -0,0 +1,44 @@ + +#RAMESH CHANDRA + +import math +x = [15 , 12 , 8 , 8 , 7 , 7 , 7 , 6 , 5 , 3] +y = [10 ,25 , 17 ,11 , 13 , 17 , 20 , 13 , 9 , 15] +#calculate mean of x and y +sum_x,sum_y=0,0 +for i in xrange(len(x)): + sum_x,sum_y=sum_x+x[i],sum_y+y[i] + +mean_x = float(sum_x)/len(x) +mean_y = float(sum_y)/len(y) +#now calculate r = A/sqrt(B*C) +# A = sum ((x[i]-x_mean_x)*(y[i]-mean_y)) +# B = sum(x[i]-mean_x)^2 +# C = sum(y[i]-mean_y)^2 + +A, B , C ,= 0,0,0; +for i in xrange(len(x)): + A += (x[i]-mean_x)*(y[i]-mean_y) + B += (x[i]-mean_x)**2 + C += (y[i]-mean_y)**2 +#Co-relation Coefficient +r = float(A)/math.sqrt(B*C) +#slope b = r*(sd_x/sd_y) +#s_d and s_y are standard deviations of x and y respectively +std_x = math.sqrt( (float(B)/(len(x)))) +std_y = math.sqrt( (float(C)/(len(y)))) +#final slop +b = float(r*std_y)/std_x + +#regression line +# y = a+ bx + +# a = mean_y - b*mean_x +a = mean_y - b*mean_x + +#regression line +# y = a+ bx +ans = a+b*10 +print("%.1f"%ans) + +#for more info....go to this link https://www.youtube.com/watch?v=GhrxgbQnEEU