1手寫線性回歸 2

2021-09-25 10:06:46 字數 4881 閱讀 4893

github

import numpy as np

from sklearn.linear_model import linearregression

from sklearn.datasets import load_diabetes

from sklearn.utils import shuffle

import matplotlib.pyplot as plt

# 形狀非常重要,而且容易錯誤

deffit_normal

(x_train, y_train)

:"""根據訓練資料集x_train, y_train訓練linear regression模型"""

assert x_train.shape[0]

== y_train.shape[0]

, \ "the size of x_train must be equal to the size of y_train"

# np.vstack():在豎直方向上堆疊

# np.hstack():在水平方向上平鋪

x_b = np.hstack(

[np.ones(

(len

(x_train),1

)), x_train]

)# 為了增加常數項

theta = np.linalg.inv(x_b.t.dot(x_b)

).dot(x_b.t)

.dot(y_train)

intercept = theta[0]

coef = theta[1:

]return theta

deffit_bgd

(x_train, y_train, eta=

0.01

, n_iters=

1e5)

:"""根據訓練資料集x_train, y_train, 使用梯度下降法訓練linear regression模型"""

assert x_train.shape[0]

== y_train.shape[0]

, \ "the size of x_train must be equal to the size of y_train"

defcostfunc

(theta, x_b, y)

:# 計算損失函式

try:

return np.

sum(

(y - x_b.dot(theta))**

2)/len

(y)/

2except

:return

float

('inf'

)def

dj(theta, x_b, y)

:# 損失函式求導

return x_b.t.dot(x_b.dot(theta)

- y)

/len

(y)def

gradient_descent

(x_b, y, initial_theta, eta, n_iters=n_iters, epsilon=1e-

8): theta = initial_theta

cur_iter =

0print

('x_b.dot(theta)='

,(x_b.dot(theta)

).shape)

print

('(x_b.dot(theta) - y).shape='

,(x_b.dot(theta)

- y)

.shape)

print

('x_b.t.dot(x_b.dot(theta) - y).shape='

,x_b.t.dot(x_b.dot(theta)

- y)

.shape)

# y = np.array(data[:,1])時的維度

# y_train.shape= (97,)

# theta.shape= (2,)

# x_b.dot(theta)= (97,)

# (x_b.dot(theta) - y).shape= (97,)

# x_b.t.dot(x_b.dot(theta) - y).shape= (2,)

# y = np.c_[data[:,1]]時的維度

# y_train.shape= (97, 1)

# theta.shape= (2,)

# x_b.dot(theta)= (97,)

# (x_b.dot(theta) - y).shape= (97, 97)

# x_b.t.dot(x_b.dot(theta) - y).shape= (2, 97)

# valueerror: operands could not be broadcast together with shapes (2,) (2,97)

while cur_iter < n_iters:

gradient = dj(theta, x_b, y)

# print((x_b.dot(theta)).shape)

last_theta = theta

# print(gradient.shape)

theta = theta - eta * gradient

if(abs

(costfunc(theta, x_b, y)

- costfunc(last_theta, x_b, y)

)< epsilon)

:break

cur_iter +=

1return theta

x_b = np.hstack(

[np.ones(

(len

(x_train),1

)), x_train]

)print

('x_b.shape='

,x_b.shape)

print

('y_train.shape='

,y_train.shape)

initial_theta = np.zeros(x_b.shape[1]

)#初始化theta

print

('theta.shape='

,initial_theta.shape)

theta = gradient_descent(x_b, y_train, initial_theta, eta, n_iters)

intercept_ = theta[0]

coef_ = theta[1:

]return theta

defpredict

(x_predict,theta)

:"""給定待**資料集x_predict,返回表示x_predict的結果向量"""

x_b = np.hstack(

[np.ones(

(len

(x_predict),1

)), x_predict]

)return x_b.dot(theta)

deftest()

: data = np.loadtxt(

'linear_regression_data1.txt'

, delimiter=

',')

x = np.c_[data[:,

0]] y = np.array(data[:,

1]) y1 = np.c_[data[:,

1]]print

(fit_normal(x,y)

)print

(fit_bgd(x,y)

) regr = linearregression(

) regr.fit(x, y)

print

(regr.intercept_,regr.coef_)

deftest0425()

:# 載入資料

diabets = load_diabetes(

) data = diabets.data

target = diabets.target

# 打亂資料

x, y = shuffle(data, target, random_state=13)

# 劃分訓練集和測試集

offset =

int(x.shape[0]

*0.9

) x_train, y_train = x[

:offset]

, y[

:offset]

x_test, y_test = x[offset:

], y[offset:

] y_train = y_train.reshape((-

1,1)

) y_test = y_test.reshape((-

1,1)

)print

(x_train.shape)

print

(x_test.shape)

print

(y_train.shape)

print

(y_test.shape)

x=x_train

y=y_train

print

(fit_normal(x,y)

)print

(fit_bgd(x,y.reshape(

len(y)))

) regr = linearregression(

) regr.fit(x, y)

print

(regr.intercept_,regr.coef_)

if __name__ ==

'__main__'

: test0425(

)

08 手寫多項式回歸

直線回歸研究的是乙個依變數與乙個自變數之間的回歸問題,但是,在畜禽 水產科學領域的許多實際問題中,影響依變數的自變數往往不止乙個,而是多個,比如綿羊的產毛量這一變數同時受到綿羊體重 胸圍 體長等多個變數的影響,因此需要進行乙個依變數與多個自變數間的回歸分析,即多元回歸分析 研究乙個因變數與乙個或多個...

Vue深入 12 手寫lazyload 1

npm i vue lazyload s main.js中引入 import vuelazyload from vue lazyload vue.use vuelazyload,使用編寫指令 vue.user會先執行install方法 main.js vue.use mylazyload index...

從零手寫RPC

clientstub sereverstub 可以看作乙個 物件,遮蔽rpc呼叫過程中複雜的網路處理邏輯,使rpc透明化,使得呼叫遠端方法想呼叫本地方法一樣。server 服務端提供遠端服務。注 serverstub又叫skeleton。public inte ce ihellopublic cla...