sklearn 多項式回歸

2021-09-24 08:27:48 字數 2599 閱讀 6231

github位址 github.com/yangjinghit…

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

%matplotlib inline

複製**

/anaconda3/envs/py35/lib/python3.5/importlib/_bootstrap.py:222: runtimewarning: numpy.dtype size changed, may indicate binary incompatibility. expected 96, got 88

return f(*args, **kwds)

複製**

from sklearn.linear_model import linearregression

from sklearn.model_selection import train_test_split

from sklearn.metrics import mean_squared_error

複製**

plt.style.use('ggplot')

複製**

data = pd.read_csv('advertising.csv')

複製**

data.head()

複製**

unnamed: 0

tvradio

news*****

sales01

230.1

37.8

69.2

22.112

44.5

39.3

45.1

10.423

17.2

45.9

69.3

9.33

4151.5

41.3

58.5

18.545

180.8

10.8

58.4

12.9

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

複製**

複製**

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

複製**

複製**

plt.scatter(data.news*****, data.sales)

複製**

複製**

x = data[['tv', 'radio', 'news*****']]

複製**

y = data.sales

複製**

x_train, x_test, y_train, y_test = train_test_split(x, y)

複製**

len(x_train), len(y_train)

複製**

(150, 150)

複製**

len(x_test)

複製**

50

複製**

model = linearregression()

複製**

model.fit(x_train, y_train)

複製**

linearregression(copy_x=true, fit_intercept=true, n_jobs=1, normalize=false)

複製**

model.coef_

複製**

array([ 0.04466416,  0.19594144, -0.00469486])

複製**

for i in zip(x_train.columns, model.coef_):

print(i)

複製**

('tv', 0.04466415613441986)

('radio', 0.1959414384329583)

('news*****', -0.0046948632484331895)

複製**

mean_squared_error(model.predict(x_test), y_test)

複製**

3.927556655626268

複製**

多項式回歸

import numpy as np import matplotlib.pyplot as plt x np.random.uniform 3,3,size 100 x x.reshape 1,1 y 0.5 x 2 x 2 np.random.normal 0,1,100 plt.scatter...

多項式回歸

多項式回歸 import torch import numpy defmake features x 獲取 x,x 2,x 3 的矩陣 x x.unsqueeze 1 將一維資料變為 n,1 二維矩陣形式 return torch.cat x i for i in range 1 4 1 按列拼接 ...

多項式回歸

線性回歸適用於資料成線性分布的回歸問題,如果樣本是非線性分布,線性回歸就不再使用,轉而可以採用非線性模型進行回歸,比如多項式回歸 多項式回歸模型定義 與線性模型,多項式模型引入了高次項 y w 0 w1 x w2 x2 w 3x3 wnxn y w 0 w 1x w 2x 2 w 3x 3 w nx...