Skip to content

Commit 4f139bb

Browse files
authored
Merge pull request #116 from adi271001/regression
Regression Techniques SImple Linear Regression
2 parents eaae319 + 46b945e commit 4f139bb

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
def estimate_coef(x, y):
5+
# number of observations/points
6+
n = np.size(x)
7+
8+
# mean of x and y vector
9+
m_x = np.mean(x)
10+
m_y = np.mean(y)
11+
12+
# calculating cross-deviation and deviation about x
13+
SS_xy = np.sum(y*x) - n*m_y*m_x
14+
SS_xx = np.sum(x*x) - n*m_x*m_x
15+
16+
# calculating regression coefficients
17+
b_1 = SS_xy / SS_xx
18+
b_0 = m_y - b_1*m_x
19+
20+
return (b_0, b_1)
21+
22+
def plot_regression_line(x, y, b):
23+
# plotting the actual points as scatter plot
24+
plt.scatter(x, y, color = "m",
25+
marker = "o", s = 30)
26+
27+
# predicted response vector
28+
y_pred = b[0] + b[1]*x
29+
30+
# plotting the regression line
31+
plt.plot(x, y_pred, color = "g")
32+
33+
# putting labels
34+
plt.xlabel('x')
35+
plt.ylabel('y')
36+
37+
# function to show plot
38+
plt.show()
39+
40+
def main():
41+
# observations / data
42+
x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
43+
y = np.array([1, 3, 2, 5, 7, 8, 8, 9, 10, 12])
44+
45+
# estimating coefficients
46+
b = estimate_coef(x, y)
47+
print("Estimated coefficients:\nb_0 = {} \
48+
\nb_1 = {}".format(b[0], b[1]))
49+
50+
# plotting regression line
51+
plot_regression_line(x, y, b)
52+
53+
if __name__ == "__main__":
54+
main()

0 commit comments

Comments
 (0)