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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python 多元线性回归_Python机器学习,多元线性回归分析问题

發布時間:2025/4/5 python 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 多元线性回归_Python机器学习,多元线性回归分析问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

@Author:潤森,Runsen,公眾號:潤森筆記

什么是多元線性回歸

在回歸分析中,如果有兩個或兩個以上的自變量,就稱為多元回歸。**事實上,一種現象常常是與多個因素相聯系的,由多個自變量的最優組合共同來預測或估計因變量,比只用一個自變量進行預測或估計更有效,更符合實際。**因此多元線性回歸比一元線性回歸的實用意義更大。

y=β0+β1x1+β2x2+ … +βpxp+ε # 公式

今天講一個例子

這里有個excel 文件數據,我們來研究到底是哪個因素影響sales最明顯,是TV,還是radio,還是newspaper,也就是找的銷售額到底是那家個元素引起的,怎么才能提高銷售額?

導入相對的庫

import pandas as pd import numpy as np import matplotlib.pyplot as plt %matplotlib inline plt.style.use('ggplot') #使用ggplot樣式 from sklearn.linear_model import LinearRegression # 導入線性回歸 from sklearn.model_selection import train_test_split # 訓練數據 from sklearn.metrics import mean_squared_error #用來計算距離平方誤差,評價模型

打開文件

data = pd.read_csv('Advertising.csv') data.head() #看下data

先畫圖分析一下

plt.scatter(data.TV, data.sales)

plt.scatter(data.radio, data.sales)

plt.scatter(data.newspaper, data.sales)

從圖中分析看出newspaper的點分散太廣,預測毫無關系,應該要去除

進入代碼環節

x = data[['TV','radio','newspaper']] y = data.sales x_train,x_test,y_train,y_test = train_test_split(x, y) #得到訓練和測試訓練集 model = LinearRegression() #導入線性回歸 model.fit(x_train, y_train) # model.coef_ # 斜率 有三個 model.intercept_ # 截距

得到

array([ 0.04480311, 0.19277245, -0.00301245]) 3.0258997429585506 for i in zip(x_train.columns, model.coef_):print(i) #打印對應的參數('TV', 0.04480311217789182) ('radio', 0.19277245418149513) ('newspaper', -0.003012450368706149) mean_squared_error(model.predict(x_test), y_test) # 模型的好壞用距離的平方和計算4.330748450267551y =0.04480311217789182 * x1 + 0.19277245418149513 *x2 -0.003012450368706149 * x3 + 3.0258997429585506

我們可以看到newspaper的的系數小于0,說明了投入了,反而影響銷售額 那么如何改進模型,就是去掉newspaper的數值

x = data[['TV','radio']] y = data.sales x_train,x_test,y_train,y_test = train_test_split(x, y) model2 = LinearRegression() model2.fit(x_train,y_train) model2.coef_ model2.intercept_ mean_squared_error(model2.predict(x_test),y_test)array([0.04666856, 0.17769367]) 3.1183329992288478 2.984535789030915 # 比第一個model的小,說明更好y =0.04666856 * x1 +0.17769367 *x2 + 3.1183329992288478

總結

以上是生活随笔為你收集整理的python 多元线性回归_Python机器学习,多元线性回归分析问题的全部內容,希望文章能夠幫你解決所遇到的問題。

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