# # NumPy 与 神经网络

import numpy as np
def sigmoid(x):
return 1 / (1 + np.exp(-x))
# derivative of sigmoid
# sigmoid(y) * (1.0 - sigmoid(y))
# the way we use this y is already sigmoided
def dsigmoid(y):
return y * (1.0 - y)

class MLP_NeuralNetwork(object):
def __init__(self, input, hidden, output):
"""
:param input: number of input neurons
:param hidden: number of hidden neurons
:param output: number of output neurons
"""
self.input = input + 1 # add 1 for bias node
self.hidden = hidden
self.output = output
# set up array of 1s for activations
self.ai = [1.0] * self.input
self.ah = [1.0] * self.hidden
self.ao = [1.0] * self.output
# create randomized weights
self.wi = np.random.randn(self.input, self.hidden)
self.wo = np.random.randn(self.hidden, self.output)
# create arrays of 0 for changes
self.ci = np.zeros((self.input, self.hidden))
self.co = np.zeros((self.hidden, self.output))

def feedForward(self, inputs):
if len(inputs) != self.input-1:
raise ValueError('Wrong number of inputs you silly goose!')
# input activations
for i in range(self.input -1): # -1 is to avoid the bias
self.ai[i] = inputs[i]
# hidden activations
for j in range(self.hidden):
sum = 0.0
for i in range(self.input):
sum += self.ai[i] * self.wi[i][j]
self.ah[j] = sigmoid(sum)
# output activations
for k in range(self.output):
sum = 0.0
for j in range(self.hidden):
sum += self.ah[j] * self.wo[j][k]
self.ao[k] = sigmoid(sum)
return self.ao[:]

def backPropagate(self, targets, N):
"""
:param targets: y values
:param N: learning rate
:return: updated weights and current error
"""
if len(targets) != self.output:
raise ValueError('Wrong number of targets you silly goose!')
# calculate error terms for output
# the delta tell you which direction to change the weights
output_deltas = [0.0] * self.output
for k in range(self.output):
error = -(targets[k] - self.ao[k])
output_deltas[k] = dsigmoid(self.ao[k]) * error
# calculate error terms for hidden
# delta tells you which direction to change the weights
hidden_deltas = [0.0] * self.hidden
for j in range(self.hidden):
error = 0.0
for k in range(self.output):
error += output_deltas[k] * self.wo[j][k]
hidden_deltas[j] = dsigmoid(self.ah[j]) * error
# update the weights connecting hidden to output
for j in range(self.hidden):
for k in range(self.output):
change = output_deltas[k] * self.ah[j]
self.wo[j][k] -= N * change + self.co[j][k]
self.co[j][k] = change
# update the weights connecting input to hidden
for i in range(self.input):
for j in range(self.hidden):
change = hidden_deltas[j] * self.ai[i]
self.wi[i][j] -= N * change + self.ci[i][j]
self.ci[i][j] = change
# calculate error
error = 0.0
for k in range(len(targets)):
error += 0.5 * (targets[k] - self.ao[k]) ** 2
return error

def train(self, patterns, iterations = 3000, N = 0.0002):
# N: learning rate
for i in range(iterations):
error = 0.0
for p in patterns:
inputs = p[0]
targets = p[1]
self.feedForward(inputs)
error = self.backPropagate(targets, N)
if i % 500 == 0:
print('error %-.5f' % error)

def predict(self, X):
"""
return list of predictions after training algorithm
"""
predictions = []
for p in X:
predictions.append(self.feedForward(p))
return predictions

precision    recall  f1-score   support

0       0.98      0.96      0.97        49
1       0.92      0.97      0.95        36
2       1.00      1.00      1.00        43
3       0.95      0.88      0.91        41
4       0.98      1.00      0.99        47
5       0.96      1.00      0.98        46
6       1.00      1.00      1.00        47
7       0.98      0.96      0.97        46
8       0.93      0.80      0.86        49
9       1.00      0.91      0.95        46

avg / total       0.97      0.95      0.96       450