-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

Machine Learning for OpenCV
By :

Perceptrons are easy enough to be implemented from scratch. We can mimic the typical OpenCV or scikit-learn implementation of a classifier by creating a Perceptron
object. This will allow us to initialize new perceptron objects that can learn from data via a fit
method and make predictions via a separate predict
method.
When we initialize a new perceptron object, we want to pass a learning rate (lr
, or η in the previous section) and the number of iterations after which the algorithm should terminate (n_iter
):
In [1]: import numpy as np In [2]: class Perceptron(object): ... def __init__(self, lr=0.01, n_iter=10): ... self.lr = lr ... self.n_iter = n_iter ...
The fit
method is where most of the work is done. This method should take as input some data samples (X
) and their associated target labels (y
). We will then create an array of weights (self.weights
), one for each feature (X.shape[1]
), initialized to zero. For convenience, we will keep the bias...