**Perceptron** is a linear classifier used to classify binary images like classifying images of** cats or dogs**.

A Perceptron has 4 parts:

**Input values or One input layer****Weights and Bias****Net sum****Activation Function**

[the_ad id=”5296″]

**The Activation function of Perceptron:**

**1 if : y_in > theta**

**0 if : -theta <=y_in <= theta**

**-1 : if y_in <= theta**

- Theta is decided by us.

**Weight Updation rule:**

**weight=weight+learning_rate*t*xi**

**Learning rate**: At how much rate the weight get the update. If learning rate too low or too high it will cause errors in the output or it can lead to overfitting.

**How it works:**

- All the input
**x**gets multiplied with the respected**w**which is weight. - Add all the multiplied value and call it
**Weighted Sum.** - Pass the
**Weighted Sum**to the activation function and calculate the y_in accordingly.

[the_ad id=”5296″]

**Code:**

import numpy as np class Perceptron(object): def __init__(self,eta=0.01,n_iter=20): self.eta=eta self.n_iter=n_iter def fit(self,X,y): self.w_=np.zeros(1+X.shape[1]) k=1 for _ in range(self.n_iter): print("iteration: ",k) flag=0 for xi,target in zip(X,y): y_predict=[self.predict(xi)] error=target-self.predict(xi) if error!=0: flag=1 update=self.eta*target self.w_[1:]+=update*xi self.w_[0]+=update print(self.w_) k+=1 if flag==0: break return self def net_input(self,X): """Calculate new input""" y=np.dot(X,self.w_[1:]+self.w_[0]) return y def predict(self,X): return np.where(self.net_input(X)>0.0,1,-1) X=np.array([[1,1],[-1,1],[1,-1],[-1,-1]]) print('Input : \n',X) y=np.array([1,-1,-1,-1]) ppn=Perceptron(eta=1,n_iter=10) ppn.fit(X,y) print('Output : \n',ppn.predict(X))

[the_ad id=”7177″]

## Comments

Loading…