scikit learning 線性回歸學習

2021-09-18 00:11:33 字數 3069 閱讀 7435

多項式回歸損失函式為均方誤差,迭代計算方式為梯度下降;在scikit-learning中,多項式回歸由sklearn.linear_model.linearregression。

當特徵不足導致欠擬合時,可以採用特徵組合的方式產生多項式特徵,在scikit-learning中,用sklearn.preprocessing.polynomialfeatures產生多項式特徵。

sklearn.pipline.pipline可以把多個模型串聯起來,執行起來相當於乙個模型。

import matplotlib.pyplot as plt

import numpy as np

from sklearn.linear_model import linearregression

from sklearn.preprocessing import polynomialfeatures

from sklearn.pipeline import pipeline

from sklearn.metrics import mean_squared_error

#多項式回歸由sklearn.linear_model.linearregression實現

#sklearn.preprocessing.polynomialfeatures產生多項式特徵

#sklearn.pipline.pipline把生成多項式特徵和線性回歸訓練串聯起來

defgenerate_dot

(dot_num, left, right)

: x = np.linspace(left, right, dot_num)

y = np.cos(x)

+0.2

* np.random.random(dot_num)

-0.1

x = x.reshape(-1

,1) y = y.reshape(-1

,1)return

(x,y)

defpolynomial_regression

(degree=1)

: polynomial_features = polynomialfeatures(degree=degree, include_bias=

false

) linear_regression_model = linearregression(normalize=

true

)#資料歸一化

new_model = pipeline([(

"polynomial_features"

, polynomial_features),(

"linear_regression"

, linear_regression_model)])

return new_model

deftrain_model

(x,y,degrees)

:

res =

for degree in degrees:

model = polynomial_regression(degree)

model.fit(x, y)

train_score = model.score(x, y)

#**點的值和其實際值均方根誤差

mse = mean_squared_error(y, model.predict(x)))

for r in res:

print

("degree: {}; train score: {}; mean squared error: {}"

.format

(r["degree"

], r[

"score"

], r[

"mse"])

)return res

x,y = generate_dot(

100,-7

,7)degrees =[2

,3,4

,5,6

,10]result = train_model(x,y,degrees)

#視覺化

plt.figure(figsize=(12

,7), dpi=

300)

for index, dic in

enumerate

(result)

: fig = plt.subplot(2,

3, index +1)

plt.title(

"modle degree : %d"

%dic[

"degree"])

plt.scatter(x, y, s=

4, alpha=

0.5)

plt.plot(x, dic[

"model"

].predict(x)

,'r--'

)plt.show(

)

輸出:

scikit learning 交叉驗證

import numpy as np from sklearn import datasets from sklearn.cross validation import train test split from sklearn.neighbors import kneighborsclassifi...

對於線線問題

以下是乙個大佬的總結 authorlcy註明出處,摘自 1 n條直線最多分平面問題 題目大致如 n條直線,最多可以把平面分為多少個區域。析 可能你以前就見過這題目,這充其量是一道初中的思考題。但乙個型別的題目還是從簡單的入手,才容易發現規律。當有n 1條直線時,平面最多被分成了f n 1 個區域。則...

交叉線和直通線

2010 1 24 21 25 49 問 copper cross over與copper straight through兩種電纜有什麼區別?這兩種銅質電纜,我就是搞不懂有什麼不同!還有serial dte和serial dce兩種線的區別又是什麼呢?這兩種線與題目上的兩種線的接頭是不是一樣的呢?...