-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleLinearRegression.py
99 lines (73 loc) · 3.3 KB
/
SimpleLinearRegression.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
import numpy as np
from metrics import r2_score
class SimpleLinearRegression1:
def __init__(self):
'''初始化 Simple Linear Regression 模型'''
self.a_ = None
self.b_ = None
def fit(self, x_train, y_train):
'''根据训练数据集 x_train, y_train 训练 Simple Linear Regression 模型'''
assert x_train.ndim == 1, \
"Simple Linear Regressor can only solve single feature training data."
assert len(x_train) == len(y_train), \
"the size of x_train must be equal to the size of y_train."
x_mean = np.mean(x_train)
y_mean = np.mean(y_train)
num = 0.0
d = 0.0
for x, y in zip(x_train, y_train):
num += (x - x_mean) * (y - y_mean)
d += (x - x_mean) ** 2
self.a_ = num / d
self.b_ = y_mean - self.a_ * x_mean
return self
def predict(self, x_predict):
"""给定待测数据集 x_predict, 返回表示 x_predict 的结果向量"""
assert x_predict.ndim == 1, \
"Simple Linear Regressor can only solve single feature training data."
assert self.a_ is not None and self.b_ is not None, \
"must fit before predict!"
return np.array([self._predict(x) for x in x_predict])
def _predict(self, x_single):
"""给定单个待预测数据 x_single, 返回 x_single 的预测结果值"""
return self.a_ * x_single + self.b_
def score(self, x_test, y_test):
'''根据训练数据集 x_test, y_test 确定当前模型的准确度'''
y_predixt = self.predict(x_test)
return r2_score(y_test, y_predixt)
def __repr__(self):
return "SimpleLinearRegression1()"
class SimpleLinearRegression2:
def __init__(self):
'''初始化 Simple Linear Regression 模型'''
self.a_ = None
self.b_ = None
def fit(self, x_train, y_train):
'''根据训练数据集 x_train, y_train 训练 Simple Linear Regression 模型'''
assert x_train.ndim == 1, \
"Simple Linear Regressor can only solve single feature training data."
assert len(x_train) == len(y_train), \
"the size of x_train must be equal to the size of y_train."
x_mean = np.mean(x_train)
y_mean = np.mean(y_train)
num = (x_train - x_mean).dot(y_train - y_mean)
d = (x_train - x_mean).dot(x_train - x_mean)
self.a_ = num / d
self.b_ = y_mean - self.a_ * x_mean
return self
def predict(self, x_predict):
"""给定待测数据集 x_predict, 返回表示 x_predict 的结果向量"""
assert x_predict.ndim == 1, \
"Simple Linear Regressor can only solve single feature training data."
assert self.a_ is not None and self.b_ is not None, \
"must fit before predict!"
return np.array([self._predict(x) for x in x_predict])
def _predict(self, x_single):
"""给定单个待预测数据 x_single, 返回 x_single 的预测结果值"""
return self.a_ * x_single + self.b_
def score(self, x_test, y_test):
'''根据训练数据集 x_test, y_test 确定当前模型的准确度'''
y_predixt = self.predict(x_test)
return r2_score(y_test, y_predixt)
def __repr__(self):
return "SimpleLinearRegression2()"