forked from lawlite19/MachineLearning_Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.py
More file actions
73 lines (57 loc) · 2.77 KB
/
Copy pathclass.py
File metadata and controls
73 lines (57 loc) · 2.77 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
69
70
71
import numpy as np
from helpers import *
class LinearRegression():
def __init__(self, train_data, learning_rate=0.01,train_iter=100):
self.weights = np.zeros((len(train_data[1])-1, 1)) # by default, the input train_data has shape: [num_samples, num_features + 1], where 1 for labels
self.lr = learning_rate
self.train_iter = train_iter
def feat_normalize(self, data):
print(f"Normalizing...\n")
data_norm = np.array(data)
mu = np.mean(data, 0)
sigma = np.std(data, 0)
sigma[sigma==0] = 1.0 # 避免除以0
data_norm = (data_norm - mu)/sigma
return data_norm
# 将权重和偏差放到一个向量中,并相应地扩展数据矩阵
# data: [num_samples, num_feat] -> [num_samples, num_feat+1]
# weights: (num_weights, ) -> (num_wegits+1, )
def extend_matrix(self, normalized_data):
print(f"Extending Matrix...\n")
num_samples = normalized_data.shape[0]
x_extend = np.hstack((normalized_data, np.ones((num_samples,1))))
# print(f"shape of x_extend is: {x_extend.shape}\n")
self.weights = np.vstack((self.weights, np.ones((1,1))))
# print(f"shape of self.weights is: {self.weights.shape}\n")
print(f"Extending Done!\n")
return x_extend
# 平方误差和作为损失函数
def get_loss(self,x_extend, labels):
J = 0
J = 0.5 * np.transpose(x_extend @ self.weights - labels) @ (x_extend @ self.weights - labels)
print(f"current loss is: {J}\n")
return J
def compute_gradient(self, x_extend, labels):
print(f"Computing Gradient...\n")
n = len(self.weights)
J_history = np.zeros((self.train_iter, 1))
for i in range(self.train_iter):
grad = np.transpose(x_extend) @ (x_extend @ self.weights - labels)
self.weights = self.weights - self.lr*grad
J_history[i] = self.get_loss(x_extend, labels)
print(f"---\n{i}_th iterate step: \n weights are: {self.weights}\n loss is: {J_history[i]}\n ### \n ")
return J_history
# shape of data: [num_samples, num_features]
def train(self, data):
# pre-process data
x = self.feat_normalize(data[:,:-1])
x_extend = self.extend_matrix(x)
print(f"check the weight size again: {len(self.weights)}\n")
# labels = data[:,-1] 返回的是一维数组(num_samples, ),需要改成二维列向量
labels = data[:,-1].reshape(-1,1)
J_history = self.compute_gradient(x_extend, labels)
plotJ(J_history, self.train_iter)
if __name__ == "__main__":
data = np.loadtxt("data.txt", delimiter=",", dtype=np.float64)
linear_regression = LinearRegression(data)
linear_regression.train(data)