forked from Ryuk17/MachineLearning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinearRegression.py
230 lines (207 loc) · 8.47 KB
/
LinearRegression.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
"""
@ Filename: Regression.py
@ Author: Danc1elion
@ Create Date: 2019-05-05
@ Update Date: 2019-05-06
@ Description: Implement linear regression
"""
import numpy as np
import preProcess
import pickle
import random
import matplotlib.pyplot as plt
class Regression:
def __init__(self, norm_type="Normalization",regression_type="Standard", k=1.0, lamda=0.2, learning_rate=0.01, iterations=100):
self.norm_type = norm_type
self.regression_type = regression_type
self.k = k # parameter for local weight linear regression
self.lamda = lamda # parameter for ridge regression
self.learning_rate = learning_rate # parameter for forward step regression
self.iterations = iterations # parameter for forward step regression
self.w = None
self.parameters = None
self.prediction = None
self.probability = None
'''
Function: standardLinearRegression
Description: standard Linear Regression, w =(X.T*X)-1*X.T*y
Input: x dataType: ndarray description: x
y dataType: ndarray description: y
Output: w dataType: ndarray description: weights
'''
def standardLinearRegression(self, x, y):
if self.norm_type == "Standardization":
x = preProcess.Standardization(x)
else:
x = preProcess.Normalization(x)
xTx = np.dot(x.T, x)
if np.linalg.det(xTx) == 0: # calculate the Determinant of xTx
print("Error: Singluar Matrix !")
return
w = np.dot(np.linalg.inv(xTx), np.dot(x.T, y))
return w
'''
Function: LWLinearRegression
Description: locally weighted linear regression, w = (X.T*W*X)-1*X.T*W*y
Input: x dataType: ndarray description: x
y dataType: ndarray description: y
Output: w dataType: ndarray description: weights
'''
def LWLinearRegression(self, x, y, sample):
if self.norm_type == "Standardization":
x = preProcess.Standardization(x)
else:
x = preProcess.Normalization(x)
sample_num = len(x)
weights = np.eye(sample_num)
for i in range(sample_num):
diff = sample - x[i, :]
weights[i, i] = np.exp(np.dot(diff, diff.T)/(-2 * self.k ** 2))
xTx = np.dot(x.T, np.dot(weights, x))
if np.linalg.det(xTx) == 0:
print("Error: Singluar Matrix !")
return
result = np.dot(np.linalg.inv(xTx), np.dot(x.T, np.dot(weights, y)))
return result
'''
Function: ridgeRegression
Description: ridge linear regression, w = (X.T*X+ LAMDA I)-1*X.T*y
Input: x dataType: ndarray description: x
y dataType: ndarray description: y
Output: w dataType: ndarray description: weights
'''
def ridgeRegression(self, x, y):
if self.norm_type == "Standardization":
x = preProcess.Standardization(x)
else:
x = preProcess.Normalization(x)
feature_dim = len(x[0])
xTx = np.dot(x.T, x)
matrix = xTx + np.exp(feature_dim)*self.lamda
if np.linalg.det(xTx) == 0:
print("Error: Singluar Matrix !")
return
w = np.dot(np.linalg.inv(matrix), np.dot(x.T, y))
return w
'''
Function: lasso Regression
Description: lasso linear regression,
Input: x dataType: ndarray description: x
y dataType: ndarray description: y
Output: w dataType: ndarray description: weights
'''
def lassoRegression(self, x, y):
if self.norm_type == "Standardization":
x = preProcess.Standardization(x)
else:
x = preProcess.Normalization(x)
sample_num, feataure_dim = np.shape(x)
w = np.zeros([feataure_dim, 1])
for i in range(self.iterations):
last_w = w
w[i] = np.dot(x[i, :], (y[i] - x[i, :] * last_w.T))/np.dot(x[i, :], x[i, :].T)
return w
'''
Function: forwardstep Regression
Description: forward step linear regression,
Input: x dataType: ndarray description: x
y dataType: ndarray description: y
Output: w dataType: ndarray description: weights
'''
def forwardstepRegression(self, x, y):
if self.norm_type == "Standardization":
x = preProcess.Standardization(x)
else:
x = preProcess.Normalization(x)
sample_num, feature_dim = np.shape(x)
w = np.zeros([self.iterations, feature_dim])
best_w = np.zeros([feature_dim, 1])
for i in range(self.iterations):
min_error = np.inf
for j in range(feature_dim):
for sign in [-1, 1]:
temp_w = best_w
temp_w[j] += sign * self.learning_rate
y_hat = np.dot(x, temp_w)
error = ((y - y_hat) ** 2).sum() # MSE
if error < min_error: # save the best parameters
min_error = error
best_w = temp_w
w[i, :] = best_w.T
return w
'''
Function: train
Description: train the model
Input: train_data dataType: ndarray description: features
train_label dataType: ndarray description: labels
Output: self dataType: obj description: the trained model
'''
def train(self, train_data, train_label):
if self.norm_type == "Standardization":
train_data = preProcess.Standardization(train_data)
else:
train_data = preProcess.Normalization(train_data)
if self.regression_type == "Standard":
self.w = self.standardLinearRegression(train_data, train_label)
elif self.regression_type == "Localweight":
self.w = self.LWLinearRegression(train_data, train_label)
elif self.regression_type == "Ridge":
self.w = self.ridgeRegression(train_data, train_label)
elif self.regression_type == "Lasso":
self.w = self.lassoRegression(train_data, train_label)
elif self.regression_type == "Forwardstep":
self.w = self.forwardstepRegression(train_data, train_label)
else:
print("Error Regression Type!")
return self
'''
Function: predict
Description: predict the testing set
Input: test_data dataType: ndarray description: features
prob dataType: bool description: return probaility of label
Output: prediction dataType: ndarray description: the prediction results for testing set
'''
def predict(self, x, prob="False"):
# Normalization
if self.norm_type == "Standardization":
x = preProcess.Standardization(x)
else:
x = preProcess.Normalization(x)
y = np.dot(x, self.w)
self.prediction = y
return y
'''
Function: plot
Description: show regression result
Input: test_label dataType: ndarray description: labels of test data
Output: accuracy dataType: float description: detection accuarcy
'''
def plot(self, test_label):
# test_label = np.expand_dims(test_label, axis=1)
prediction = self.prediction
plot1 = plt.plot(test_label, 'r*', label='Regression values')
plot2 = plt.plot(prediction, 'b', label='Real values')
plt.xlabel('X ')
plt.ylabel('Y')
plt.legend(loc=3)
plt.title('Regression')
plt.show()
'''
Function: save
Description: save the model as pkl
Input: filename dataType: str description: the path to save model
'''
def save(self, filename):
f = open(filename, 'w')
pickle.dump(self.w, f)
f.close()
'''
Function: load
Description: load the model
Input: filename dataType: str description: the path to save model
Output: self dataType: obj description: the trained model
'''
def load(self, filename):
f = open(filename)
self.w = pickle.load(f)
return self