感知機手寫

2021-10-08 06:14:11 字數 2571 閱讀 6062

參考文獻:1.

2. 應用多元統計分析,高惠璇. 北京大學出版社

3. 統計學習方法第二版,李航

import numpy as np

class

model

:def

__init__

(self, w=[0

,0], b=

0, alpha=

0.2, max_counts=

5000):

self.w = np.array(w, dtype=np.float32)

self.b = b

self.l_rate = alpha

self.max_counts = max_counts

self.pred_list =

# self.data = data

defsign

(self, x, w, b)

: y = np.dot(x, w)

+ b return y

# 隨機梯度下降法

deffit

(self, x_train, y_train)

: is_wrong =

false

count =

0while

not is_wrong:

count +=

1 wrong_count =

0for d in

range

(len

(x_train)):

x = x_train[d]

y = y_train[d]

if y * self.sign(x, self.w, self.b)

<=0:

self.w = self.w + self.l_rate * np.dot(y, x)

self.b = self.b + self.l_rate * y

wrong_count +=

1if self.max_counts == count:

print

("在設定的最大次數中,無法分開"

)break

if wrong_count ==0:

is_wrong =

true

return

'perceptron model!'

defpredict

(self, x_test, y_test)

: self.y_test = y_test

for i in x_test:

if(np.dot(self.w, i)

+ self.b)

>0:

1)else:-

1)self.pred_array = np.array(np.array(self.pred_list)

== self.y_test)

return self.pred_array

defscore

(self, method=

"precision"):

if method ==

"precision"

: pre_scr = np.array(self.pred_array, dtype=np.int8)

precision_scr = np.

sum(pre_scr)

/len

(pre_scr)

return precision_scr

elif method ==

"call"

:from collections import counter

class_dic = counter(self.y_test)

count =

0for i, j in

zip(self.pred_list, self.y_test)

:if i ==

1and j ==1:

count +=

1 call_scr = count / class_dic[1]

return call_scr

elif method ==

"f":

pre_scr = np.array(self.pred_array, dtype=np.int8)

precision_scr = np.

sum(pre_scr)

/len

(pre_scr)

from collections import counter

class_dic = counter(self.y_test)

count =

0for i, j in

zip(self.pred_list, self.y_test)

:if i ==

1and j ==1:

count +=

1 call_scr = count / class_dic[1]

return

(call_scr + precision_scr)

/2

機器學習 手寫感知機模型

資料集來自mnist資料集,主要利用numpy裡的matrix矩陣計算。演算法的實現主要在於對引數w和b的求解。演算法的推導過程參考李航 統計學習方法 推導最優化函式然後更新引數的過程。import numpy as np import pandas as pd import time defdat...

離線手寫中文簽名檢索 軟體設計方案

離線手寫中文簽名檢索 軟體設計方案 一 專案背景 我的工程實踐是離線 離線手寫中文簽名的智慧型檢索系統,首先通過人工智慧和深入學習技術,搭建乙個深度學習網路,再利用資料庫對網路進行訓練,最後通過自製框架整合到網頁端,搭建乙個簽名檢測系統,實現乙個在web端根據輸入的簽名從簽名資料庫中根據每個簽名的相...

Keras多層感知器識別手寫數字

from keras.utils import np utils import numpy as np np.random.seed 10 from keras.datasets import mnist x train image,y train label x test image,y test...