機器學習 線性回歸( 簡單線性回歸的實現)

2021-09-29 07:08:39 字數 3513 閱讀 7418

【案例】給定一組資料(1, 1) , (2, 3) , (3, 2) , (4, 3) , (5, 5)。試通過簡單線性回歸求出擬合的直線,並能根據擬合的直線計算給定資料集的**值。

(1)繪製出影象。

(2)根據公式計算a和b的值。

(3)將要**的資料代入到擬合直線的方程進行計算。

紅色直線即我們通過簡單線性回歸得到的擬合函式。

將簡單線性回歸編寫為乙個類,在類中包含初始化回歸模型,訓練回歸模型和**等方法。

(1)初始化簡單線性回歸模型

# 初始化簡單線性回歸模型

def __init__(self):

# 使用a_,b_表明兩個變數不是使用者傳入的引數,而是要計算的結果

self.a_ = none

self.b_ = none

(2)訓練回歸模型

# 根據訓練資料集x_train,y_train訓練簡單線性回歸模型

def fit(self, x_train, y_train):

assert x_train.ndim == 1, \

"簡單線性回歸僅能解決單特徵訓練資料!"

assert len(x_train) == len(y_train), \

"訓練集x_train和y_train的大小必須一致!"

x_mean = np.mean(x_train)

y_mean = np.mean(y_train)

numerator = 0.0 # 分子

denominator = 0.0 # 分母

for x_i, y_i in zip(x_train, y_train):

numerator += (x_i - x_mean) * (y_i - y_mean)

denominator += (x_i - x_mean) ** 2

self.a_ = numerator / denominator

self.b_ = y_mean - self.a_ * x_mean

return self

(3)**函式:

# 給定待**資料集x_predict

def predict(self, x_predict):

assert x_predict.ndim == 1, \

"簡單線性回歸僅能解決單特徵訓練資料!"

assert self.a_ is not none and self.b_ is not none, \

"必須給出擬合直線的引數!"

return np.array([self._predict(x) for x in x_predict])

# 給定單個資料

def _predict(self, x_single):

return self.a_ * x_single + self.b_

(4)完整的**:

import numpy as np

class ******linearregression1:

# 初始化簡單線性回歸模型

def __init__(self):

# 使用a_,b_表明兩個變數不是使用者傳入的引數,而是要計算的結果

self.a_ = none

self.b_ = none

# 根據訓練資料集x_train,y_train訓練簡單線性回歸模型

def fit(self, x_train, y_train):

assert x_train.ndim == 1, \

"簡單線性回歸僅能解決單特徵訓練資料!"

assert len(x_train) == len(y_train), \

"訓練集x_train和y_train的大小必須一致!"

x_mean = np.mean(x_train)

y_mean = np.mean(y_train)

numerator = 0.0 # 分子

denominator = 0.0 # 分母

for x_i, y_i in zip(x_train, y_train):

numerator += (x_i - x_mean) * (y_i - y_mean)

denominator += (x_i - x_mean) ** 2

self.a_ = numerator / denominator

self.b_ = y_mean - self.a_ * x_mean

return self

# 給定待**資料集x_predict

def predict(self, x_predict):

assert x_predict.ndim == 1, \

"簡單線性回歸僅能解決單特徵訓練資料!"

assert self.a_ is not none and self.b_ is not none, \

"必須給出擬合直線的引數!"

return np.array([self._predict(x) for x in x_predict])

# 給定單個資料

def _predict(self, x_single):

return self.a_ * x_single + self.b_

if __name__ == "__main__":

regression1 = ******linearregression1()

x = np.array([1., 2., 3., 4., 5.])

y = np.array([1., 3., 2., 3., 5.])

# 擬合直線

regression1.fit(x, y)

print('a:', regression1.a_, ',b:', regression1.b_)

print('擬合的直線方程為:y=', regression1.a_, '* x + ', regression1.b_)

# 進行**

x_predict = 6

y_predict = regression1.predict(np.array([x_predict]))

print(y_predict)

(5)執行效果:

機器學習線性回歸 機器學習中的簡單線性回歸步驟

簡單線性回歸,這是一種基於自變數值 x 來 因變數值 y 的方法。假設這兩個變數是線性相關的。因此,我們嘗試尋找一種根據特徵或自變數 x 的線性函式來精確 響應值 y 我們將按照之前的資料預處理資訊圖表那樣來執行相同的步驟 其中第三相關庫matplotlib是用來視覺化資料的。匯入資料集 檢查缺失資...

機器學習回歸篇 簡單線性回歸

之前的幾篇裡面講了機器學習分類問題的一些演算法,下面幾篇來講一下回歸問題。回歸問題和分類問題有什麼區別呢?分類問題 的結果是一些類別值,比如說,顏色類別,電腦品牌,有無信譽等。回歸問題 的是連續型的數值,如房價,人數,降雨量等等 生活中我們做決定的過程通常是根據兩個或多個變數之間的關係,解決回歸問題...

機器學習(1) 簡單線性回歸

coding utf8 import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt from matplotlib.font manager import rebuild rebuild mpl.rcpara...