Skip to content

Instantly share code, notes, and snippets.

@Manikant92
Last active September 5, 2018 04:35
Show Gist options
  • Save Manikant92/3a6f814c0edde77bdfc8f71a55eaf5ff to your computer and use it in GitHub Desktop.
Save Manikant92/3a6f814c0edde77bdfc8f71a55eaf5ff to your computer and use it in GitHub Desktop.
#new input data which is represented in excel
new_x = [2,3,4,5,6,7,8,9,10,11]
#new output data which is represented in excel
new_y = [10,12,20,22,21,25,30,21,32,34]
#plot the graph
plt.scatter(new_x, new_y)
plt.ylabel('New Dependent Variable')
plt.xlabel('New Independent Variable')
plt.show()
#see plot Input and Output Scatter Plot before Best Fit
#calculating the same formula of OLS in code
# Mean X and Y
mean_x = np.mean(new_x)
mean_y = np.mean(new_y)
# Total number of values
m = len(new_x)
# Using the formula to calculate m and b
numer = 0
denom = 0
for i in range(m):
numer += (new_x[i] - mean_x) * (new_y[i] - mean_y)
denom += (new_x[i] - mean_x) ** 2
m = numer / denom
b = mean_y - (m * mean_x)
print('Slope: ',m)
print('Intercept: ',b)
#output: Slope: 2.35757575758
# Intercept: 7.37575757576
#output is same as which we had calculated manually.
regression_line = [(m*i)+b for i in new_x]
plt.scatter(new_x,new_y,color='blue')
plt.plot(new_x,regression_line, color='red')
plt.ylabel('Dependent/Output Variable')
plt.xlabel('Independent/Input Variable')
plt.title('Regression Line fit')
plt.show()
#see plot of Regression Line on Input and Output after Best fit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment