"""
@author: jacksonkim
@filename: linear_regression.py
@start: 2021/02/01
@end: 2021/02/02
"""import numpy as np
import matplotlib.pyplot as plt
'''引入層級結構或高維對映而得。此外,線性模型有很好的可解釋性。
2. 線性回歸演算法是回歸任務中比較簡單的一種模型,其結構為 f(x) = wx + b
3. 本專案主要是學習自主實現線性回歸模型,也就是不調包的形式
'''class
linear
(object):
def__init__
(self, fit_intercept=
true
, solver=
'sgd'
, if_standard=
true
, epochs=
10, eta=1e-
2, batch_size=1)
:"""
:param fit_intercept: 是否訓練bias
:param solver:
:param if_standard:
:param epochs:
:param eta:
:param batch_size:
"""self.w =
none
self.fit_intercept = fit_intercept
self.solver = solver
self.if_standard = if_standard
if if_standard:
self.feature_mean =
none
self.feature_std =
none
self.epochs = epochs
self.eta = eta
self.batch_size = batch_size
definit_params
(self, n_features)
:"""
初始化引數
:param n_features:
:return:
"""self.w = np.random.random(size=
(n_features,1)
)def
_fit_closed_from_solution
(self, x, y)
:"""
直接求解閉式解
:param x:
:param y:
:return:
"""self.w = np.linalg.pinv(x)
.dot(y)
def_fit_sgd
(self, x, y)
:"""
隨機梯度下降求解
:param x:
:param y:
:return:
"""x_y = np.c_[x, y]
# 按batch_size更新w, b
for _ in
range
(self.epochs)
: np.random.shuffle(x_y)
for index in
range
(x_y.shape[0]
// self.batch_size)
: batch_x_y = x_y[self.batch_size * index:self.batch_size *
(index +1)
] batch_x = batch_x_y[:,
:-1]
batch_y = batch_x_y[:,
-1:]
dw =-2
* batch_x.t.dot(batch_y - batch_x.dot(self.w)
)/ self.batch_size
self.w = self.w - self.eta * dw
deffit(self, x, y)
:"""
訓練模型
:param x:
:param y:
:return:
"""# 是否歸一化feature
if self.if_standard:
self.feature_mean = np.mean(x, axis=0)
self.feature_std = np.std(x, axis=0)
+1e-8
x =(x - self.feature_mean)
/ self.feature_std
# 是否訓練bias
if self.fit_intercept:
x = np.c_[x, np.ones_like(y)
]# 初始化引數
self.init_params(x.shape[1]
)# 訓練模型
if self.solver ==
'closed_form'
: self._fit_closed_from_solution(x, y)
elif self.solver ==
'sgd'
: self._fit_sgd(x, y)
defget_params
(self)
:"""
輸出原始資料的係數
:return:
"""if self.fit_intercept:
w = self.w[:-
1]b = self.w[-1
]else
: w = self.w
b =0if self.if_standard:
w = w / self.feature_std.reshape(-1
,1) b = b - w.t.dot(self.feature_mean.reshape(-1
,1))
return w.reshape(-1
), b
defpredict
(self, x)
:"""
對資料進行**
:param x: ndarray格式資料 m x n
:return:
"""if self.if_standard:
x =(x - self.feature_mean)
/ self.feature_std
if self.fit_intercept:
x = np.c_[x, np.ones(shape=x.shape[0]
)]return x.dot(self.w)
defplot_fit_boundary
(self, x, y)
:"""
繪製擬合結果
:param x:
:param y:
:return:
"""plt.scatter(x[:,
0], y)
plt.plot(x[:,
0], self.predict(x)
,'r'
)# 測試
# 隨機產生資料集
x = np.linspace(0,
100,
100)
x = np.c_[x, np.ones(
100)
]w = np.array([3
,2])
y = x.dot(w)
x = x.astype(
'float'
)y = y.astype(
'float')x[
:,0]
+= np.random.normal(size=x[:,
0].shape)*3
y = y.reshape(
100,1)
lr = linear(solver=
'sgd'
)lr.fit(x[:,
:-1]
, y)
predict = lr.predict(x[:,
:-1]
)# 檢視w
print
('w'
, lr.get_params())
# 檢視標準差
print
(np.std(y - predict)
)# 視覺化結果
lr.plot_fit_boundary(x[:,
:-1]
, y)
plt.show(
)
線性最小二乘法
example f x 12 ax b 22 f x frac parallel ax b parallel 2 2 f x 2 1 a x b 22 最小化下式時x的值。首先計算 xf x at a x b ata x at b big down xf x a t ax b a tax a tb ...
機器學習 線性回歸的最小二乘法
一 問題闡述 緊接上篇,本文使用python語言測試該演算法。因此演算法原理不再贅述。上篇博文已經提及損失函式j 對其求關於 的偏導數並令其等於0,得 以下演算法便用到此式。計算過程涉及很多線性代數的定理,主要用到此式 其中tr表示矩陣的跡 trace 二 實現 from numpy import ...
機器學習之線性回歸的最小二乘法求解
機器學習之線性回歸的最小二乘法求解 假設現在乙個普通的一階線性方程,y 2 x 2 t。t是隨機噪音,生成的雜湊點 x,y 會沿直線y 2 x上下擺動。利用最小二乘法做一次簡單的一階 曲線 擬合。用matlab做資料實驗 t randn 1,101 x 10 0.2 10 y 2 x t 2 s s...