Skip to content

Commit 7cac631

Browse files
author
Simon
committed
linear regression
1 parent 9c0ab0e commit 7cac631

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
def coefficient(x, y):
5+
n = np.size(x)
6+
mean_x, mean_y = np.mean(x), np.mean(y)
7+
cross_deviation_sum = np.sum(x * y) - n * mean_x * mean_y
8+
square_deviation_sum = np.sum(x * x) - n * mean_x * mean_x
9+
b_1 = cross_deviation_sum / square_deviation_sum
10+
b_0 = mean_y - b_1 * mean_x
11+
12+
return (b_0, b_1)
13+
14+
def plot_line( x, y, b):
15+
plt.scatter(x, y, color = "r")
16+
y_pred = b[0] + b[1] * x
17+
plt.plot( x, y_pred, color = "g")
18+
plt.xlabel('x')
19+
plt.ylabel('y')
20+
plt.show()
21+
22+
x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
23+
y = np.array([1, 2, 4, 5, 7, 8, 3, 6, 9, 10 ])
24+
b = coefficient( x, y)
25+
print("The coefficients are : {} and {}".format(b[0], b[1]))
26+
plot_line( x, y, b)

0 commit comments

Comments
 (0)