2020-07-13
閱讀量:
2681
感知機(jī)對(duì)偶形式的Python代碼實(shí)現(xiàn)
''' 感知機(jī)(Perceptron) 1957年由Rosenblatt提出,是神經(jīng)網(wǎng)絡(luò)與支持向量機(jī)的基礎(chǔ)。 感知機(jī)學(xué)習(xí)算法具有簡(jiǎn)單而易于實(shí)現(xiàn)的優(yōu)點(diǎn),分為原始形式和對(duì)偶形式 輸入為實(shí)例的特征向量,輸出為實(shí)例的類(lèi)別,取+1和-1 感知機(jī)對(duì)應(yīng)于輸入空間中將實(shí)例劃分為正負(fù)兩類(lèi)的分離超平面,屬于判別模型 導(dǎo)入基于誤分類(lèi)的損失函數(shù) 利用梯度下降法對(duì)損失函數(shù)進(jìn)行極小化 ''' import numpy as np class Dual_Perceptron(object): def __init__(self, X, y, eta = 1, n_iter = 100): self.eta = eta self.n_iter = n_iter def fit(self, X, y): self.errors_ = [] self.Remp_ = [] self.Gram = np.zeros((len(X), len(X))) for i in np.arange(len(X)): for j in np.arange(len(X)): self.Gram[i,j] = self.Gram[j,i] = X[i] @ X[j] + 1 self.alpha = np.zeros(len(X)) self.b = 0 self._L = y * self.alpha @ self.Gram n = 1 while np.any(self._L <= 0) and n <= self.n_iter: error_id = np.argmax(self._L <= 0) self.alpha[error_id] += self.eta self.b += self.eta * y[error_id] self._L = y * (y * self.alpha @ self.Gram) Remp = -1 * np.sum(self._L[self._L <= 0]) print(f'Round {n}: alpha: {self.alpha}, Remp: {Remp}') self.errors_.append(np.sum(self._L <= 0)) self.Remp_.append(Remp) n += 1 self.w = self.alpha * y @ X print(f'w: {self.w}, b: {self.b}') def predict(self, X): return np.sign(X @ self.w + self.b)






CDA考試動(dòng)態(tài)
CDA報(bào)考指南
推薦帖子
0條評(píng)論
0條評(píng)論
0條評(píng)論