Perceptron is a learning algorithm of machine learning used as a binary classifier,which is used to classify whether a input belongs to a particular group or not.It is a type of linear classifier in which prediction is based on linear predictor function combine to set of weight with feature vector.
Concept behind the Algorithm:-
We define a activation function ϕ(z) that takes linear combination of certain input x and corresponding weight vector w,where z is the so called net input z.
z= + bias = w^T.x
=> ϕ(z) =1 if z >=0 else -1 otherwise.
In perceptron,in each iteration the weight are updated using:
w = w + learning_rate * (expected – predicted) * x
following is the diagram of working of ϕ(z):-
perceptron rule can be summarized in following steps:
1.Initialize the weights to 0 or small random number.
2.For each training smaple perform the following steps:-
a)compute the output value .
b)Update the weights.
Here, the output value is the class label predicted by the unit step function that we
defined earlier, and the simultaneous update of each weight in the weight vector w can be more formally written as:
The value of ,which is used to update the weight ,is calculated by the perceptron learning rule:
Here is the learning rate which generally lie between 0.0 and 1.0. is the true class label of is the predicted class label.It is important to note that all weight in the weight vector are being updated simultaneously.
It is important to note that the convergence of the perceptron is only guaranteed if
the two classes are linearly separable and the learning rate is sufficiently small. If the
two classes can’t be separated by a linear decision boundary, we can set a maximum
number of passes over the training dataset (epochs) and/or a threshold for the
number of tolerated misclassifications—the perceptron would never stop updating
the weights otherwise.
Lets summarize the whole thing that we discuss above in the form of figure:-
I’m a python lover,so i do most of a coding part in python.following are some function and definition which i defined in perceptron class:
eta : learning rate
n_iter : no of passes over the training data
w : weights after fitting
error = no. of misclassification in every pass
defining the fit function
w = np.zeros(1+X.shape)
errors = 
for _ in range(n_iter):
error = 0
for xi,target in zip(X,y):
update = eta*(target-self.predict(xi))
w[1:] += update*xi
w += update
errors += int(update != 0.0)
“””Calculate net input”””
“””Return class label after unit step”””
return np.where(net_input(X) >=0.0,1,-1)
Using the above definition in perceptron class we can make a object of perceptron model.
Now lets train the perceptron model on iris data sets.
Iris data sets consists of 3 different types of irises’ (Setosa, Versicolour, and Virginica) petal and sepal length, stored in a 150×4 numpy.ndarray
The rows being the samples and the columns being: Sepal Length, Sepal Width, Petal Length and Petal Width.
a)setosa b)versicolor c)virginica
we will try to classify any one of flower with the rest.
lets visulize the whole dataset in scatter plot.
considering sepal lenght and petal length we can classify setosa and versicolor from above diagram.
following is the scatter plot taking petal and sepal lenght as feature:-
Taking eta = 0.01 and n_iter = 10,lets find out the number of missclassification in each iteration:-
We can see from above graph that after the sixth iteration,graph started converging towards classifying the training datasets perfectly.
following is how graph changed after 5th iteration:-
for the actual python code for above discussed things,click below:-