簡單線性迴歸演算法
阿新 • • 發佈:2018-11-27
一、目標
尋找一條直線,最大程度的“擬合”樣本特徵和樣本輸出標記之間的關係。在迴歸問題中我們預測的是一個具體的數值,這個具體的數值是在一個連續的空間裡的,如果想看兩個特徵的迴歸問題就需要在三維空間裡進行觀察。樣本特徵有多個的迴歸稱為多元線性迴歸
損失函式
對a求偏導數:
最後得到的結果:
求a、b的Python程式碼:
封裝SampleLinearRegression演算法的程式碼實現
"""coding:utf-8""" import numpy as np class SimpleLinearRegression(object): 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): """給定單個待預測資料x,返回x的預測結果值""" return self.a_ * x +self.b_ def __repr__(self): return "SimpleLinearRegression1()"
檢驗封裝演算法的測試程式碼
"""coding:utf-8""" import numpy as np import matplotlib.pyplot as plt X = np.array([1.,2.,3.,4.,5.]) y = np.array([1.,3.,2.,3.,5.]) plt.scatter(X,y) plt.axis([0,6,0,6]) plt.show() from play_ML.SimpleLinearRrgression import SimpleLinearRegression slr = SimpleLinearRegression() slr.fit(X,y) y_hat = slr.predict(X) plt.scatter(X,y) plt.plot(X,y_hat,color="r") plt.axis([0,6,0,6]) plt.show()
測試結果