日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

机器学习线性回归案例讲解_09机器学习实战之简单线性回归

發布時間:2023/12/10 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 机器学习线性回归案例讲解_09机器学习实战之简单线性回归 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

基本概念

1. 介紹:

回歸(regression) Y變量為連續數值型(continuous?numerical?variable)

如:房價,人數,降雨量

分類(Classification): Y變量為類別型(categorical?variable)

如:顏色類別,電腦品牌,有無信譽

2. 簡單線性回歸(Simple Linear Regression)

2.1 很多做決定過過程通常是根據兩個或者多個變量之間的關系

2.3 回歸分析(regression analysis)用來建立方程模擬兩個或者多個變量之間如何關聯

2.4 被預測的變量叫做:因變量(dependent variable), y, 輸出(output)

2.5 被用來進行預測的變量叫做: 自變量(independent variable), x, 輸入(input)

3. 簡單線性回歸介紹

3.1 簡單線性回歸包含一個自變量(x)和一個因變量(y)

3.2 以上兩個變量的關系用一條直線來模擬

3.3 如果包含兩個以上的自變量,則稱作多元回歸分析(multiple regression)

4. 簡單線性回歸模型

4.1 被用來描述因變量(y)和自變量(X)以及偏差(error)之間關系的方程叫做回歸模型

4.2 簡單線性回歸的模型是:

其中:? ?參數 ? ? ? ? ? ? ? ? ? 偏差

5. 簡單線性回歸方程

E(y) =?β0+β1x

這個方程對應的圖像是一條直線,稱作回歸線

其中,β0是回歸線的截距

β1是回歸線的斜率

E(y)是在一個給定x值下y的期望值(均值)

ε服從標準正太分布,均值為0

6. 正向線性關系

7. 負向線性關系

8. 無關系

9. 估計的簡單線性回歸方程

y?=b0+b1x

這個方程叫做估計線性方程(estimated?regression line)

其中,b0是估計線性方程的縱截距

b1是估計線性方程的斜率

y?是在自變量x等于一個給定值的時候,y的估計值

10. 線性回歸分析流程

11. 關于偏差ε的假定

11.1 是一個隨機的變量,均值為0

11.2?ε的方差(variance)對于所有的自變量x是一樣的

11.3?ε的值是獨立的

11.4?ε滿足正態分布

例子

簡單線性回歸模型舉例:

汽車賣家做電視廣告數量與賣出的汽車數量:

第一步:如何練處適合簡單線性回歸模型的最佳回歸線?

使sum of squares最小

第二步:計算

分子 = (1-2)(14-20)+(3-2)(24-20)+(2-2)(18-20)+(1-2)(17-20)+(3-2)(27-20)

= 6 + 4 + 0 + 3 + 7

= 20

分母 = (1-2)^2 + (3-2)^2 + (2-2)^2 + (1-2)^2 + (3-2)^2

= 1 + 1 + 0 + 1 + 1

4

b1 = 20/4 ?=5

b0 = 20 - 5*2 = 20 - 10 = 10

推導過程

代碼實現

In?[3]:

import numpy as np

import matplotlib.pyplot as plt

In?[4]:

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

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

In?[10]:

plt.scatter(x, y)

plt.axis([0, 6, 0, 6])

plt.show()

In?[11]:

x_mean = np.mean(x)

y_mean = np.mean(y)

In?[12]:

numerator = 0.0 # 分子

denominator = 0.0 # 分母

In?[13]:

for x_i, y_i in zip(x, y):

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

denominator += (x_i - x_mean) ** 2

In?[14]:

a = numerator / denominator

b = y_mean - a * x_mean

In?[15]:

a

Out[15]:

0.8

In?[16]:

b

Out[16]:

0.39999999999999947

In?[17]:

y_hat = a * x + b

In?[19]:

plt.scatter(x, y)

plt.plot(x, y_hat, color='r')

plt.axis([0, 6, 0, 6])

plt.show()

In?[20]:

x_predict = 6

y_predict = a * x_predict + b

y_predict

Out[20]:

5.2

In?[28]:

from ml09simpleLinearRegression1 import SimpleLinearRegression1

In?[29]:

reg1 = SimpleLinearRegression1()

reg1.fit(x, y)

Out[29]:

SimpleLinearRegression()

In?[30]:

reg1.predict(np.array([x_predict]))

Out[30]:

array([5.2])

In?[31]:

reg1.a_

Out[31]:

0.8

In?[32]:

reg1.b_

Out[32]:

0.39999999999999947

In?[33]:

y_hat1 = reg1.predict(x)

In?[34]:

plt.scatter(x, y)

plt.plot(x, y_hat1, color='r')

plt.axis([0, 6, 0, 6])

plt.show()

importnumpy as npclassSimpleLinearRegression1:def __init__(self):"""初始化Simple Linear Regression模型"""self.a_=None

self.b_=Nonedeffit(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)#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

"""使用向量點積,代替上面的for循環"""self.a_= (x_train - x_mean).dot(y_train - y_mean) / (x_train - x_mean).dot(x_train -x_mean)

self.b_= y_mean - self.a_ *x_meanreturnselfdefpredict(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 notNone, \"must fit before predict!"

return np.array([self._predict(x) for x inx_predict])def_predict(self, x_single):"""給定單個待預測數據x,返回x的預測結果值"""

return self.a_ * x_single +self.b_def __repr__(self):return "SimpleLinearRegression()"

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的机器学习线性回归案例讲解_09机器学习实战之简单线性回归的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。