运行环境:win10 64位 py 3.6 pycharm 2018.1.1
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 create_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 #沿x轴旋转45度
new_x_13 = x_12*np.sqrt(2)/2 + x_13*np.sqrt(2)/2 #沿x轴旋转45度
new_x_22 = x_22*np.sqrt(2)/2 - x_23*np.sqrt(2)/2 #沿x轴旋转45度
new_x_23 = x_22*np.sqrt(2)/2 + x_23*np.sqrt(2)/2 #沿x轴旋转45度
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
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 create_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 #沿x轴旋转45度
new_x_13 = x_12*np.sqrt(2)/2 + x_13*np.sqrt(2)/2 #沿x轴旋转45度
new_x_22 = x_22*np.sqrt(2)/2 - x_23*np.sqrt(2)/2 #沿x轴旋转45度
new_x_23 = x_22*np.sqrt(2)/2 + x_23*np.sqrt(2)/2 #沿x轴旋转45度
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 plot_samples(ax,samples):
Y = samples[:, -1]
position_p = Y == 1
position_m = Y == -1
print(position_p)
print(position_m)
ax.scatter(samples[position_p, 0],samples[position_p,1],samples[position_p,2],marker='+',label='+',color='b')
ax.scatter(samples[position_m, 0],samples[position_m,1],samples[position_m,2],marker='^',label='-',color='y')
fig = plt.figure()
ax = Axes3D(fig)
data = create_data(100)
plot_samples(ax,data)
ax.legend(loc='best')
plt.show()
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
print('w_0')
print(w)
b = b_0
print('b')
print(b)
step_num = 0
while True:
i = 0
while(i
def creat_hyperplane(x,y,w,b):
return (-w[0][0]*x - w[1][0]*y - b)/w[2][0]
data = create_data(100)
eta, w_0, b_0 = 0.1, np.ones((3,1),dtype=float),1
w, b, num = perceptron(data, eta ,w_0,b_0)
fig = plt.figure()
plt.suptitle('perceptron')
ax = Axes3D(fig)
plot_samples(ax, data)
x = np.linspace(-30,100,100)
y = np.linspace(-30,100,100)
x,y = np.meshgrid(x, y)
z = creat_hyperplane(x,y,w,b)
ax.plot_surface(x,y,z,rstride=1,cstride=1,color='g',alpha=0.2)
ax.legend(loc='best')
plt.show()