純numpy寫線性回歸 及二分類模型

2021-10-07 10:49:31 字數 4244 閱讀 3401

參照別人的寫法進行改進,資料標準化和資料集的切分,也完全手寫,不使用sklearn的相關方法。

改進了下之前的**,引用了sklearn中的資料集。

通過引數設定,來指定使用線性回歸,還是二分類邏輯回歸。

但是,不知道怎麼寫多分類的模型演算法。。如可以,懇請高手指點下。。

#純手寫numpy實現回歸和二分類模型

from sklearn import datasets

import numpy as np

defdata_load()

:if model_type ==

"linear"

:#線性回歸模型

data = datasets.load_boston(

)elif model_type ==

"sigmoid"

:#二分類模型

data = datasets.load_breast_cancer(

) x = data.data

y = data.target

return x, y

defdata_split

(x, y, train_size =

0.8)

:#切分資料集

num = x.shape[0]

#獲取資料量

index =

[i for i in

range

(num)

]#獲取虛擬的索引值,以便繫結x和y的對應關係

np.random.seed(

10001

)#獲取隨機種子,方便結果復現

np.random.shuffle(index)

x = x[index]

y = y[index]

train_num =

int(num * train_size)

x_train = x[

:train_num]

x_test = x[train_num:

] y_train = y[

:train_num]

y_test = y[train_num :

]return x_train, x_test, y_train, y_test

defdata_standscale

(x):

#標準化資料集

x_mean = np.mean(x)

x_std = np.std(x)

x_new =

(x - x_mean)

/ x_std

return x_new

defdata_cal

(x, w, b)

: y_ = np.dot(w, x.t)

+ b if model_type ==

'sigmoid'

: y_ =1/

(1+ np.exp(

-y_)

)return y_

defgradient_descent

(x, y, w, b, learning_rate)

: num = x.shape[0]

y_ = data_cal(x, w, b)

#計算初步結果

if model_type ==

'linear'

: cost = np.

sum(

(y_ - y)**2

)/ num #均方誤差損失函式

elif model_type ==

'sigmoid'

: cost =

- np.

sum(y * np.log(y_)+(

1- y)

* np.log(

1- y_)

)/ num #二分類交叉熵損失函式

g_w = np.dot(x.t,

(y_ - y)

)/ num

g_b = np.

sum(y_ - y)

/ num

w = w - g_w * learning_rate

b = b - g_b * learning_rate

return w, b, cost

defmodel_train

(x, y, epochs, batch_size, learning_rate)

: num = x.shape[0]

#資料量

num_features = x.shape[1]

#特徵數

if x.ndim ==1:

num_features =

1 w = np.random.random(size=

(num_features,))

#隨機初始化w值

b =0for epoch in

range

(epochs)

:#迭代的輪數

batch_num = num // batch_size +

1#根據資料情況,獲得batch的數量,來確定每輪裡還要更新幾次權重

if num % batch_size ==0:

batch_num = num // batch_size

for batch in

range

(batch_num)

: batch_x = x[batch * batch_size :

(batch +1)

* batch_size]

batch_y = y[batch * batch_size :

(batch +1)

* batch_size]

w, b, cost = gradient_descent(batch_x, batch_y, w, b, learning_rate)

if epoch %

(epochs //10)

==0:print

('epoch'

, epoch,

'cost'

, cost)

return w, b

defmain()

: x, y = data_load(

)#載入資料集

x = data_standscale(x)

#標準化處理資料

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

#切分資料集

w, b = model_train(x_train, y_train, epochs, batch_size, learning_rate)

prediction = data_cal(x_test, w, b)

#根據迭代之後的權重值,獲得**結果

if model_type ==

'linear'

: accuracy =

#初始化準確度

for i, pred in

enumerate

(prediction):1

- np.

abs(pred - y_test[i]

)/ y_test[i]

)print

('test accuracy:'

, np.mean(accuracy)

)elif model_type ==

"sigmoid"

: num =

0#初始化正確**的個數

for i, pred in

enumerate

(prediction)

:if pred >=

0.5:

if y_test[i]==1

: num +=

1else

:if y_test[i]==0

: num +=

1print

('test accuracy'

, num /

len(y_test)

)if __name__ ==

'__main__'

: epochs =

int(

1e5)

batch_size =

32 learning_rate =1e-

3 model_type =

'sigmoid'

main(

)

邏輯回歸 線性二分類和非線性啟用

目錄 背景介紹 邏輯回歸 邏輯回歸屬於線性分類器?神經網路 交叉熵與kl散度 啟用函式 自然邏輯 比如,當已知y的區間為 0,1 時,線性回歸就保證不了 值屬於這個區間。logistic regression 二分類線性分類器。邏輯回歸是個二分類器 binary classification 雖說邏...

NG 邏輯回歸 二分類

import time import numpy as np import pandas as pd import matplotlib.pyplot as plt a pd.read table 01.txt header none,usecols 0,1,2 插入一列 1 a.insert 0,...

機器學習 回歸二分類demo

一基礎知識 1.tensorflow使用張量表示資料,使用 圖 表示計算任務,使用會話來執行圖。dtype和shape是張量的屬性 import tensorflow as tf 基礎知識 t1 tf.constant 10,dtype tf.int32 sess tf.session print ...