Perceptron Numpy
Perceptron Numpy
Implementation of the classic Perceptron by Frank Rosenblatt for binary classification (here: 0/1 class labels) in NumPy
Imports
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
X_train.std(axis=0)
array([1., 1.])
for i in range(y.shape[0]):
errors = self.backward(x[i].reshape(1, self.num_features), y[i]).reshape(-1)
self.weights += (errors * x[i]).reshape(self.num_features, 1)
self.bias += errors
print('Model parameters:\n\n')
print(' Weights: %s\n' % ppn.weights)
print(' Bias: %s\n' % ppn.bias)
Model parameters:
Weights: [[1.27340847]
[1.34642288]]
Bias: [-1.]
##########################
### 2D Decision Boundary
##########################
w, b = ppn.weights, ppn.bias
x0_min = -2
x1_min = ( (-(w[0] * x0_min) - b[0])
/ w[1] )
x0_max = 2
x1_max = ( (-(w[0] * x0_max) - b[0])
/ w[1] )
# x0*w0 + x1*w1 + b = 0
# x1 = (-x0*w0 - b) / w1
ax[1].legend(loc='upper left')
plt.show()
Loading [MathJax]/jax/output/CommonHTML/fonts/TeX/fontdata.js