InfoClusters
Information at sight.

# What is Perceptron in Machine Learning? With code

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

A Perceptron has 4 parts:

1. Input values or One input layer
2. Weights and Bias
3. Net sum
4. Activation Function

## 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:

1. All the input gets multiplied with the respected which is weight.
2. Add all the multiplied value and call it Weighted Sum.
3. Pass the Weighted Sum to the activation function and calculate the y_in accordingly.

## 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))```

Get real time updates directly on you device, subscribe now.