簡單對照統(tǒng)計學(xué)習(xí)上的感知機理論,基于numpy實現(xiàn)感知機學(xué)習(xí)模型
1代碼
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
def fit(x,y,max_iter=100, tol=1e-3, eta0=0.1):
y = y.reshape(-1, 1)
if x.shape[0]!=y.shape[0] or y.shape[1]!=1:
raise ValueError
iter_count = 0
w = np.ones(shape=(x.shape[1],1))
b = 0
loss = (np.dot(x, w) + b) * y
loss_function_sum = -1 * np.sum(loss[np.where(loss < 0)])
error_lst = list(np.where(loss < 0)[0])
while (iter_count<max_iter and loss_function_sum>tol and len(error_lst)>0):
loss = (np.dot(x, w) + b) * y
loss_function_sum = -1*np.sum(loss[np.where(loss<0)])
iter_count += 1
error_lst = list(np.where(loss < 0)[0])
if len(error_lst)>0:
error_x = x[error_lst[0]]
error_y = y[error_lst[0]]
w_step = (eta0 * error_x * error_y).reshape(-1, 1)
b_step = (eta0*error_y)[0]
w += w_step
b += b_step
b = round(b,6)
else:
continue
return w,b
if __name__ == '__main__':
filePath = r'D:\statisticsLearn\perception\data'
data = pd.read_csv(filePath + r'\sample.csv')
data['y'] = data['y'].apply(lambda x:2*x-1)
x = data[['x1','x2']].values
y = data['y'].values
# 訓(xùn)練數(shù)據(jù)和測試數(shù)據(jù)
x_data_train = x[:80, :]
x_data_test = x[80:, :]
y_data_train = y[:80]
y_data_test = y[80:]
# 正例和反例
positive_x1 = [x[i, 0] for i in range(100) if y[i] == 1]
positive_x2 = [x[i, 1] for i in range(100) if y[i] == 1]
negetive_x1 = [x[i, 0] for i in range(100) if y[i] == -1]
negetive_x2 = [x[i, 1] for i in range(100) if y[i] == -1]
coef = fit(x,y,max_iter=1000,eta0=1)
plt.scatter(positive_x1, positive_x2, c='red')
plt.scatter(negetive_x1, negetive_x2, c='blue')
# 畫出超平面(在本例中即是一條直線)
line_x = np.arange(-4, 4)
line_y = line_x * (coef[0][0] / coef[0][1]) - coef[1]
plt.plot(line_x, line_y)
plt.show()
2與sklearn比較

Figure_1.png