-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinear_Regression.py
More file actions
68 lines (46 loc) · 1.29 KB
/
Copy pathLinear_Regression.py
File metadata and controls
68 lines (46 loc) · 1.29 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#Linear_Regression
import matplotlib.pyplot as plt
import numpy as np
import sklearn
from sklearn import datasets
from sklearn.linear_model import LinearRegression
# load data
d = datasets.load_diabetes()
X = d.data[:, 2]
# X = (X - np.mean(X))/(X.max() - X.min())
Y = d.target
# Y = (Y - np.mean(Y))/(Y.max() - Y.min())
m = X.shape[0]
# print(X, Y)
# a = np.array([[1,2,3],[2,3,4]])
# print(a.sum())
# print(a.transpose())
# # draw original data
# plt.scatter(X, Y)
# plt.xlabel("X")
# plt.ylabel("Y")
# plt.show()
################ my program #################
S_X = X.sum()
S_Y = Y.sum()
S_XY = np.sum(X*Y)
S_XX = np.sum(X*X)
learning_rate = 0.5 #when learning rate is diffent, it may not be able to convergent or be too slow to convergent
n = 3000
a, b = 0, 0
for i in range(n):
a = a - learning_rate * 1/m*(a * S_XX + b * S_X - S_XY)
b = b - learning_rate * 1/m*(a * S_X + b * m - S_Y)
J = 0
for j in range(m):
J = J + 0.5/m * (a * X[j] + b - Y[j])**2
print('step %d: loss = %.6f a = %.3f b = %.3f' % (i+1, J, a ,b))
plt.figure('Compare')
plt.plot(X, a*X + b, label='my_prog')
######## sklearn methon #################
reg = LinearRegression().fit(X.reshape(-1, 1), Y)
a_ = reg.coef_
b_ = reg.intercept_
plt.plot(X, a_*X + b_, label='skelearn method')
plt.legend()
plt.show()