線性回歸模型
概述
? 機器學習整體大致可以分為兩大類,分別就是回歸與分類,線性回歸和邏輯回歸是兩種最基本的模型,其它模型也大致是從這兩類中演變來的,所以這兩個模型算是機器學習的入門模型,雖然sklearn庫中都有直接的代碼可以呼叫,但是理解其原理,并自己手動實作還是很有必要的,
模型概述
? 所謂線性回歸就是用一條直線去擬合資料,回歸兩字來自于遺傳學,意思是回到平均水平,其簡單形式為y = wx + b,我們要做的就是尋找到最合適的w和b,讓預測準與真實值之間的誤差最小,要想找到最合適的w和b,有兩種方法,一種就是最小二乘法,一種就是梯度下降法,
? 有一說一,公式太難打了,主要公式如下面圖片所示:

python代碼實作
class Linear_Regression:
"""
實作簡單線性回歸
"""
def __init__(self):
pass
def train_gradient_descent(self, X, y, learning_rate=0.01,iters=100):
"""
用梯度下降法求引數
----------
X : 自變數資料,形狀為(m,n),m:資料的個數,n:自變數(或者特征)的個數
y : 因變數資料,形狀為(m,1)
learning_rate : 學習速率,默認為0.01
iters : 迭代次數,默認為100
----------
"""
n_samples = X.shape[0]
n_features = X.shape[1]
#初始化引數
self.w = np.zeros(shape=(n_features,1))
self.b = 0
costs = []
for i in range(iters):
# 計算預測值
y_predict = np.dot(X, self.w) + self.b
# 計算損失函式
cost = np.sum((y_predict - y)**2) / (2*n_samples)
costs.append(cost)
if i % 100 == 0:
print(f"Cost at iteration {i}: {cost}")
# 計算梯度
dw = (1 / n_samples) * np.dot(X.T, (y_predict - y))
db = (1 / n_samples) * np.sum((y_predict - y))
# 更新梯度
self.w = self.w - learning_rate * dw
self.b = self.b - learning_rate * db
return self.w, self.b, costs
def train_normal_equation(self, X, y):
"""
最小二乘法,方程式法計算
"""
X = np.column_stack((np.ones(X.shape[0]),X))
X = np.matrix(X)
y = np.matrix(y)
coef = (X.T*X).I*X.T*y
self.w = coef[0]
self.b= coef[1:]
return self.w, self.b
def predict(self, X):
return np.dot(X, self.w) + self.b
簡單實體驗證
生成資料
生成資料代碼如下:
X = np.random.rand(500,3) #生成形狀為(500,3)的資料
coef = np.array([2,0.2,1.5]) #生成斜率項
y = 5 + np.dot(X,coef) #計算因變數值,形狀為(500,)
y = y[:,np.newaxis] #改變y值形狀為(500,1)
梯度下降法計算
運行如下代碼:
linear_regression = Linear_Regression()
grd = linear_regression.train_gradient_descent(X,y,learning_rate=0.1,iters=1000)
print("-"*50)
print(f"斜率項為:\n{grd[0]}")
print(f"截距項為:\n{grd[1]}")
print("-"*50)
代碼運行結果如下:
Cost at iteration 0: 23.908020401267393
Cost at iteration 100: 0.042615847946421086
Cost at iteration 200: 0.014364230409000197
Cost at iteration 300: 0.005176592296081314
Cost at iteration 400: 0.0019494499429811344
Cost at iteration 500: 0.0007538294158717608
Cost at iteration 600: 0.00029592278743143565
Cost at iteration 700: 0.00011713723139089097
Cost at iteration 800: 4.657686237953135e-05
Cost at iteration 900: 1.856514407834283e-05
--------------------------------------------------
斜率項為:
[[2.00696415]
[0.20748925]
[1.50849497]]
截距項為:
4.987877313714786
--------------------------------------------------
最小二乘法計算
運行如下代碼:
ols = Linear_Regression().train_normal_equation(X,y)
print('-'*50)
print(f"斜率項為:\n{ols[1]}")
print(f"截距項為:\n{ols[0]}")
print('-'*50)
代碼運行結果如下:
--------------------------------------------------
斜率項為:
[[2. ]
[0.2]
[1.5]]
截距項為:
[[5.]]
--------------------------------------------------
結語
? 基于最小二乘法改進的模型還有很多,如嶺回歸、Lasso回歸、elasticnet回歸等,其中嶺回歸用的是L2正則化,Lasso回歸用的是L1正則化,elasticnet回歸是兩者結合,
? 希望本文對大家有所幫助,有空再寫一下邏輯回歸的內容,謝謝大家,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/26540.html
標籤:其他
下一篇:求滿足條件的最長字串的長度
