python线性回归算法简介_Python机器学习(二):线性回归算法
機(jī)器學(xué)習(xí)研究的問題分為分類問題和回歸問題。分類問題很好理解,而回歸問題就是找到一條曲線,可以最大程度地?cái)M合樣本特征和樣本輸出標(biāo)記之間的關(guān)系。當(dāng)給算法一個輸入時(shí),這條曲線可以計(jì)算出相應(yīng)可能的輸出。回歸算法最簡單的就是線性回歸。當(dāng)樣本特征只有一個時(shí),稱為簡單線性回歸;當(dāng)樣本特征有多個時(shí),稱為多元線性回歸。
線性回歸
1.簡單線性回歸
由上圖可知,簡單線性回歸只有一個特征x,一個標(biāo)記y。假定x和y之間具有類似于線性的關(guān)系,就可以使用使用簡單線性回歸算法。假定我們找到了最佳擬合的直線方程
最佳擬合的直線方程
則對于每一個樣本點(diǎn)x(i),預(yù)測值如下。其中帶箭頭的y是預(yù)測值,稱為 y head。右上角的 i 是指樣本的索引。
預(yù)測值
我們希望預(yù)測值和真實(shí)值之間的差距盡量小。一般用歐氏距離來衡量。下式稱為損失函數(shù)(Loss Function)
損失函數(shù)
換句話說,我們的目標(biāo)就是找到一組a和b,使得下式最小
y(i)和x(i)是固定的
通過分析不同的問題,我們需要確定問題的損失函數(shù)。通過最優(yōu)化損失函數(shù),獲得機(jī)器學(xué)習(xí)的模型。幾乎所有的參數(shù)學(xué)習(xí)算法都是這樣的套路
那么這個問題是一個典型的最小二乘法問題,即最小化誤差的平方。推導(dǎo)可得以下公式
最小二乘法
可以用python封裝成這種形式
"""
Created by 楊幫杰 on 10/1/18
Right to use this code in any way you want without
warranty, support or any guarantee of it working
E-mail: yangbangjie1998@qq.com
Association: SCAU 華南農(nóng)業(yè)大學(xué)
"""
import numpy as np
class SimpleLinearRegression:
def __init__(self):
"""初始化Simple Linear Regression 模型"""
self.a_ = None
self.b_ = None
def fit(self, x_train, y_train):
"""根據(jù)訓(xùn)練數(shù)據(jù)集x_train,y_train訓(xùn)練Simple Linear Regression 模型"""
assert x_train.nidm == 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)
"""進(jìn)行向量化可以加快訓(xùn)練速度"""
# 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
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):
"""給定待預(yù)測數(shù)據(jù)集x_predict, 返回表示x_predict的結(jié)果向量"""
assert x_predict.ndim == 1, \
"Simeple 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):
"""給定單個待預(yù)測數(shù)據(jù)x_single, 返回x_single的預(yù)測結(jié)果值"""
return self.a_ * x_single + self.b_
def __repr__(self):
return "SimpleLinearRegression()"
衡量線性回歸模型好壞有多個標(biāo)準(zhǔn),均方誤差(Mean Squared Error)、均方根誤差(Root Mean Squared Error)、平均絕對誤差(Mean Absolute Error)等。一般使用MSE。
均方誤差MSE
均方根誤差RMSE
平均絕對誤差MAE
而如果想像分類問題一樣將評判得分限制在0和1之間,則應(yīng)該使用R Square
R Square
右邊一項(xiàng)的分子代表使用模型產(chǎn)生的錯誤,分母代表使用平均值進(jìn)行預(yù)測產(chǎn)生的錯誤。分母也可以理解為一個模型,稱為Baseline Model。
R Square的輸出分為以下幾種情況:
R^2 = 1,則模型不犯任何錯誤,完美
R^2 = 0,模型為基準(zhǔn)模型,相當(dāng)于沒訓(xùn)練過
R^2 < 0,數(shù)據(jù)可能不存在任何線性關(guān)系
2.多元線性回歸
多元線性回歸,就是指樣本特征值有多個。根據(jù)這多個特征值來預(yù)測樣本的標(biāo)記值。那么特征X和參數(shù)Θ就是一個向量。
多元線性回歸
相類似地,我們需要找到一個損失函數(shù)。我們需要找到一組參數(shù)Θ,使下式盡可能小
損失函數(shù)
預(yù)測值有n個參數(shù)
為了方便進(jìn)行矩陣運(yùn)算,我們寫成這種形式
X0不是特征輸入!
預(yù)測值可以寫成這種形式
預(yù)測值和參數(shù)是n維向量,X是n維矩陣
X展開是這個樣子。每一行是一個樣本點(diǎn),每一列(除了第一列)是一種特征
展開
經(jīng)過推導(dǎo),得到這樣一個公式。這成為多元線性回歸的正規(guī)方程解(Normal Equation)。結(jié)果就是參數(shù)向量。
我也不知道怎么來的
Θ0就是簡單線性回歸中的b
如上,可以封裝成這種形式
"""
Created by 楊幫杰 on 10/1/18
Right to use this code in any way you want without
warranty, support or any guarantee of it working
E-mail: yangbangjie1998@qq.com
Association: SCAU 華南農(nóng)業(yè)大學(xué)
"""
import numpy as np
class LinearRegression:
def __init__(self):
"""初始化Linear Regression模型"""
self.coef_ = None
self.interception_ = None
self._theta = None
def fit_normal(self, X_train, y_train):
"""根據(jù)訓(xùn)練數(shù)據(jù)集X_train, y_train訓(xùn)練Linear Regression模型"""
assert X_train.shape[0] == y_train.shape[0], \
"the size of X_train must be equal to the size of y_train"
X_b = np.hstack([np.ones((len(X_train), 1)), X_train])
self._theta = np.linalg.inv(X_b.T.dot(X_b)).dot(X_b.T).dot(y_train)
self.interception_ = self._theta[0]
self.coef_ = self._theta[1:]
return self
def predict(self, X_predict):
"""給定待預(yù)測數(shù)據(jù)集X_predict, 返回表示X_predict的結(jié)果向量"""
assert self.interception_ is not None and self.coef_ is not None, \
"must fit before predict!"
assert X_predict.shape[1] == len(self.coef_), \
"the feature number of X_predict must be equal to X_train"
X_b = np.hstack([np.ones((len(X_predict), 1)), X_predict])
return X_b.dot(self._theta)
def __repr__(self):
return "LinearRegression()"
sciki-learn中使用線性回歸如下
"""
Created by 楊幫杰 on 10/1/18
Right to use this code in any way you want without
warranty, support or any guarantee of it working
E-mail: yangbangjie1998@qq.com
Association: SCAU 華南農(nóng)業(yè)大學(xué)
"""
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
# 加載波士頓房價(jià)的數(shù)據(jù)集
boston = datasets.load_boston()
# 清除一些不合理的數(shù)據(jù)
X = boston.data
y = boston.target
X = X[y < 50.0]
y = y[y < 50.0]
# 分離出測試集并擬合
X_train, X_test, y_train, y_test = train_test_split(X, y)
lin_reg = LinearRegression()
lin_reg.fit(X_train, y_train)
# 打印結(jié)果
print(lin_reg.coef_)
print(lin_reg.intercept_)
print(lin_reg.score(X_test, y_test))
輸出如下
打印結(jié)果
3.總結(jié)
線性回歸是許多其他回歸和分類問題的基礎(chǔ)。
它最大的優(yōu)點(diǎn)是對數(shù)據(jù)具有很強(qiáng)的解釋性。比如某一項(xiàng)的參數(shù)是正數(shù),那么很可能這個特征和樣本標(biāo)記之間成正相關(guān),反之成負(fù)相關(guān)。
優(yōu)點(diǎn):
思想簡單,實(shí)現(xiàn)容易
是許多非線性模型的基礎(chǔ)
具有很好的可解釋性
缺點(diǎn):
假設(shè)特征和標(biāo)記之間有線性關(guān)系,現(xiàn)實(shí)中不一定
訓(xùn)練的時(shí)間復(fù)雜度比較高
References:
Python3 入門機(jī)器學(xué)習(xí) 經(jīng)典算法與應(yīng)用 —— liuyubobobo
機(jī)器學(xué)習(xí)實(shí)戰(zhàn) —— Peter Harrington
總結(jié)
以上是生活随笔為你收集整理的python线性回归算法简介_Python机器学习(二):线性回归算法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python俄罗斯方块代码34行_少儿编
- 下一篇: websocket python爬虫_p