-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfirst tutorial.py
52 lines (39 loc) · 1.1 KB
/
first tutorial.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import numpy as np
import matplotlib.pyplot as plt
def first():
x = 2 * np.random.rand(100, 1)
y = 4 + 3 * x + np.random.randn(100, 1)
plt.scatter(x, y)
plt.xlabel("x1")
plt.ylabel("Y")
plt.show()
return x, y
# x_b (100, 2) (2, 100)
# (100, 100) (100, 2) --> (2, 100) (100, 1) --> (2, 1)
# y (100, 1)
def second(x, y):
x_b = np.c_[np.ones((100, 1)), x]
print(y.shape)
best_theta = (np.linalg.inv(x_b.dot(x_b.T)).dot(x_b)).T.dot(y)
print(best_theta)
return best_theta
def predictor(best_theta):
x_new = np.array([[0], [2]])
x_new_b = np.c_[np.ones((2, 1)), x_new] # adds xθ to each instance
y_predict = x_new_b.dot(best_theta)
print(y_predict)
return x_new, y_predict
def plotter(x, y, x_new, y_predict):
plt.plot(x_new, y_predict, "r-")
plt.plot(x, y, "b")
plt.axis([0, 2, 0, 15])
plt.show()
def shut_down():
import shutdown
shutdown.shutdown()
if __name__ == '__main__':
x, y = first()
# print(x, )
best_theta = second(x, y)
x_new, y_predict = predictor(best_theta)
plotter(x, y, x_new, y_predict)