-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlinear_regression.py
More file actions
83 lines (61 loc) · 2.1 KB
/
linear_regression.py
File metadata and controls
83 lines (61 loc) · 2.1 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
72
73
74
75
76
77
78
79
80
81
82
83
# -*- coding: utf-8 -*-
# linear regression.
# I have completed the major framework, you can fill the space
# to make it work.
__author__ = "Zifeng Wang"
__email__ = "wangzf18@mails.tsinghua.edu.cn"
__date__ = "20190920"
import numpy as np
import pdb
import os
np.random.seed(2019)
class linear_regression:
def __init__(self):
self.mse_func = lambda x, y: 1 / x.shape[0] * np.sum((x - y) ** 2)
pass
def train(self, x_train, y_train):
"""Receive the input training data, then learn the model.
Inputs:
x_train: np.array, shape (num_samples, num_features)
y_train: np.array, shape (num_samples, )
Outputs:
None
"""
self.learning_rate = 0.00001
self.w = np.random.rand(10)
iteration = 10000
for i in range(iteration):
pred = self.predict(x_train)
"""
Please Fill Your Code Here.
"""
self.w = self.w + self.learning_rate * (y_train[i] - x_train[i].dot(self.w)) * x_train[i]
if i % 100 == 0:
print("Iteration {}/{}, MSE loss {:.4f}".format(i + 1, iteration, self.mse_func(pred, y_train)))
return
def predict(self, x_test):
"""Do prediction via the learned model.
Inputs:
x_test: np.array, shape (num_samples, num_features)
Outputs:
pred: np.array, shape (num_samples, )
"""
"""
Please Fill Your Code Here.
"""
pred = x_test.dot(self.w)
return pred
if __name__ == '__main__':
# "load" your data
real_weights = np.random.rand(10)
x_train = np.random.randn(10000, 10) * 10
y_train = x_train.dot(real_weights) + np.random.randn(10000)
# train and test your model
linear_regressor = linear_regression()
linear_regressor.train(x_train, y_train)
weights_error = np.sum((real_weights - linear_regressor.w) ** 2)
print('weights error: ', weights_error)
if weights_error < 1e-4:
print("Congratulations! Your linear regressor works!")
else:
print("Check your code! Sth went wrong.")