from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from sklearn.neural_network import MLPClassifier
from sklearn.datasets import load_iris
生成线性可分数据集
def creat_data(n):
np.random.seed(1)
x_11=np.random.randint(0,100,(n,1))
x_12=np.random.randint(0,100,(n,1,))
x_13=20+np.random.randint(0,10,(n,1,))
x_21=np.random.randint(0,100,(n,1))
x_22=np.random.randint(0,100,(n,1))
x_23=10-np.random.randint(0,10,(n,1,))
new_x_12=x_12*np.sqrt(2)/2-x_13*np.sqrt(2)/2
new_x_13=x_12*np.sqrt(2)/2+x_13*np.sqrt(2)/2
new_x_22=x_22*np.sqrt(2)/2-x_23*np.sqrt(2)/2
new_x_23=x_22*np.sqrt(2)/2+x_23*np.sqrt(2)/2
plus_samples=np.hstack([x_11,new_x_12,new_x_13,np.ones((n,1))])
minus_samples=np.hstack([x_21,new_x_22,new_x_23,-np.ones((n,1))])
samples=np.vstack([plus_samples,minus_samples])
np.random.shuffle(samples)
return samples
感知机学习算法的原始形式算法
def perceptron(train_data,eta,w_0,b_0):
x=train_data[:,:-1]
y=train_data[:,-1]
length=train_data.shape[0]
w=w_0
b=b_0
step_num=0
while True:
i=0
while(i1
x_i=x[i].reshape((x.shape[1],1))
y_i=y[i]
if y_i*(np.dot(np.transpose(w),x_i)+b)<=0:
w+=eta*y_i*x_i
b+=eta*y_i
break
else:
i+=1
if(i==length):
break
return(w,b,step_num)
根据训练数据集和 α⃗ α → 得到 w⃗ w →
def creat_w(train_data,alpha):
x=train_data[:,:-1]
y=train_data[:,-1]
N=train_data.shape[0]
w=np.zeros((x.shape[1],1))
for i in range(0,N):
w+=alpha[i][0]*y[i]*(x[i].reshape(x[i].size,1))
return w
感知机学习算法的对偶形式
def perceptron_dual(train_data,eta,alpha_0,b_0):
x=train_data[:,:-1]
y=train_data[:,-1]
length=train_data.shape[0]
alpha=alpha_0
b=b_0
step_num=0
while_num=0
while True:
i=0
while(i1
x_i=x[i].reshape((x.shape[1],1))
y_i=y[i]
w=creat_w(train_data,alpha)
z=y_i*(np.dot(np.transpose(w),x_i)+b)
if z<=0:
alpha[i][0]+=eta
b+=eta*y_i
break
else:
i+=1
if(i==length):
break
return (alpha,b,step_num)
η参数的影响
def test_eta(data,ax,etas,w_0,alpha_0,b_0):
nums1=[]
nums2=[]
for eta in etas:
_,_,num_1=perceptron(data,eta,w_0=w_0,b_0=b_0)
_,_,num_2=perceptron_dual(data,eta=0.1,alpha_0=alpha_0,b_0=b_0)
nums1.append(num_1)
nums2.append(num_2)
ax.plot(etas,np.array(nums1),label='orignal iteraton times')
ax.plot(etas,np.array(nums2),label='dual iteraton times')
fig=plt.figure()
fig.suptitle('perceptron')
ax=fig.add_subplot(1,1,1)
ax.set_xlabel(r'$\eta$')
data=creat_data(20)
etas=np.linspace(0.01,1,num=25,endpoint=False)
w_0,b_0,alpha_0=np.ones((3,1)),0,np.zeros((data.shape[0],1))
test_eta(data,ax,etas,w_0,alpha_0,b_0)
ax.legend(loc='best',framealpha=0.5)
plt.show()