多項式回歸

2021-08-19 14:12:36 字數 4076 閱讀 7451

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(x, y)

plt.show()

from sklearn.linear_model import linearregression

lin_reg = linearregression()

lin_reg.fit(x, y)

y_predict = lin_reg.predict(x)

plt.scatter(x, y)

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

plt.show()

x2 = np.hstack([x, x**2])

>>> x2.shape

(100, 2)

lin_reg2 = linearregression()

lin_reg2.fit(x2, y)

y_predict2 = lin_reg2.predict(x2)

plt.scatter(x, y)

plt.plot(np.sort(x), y_predict2[np.argsort(x)], color='r')

plt.show()

from sklearn.preprocessing import polynomialfeatures

poly = polynomialfeatures(degree=2) //為原本資料集新增最多2次冪的特徵

poly.fit(x)

x2 = poly.transform(x)

>>> x2.shape

(100, 3) //第一列全是1,代表x的0次方。第二列和第三列分別代表x的1,2次方

from sklearn.linear_model import linearregression

lin_reg2 = linearregression()

lin_reg2.fit(x2, y)

y_predict2 = lin_reg2.predict(x2)

plt.scatter(x, y)

plt.plot(np.sort(x), y_predict2[np.argsort(x)], color='r')

plt.show()

lin_reg2.coef_ //係數

>>> array([ 0. , 0.9460157 , 0.50420543])

>>> lin_reg2.intercept_

2.1536054095953823 //截距

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)

from sklearn.pipeline import pipeline

from sklearn.preprocessing import standardscaler

//構造pipeline時傳入列表,列表中傳入"管道"中每乙個步驟對應的類,每個類時元組的形式。

//送給pipeline的資料沿著"管道"的格式進行

poly_reg = pipeline([

("poly", polynomialfeatures(degree=2)),//第一步:多項式的特徵

("std_scaler", standardscaler()), //第二步:資料歸一化

("lin_reg", linearregression()) //第三部:線性回歸

])poly_reg.fit(x, y)

y_predict = poly_reg.predict(x)

plt.scatter(x, y)

plt.plot(np.sort(x), y_predict[np.argsort(x)], color='r')

plt.show()

from sklearn.metrics import mean_squared_error

y_predict = lin_reg.predict(x)

mean_squared_error(y, y_predict)

np.random.seed(666)

x = np.random.uniform(-3.0, 3.0, size=100)

x = x.reshape(-1, 1)

y = 0.5 * x**2 + x + 2 + np.random.normal(0, 1, size=100)

from sklearn.model_selection import train_test_split

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

from sklearn.linear_model import linearregression

from sklearn.metrics import mean_squared_error

train_score =

test_score =

for i in range(1, 76):

lin_reg = linearregression()

lin_reg.fit(x_train[:i], y_train[:i])

y_train_predict = lin_reg.predict(x_train[:i])

y_test_predict = lin_reg.predict(x_test)

plt.plot([i for i in range(1, 76)], np.sqrt(train_score), label="train")

plt.plot([i for i in range(1, 76)], np.sqrt(test_score), label="test")

plt.legend()

plt.show()

def

plot_learning_curve

(algo, x_train, x_test, y_train, y_test): //algo:演算法

train_score =

test_score =

for i in range(1, len(x_train)+1):

algo.fit(x_train[:i], y_train[:i])

y_train_predict = algo.predict(x_train[:i])

y_test_predict = algo.predict(x_test)

plt.plot([i for i in range(1, len(x_train)+1)],

np.sqrt(train_score), label="train")

plt.plot([i for i in range(1, len(x_train)+1)],

np.sqrt(test_score), label="test")

plt.legend()

plt.axis([0, len(x_train)+1, 0, 4])

plt.show()

plot_learning_curve(linearregression(), x_train, x_test, y_train, y_test)

多項式回歸

多項式回歸 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...

多項式回歸

多項式回歸 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 按列拼接 ...