#### 书

《统计学习基础》（The Elements of Statistical Learning），第 4.5.1 节（https://web.stanford.edu/~hastie/Papers/ESLII.pdf）

《深入理解机器学习：从原理到算法》，第 21.4 节（https://www.cs.huji.ac.il/~shais/UnderstandingMachineLearning/understanding-machine-learning-theory-algorithms.pdf）

#### 博客

Jason Brownlee 写的《如何用 Python 从零开始实现感知器算法》（https://machinelearningmastery.com/implement-perceptron-algorithm-scratch-python/）

Sebastian Raschka 写的《单层神经网络和梯度下降》（https://sebastianraschka.com/Articles/2015_singlelayer_neurons.html）

#### 从简单的例子开始

# Importing libraries
# NAND Gate
# Note: x0 is a dummy variable for the bias term
#     x0  x1  x2
x = [[1., 0., 0.],
[1., 0., 1.],
[1., 1., 0.],
[1., 1., 1.]]
y =[1.,
1.,
1.,
0.]

#### 1. 初始化权重

# Initialize the weights
import numpy as np
w = np.zeros(len(x[0]))

Out:
[ 0.  0.  0.]

#### 2. 将权重和输入相乘并对其求和

# Dot Product
f = np.dot(w, x[0])
print f

Out:
0.0

#### 3. 与阈值相比较

# Activation Function
z = 0.0
if f > z:
yhat = 1.
else:
yhat = 0.
print yhat

Out:
0.0

#### 4. 更新权重

# Update the weights
eta = 0.1
w[0] = w[0] + eta*(y[0] - yhat)*x[0][0]
w[1] = w[1] + eta*(y[0] - yhat)*x[0][1]
w[2] = w[2] + eta*(y[0] - yhat)*x[0][2]
print w

Out:
[ 0.1  0.   0. ]

#### 5. 重复

import numpy as np

# Perceptron function
def <mark data-type="technologies" data-id="f9849d6c-6262-4c1f-8f42-6d976be17161">perceptron</mark>(x, y, z, eta, t):
'''
Input Parameters:
x: data set of input features
y: actual outputs
z: activation function threshold
eta: learning rate
t: number of <mark data-type="technologies" data-id="1f2c00e7-f2e0-461c-ad70-eca3f91cdd65">iteration</mark>s
'''
# initializing the weights
w = np.zeros(len(x[0]))
n = 0
# initializing additional <mark data-type="technologies" data-id="2e982b73-88e2-41e8-a430-f7ae5a9af4bf">parameter</mark>s to compute sum-of-squared errors
yhat_vec = np.ones(len(y))     # vector for predictions
errors = np.ones(len(y))       # vector for errors (actual - predictions)
J = []                         # vector for the SSE cost function
while n < t: for i in xrange(0, len(x)): # dot product f = np.dot(x[i], w) # activation function if f >= z:
yhat = 1.
else:
yhat = 0.
yhat_vec[i] = yhat
# updating the weights
for j in xrange(0, len(w)):
w[j] = w[j] + eta*(y[i]-yhat)*x[i][j]
n += 1
# computing the sum-of-squared errors
for i in xrange(0,len(y)):
errors[i] = (y[i]-yhat_vec[i])**2
J.append(0.5*np.sum(errors))
return w, J

#     x0  x1  x2
x = [[1., 0., 0.],
[1., 0., 1.],
[1., 1., 0.],
[1., 1., 1.]]
y =[1.,
1.,
1.,
0.]
z = 0.0
eta = 0.1
t = 50
print "The weights are:"
print <mark data-type="technologies" data-id="f9849d6c-6262-4c1f-8f42-6d976be17161">perceptron</mark>(x, y, z, eta, t)[0]
print "The errors are:"
print <mark data-type="technologies" data-id="f9849d6c-6262-4c1f-8f42-6d976be17161">perceptron</mark>(x, y, z, eta, t)[0]

Out:
The weights are:
[ 0.2 -0.2 -0.1]
The errors are:
[0.5, 1.5, 1.5, 1.0, 0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

#### 1. 导入数据

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.scatter(df.values[:,1], df.values[:,2], c = df['3'], alpha=0.8)

text

#### 2. 将数据分割成训练集 / 测试集

df = df.values
np.random.seed(5)
np.random.shuffle(df)

train = df[0:int(0.7*len(df))]
test = df[int(0.7*len(df)):int(len(df))]

x_train = train[:, 0:3]
y_train = train[:, 3]
x_test = test[:, 0:3]
y_test = test[:, 3]

#### 3. 训练感知器

def <mark data-type="technologies" data-id="f9849d6c-6262-4c1f-8f42-6d976be17161">perceptron</mark>_train(x, y, z, eta, t):
'''
Input Parameters:
x: data set of input features
y: actual outputs
z: activation function threshold
eta: learning rate
t: number of <mark data-type="technologies" data-id="1f2c00e7-f2e0-461c-ad70-eca3f91cdd65">iteration</mark>s
'''
# initializing the weights
w = np.zeros(len(x[0]))
n = 0
# initializing additional <mark data-type="technologies" data-id="2e982b73-88e2-41e8-a430-f7ae5a9af4bf">parameter</mark>s to compute sum-of-squared errors
yhat_vec = np.ones(len(y))     # vector for predictions
errors = np.ones(len(y))       # vector for errors (actual - predictions)
J = []                         # vector for the SSE cost function
while n < t:          for i in xrange(0, len(x)):                                           # dot product             f = np.dot(x[i], w)                                   # activation function             if f >= z:
yhat = 1.
else:
yhat = 0.
yhat_vec[i] = yhat
# updating the weights
for j in xrange(0, len(w)):
w[j] = w[j] + eta*(y[i]-yhat)*x[i][j]
n += 1
# computing the sum-of-squared errors
for i in xrange(0,len(y)):
errors[i] = (y[i]-yhat_vec[i])**2
J.append(0.5*np.sum(errors))
return w, J
z = 0.0
eta = 0.1
t = 50
<mark data-type="technologies" data-id="f9849d6c-6262-4c1f-8f42-6d976be17161">perceptron</mark>_train(x_train, y_train, z, eta, t)

w = <mark data-type="technologies" data-id="f9849d6c-6262-4c1f-8f42-6d976be17161">perceptron</mark>_train(x_train, y_train, z, eta, t)[0]
J = <mark data-type="technologies" data-id="f9849d6c-6262-4c1f-8f42-6d976be17161">perceptron</mark>_train(x_train, y_train, z, eta, t)[1]
print w
print J

Out:
[-0.5        -0.29850122  0.35054929]
[4.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

#### 4. 测试感知器

from sklearn.metrics import accuracy_score
w = <mark data-type="technologies" data-id="f9849d6c-6262-4c1f-8f42-6d976be17161">perceptron</mark>_train(x_train, y_train, z, eta, t)[0]
def <mark data-type="technologies" data-id="f9849d6c-6262-4c1f-8f42-6d976be17161">perceptron</mark>_test(x, w, z, eta, t):
y_pred = []
for i in xrange(0, len(x-1)):
f = np.dot(x[i], w)
# activation function
if f > z:
yhat = 1
else:
yhat = 0
y_pred.append(yhat)
return y_pred
y_pred = <mark data-type="technologies" data-id="f9849d6c-6262-4c1f-8f42-6d976be17161">perceptron</mark>_test(x_test, w, z, eta, t)
print "The accuracy score is:"
print accuracy_score(y_test, y_pred)

#### 5. 和 scikit-learn感知器进行比较

from sklearn.linear_model import Perceptron
# training the sklearn Perceptron
clf = Perceptron(random_state=None, eta0=0.1, shuffle=False, fit_intercept=False)
clf.fit(x_train, y_train)
y_predict = clf.predict(x_test)

Out:
sklearn weights:
[-0.5        -0.29850122  0.35054929]
my <mark data-type="technologies" data-id="f9849d6c-6262-4c1f-8f42-6d976be17161">perceptron</mark> weights:
[-0.5        -0.29850122  0.35054929]

scikit-learn 模型中的权重和我们模型的权重完全相同。这意味着我们的模型可以正确地工作，这是个好消息。

#### 写下你的过程

GitHub 个人资料是展示你所做工作的一种很好的方法。