OR - Ipynb - Colab
OR - Ipynb - Colab
ipynb - Colab
import numpy as np
def perceptron(x,y,learning_rate=0.01,epoch=1000):
weight=np.zeros(x.shape[1])
bias=0
for e in range(epoch):
for i in range(len(y)):
linear_output=np.dot(x[i],weight) + bias
y_prediction=1 if linear_output >= 0 else 0
if y_prediction!=y[i]:
update=learning_rate *(y[i]-y_prediction)
weight+=update*x[i]
bias+=update
return weight,bias
def predict(x,weight,bias):
linear_output=np.dot(x,weight) + bias
return([1 if i>=0 else 0 for i in linear_output])
x=np.array([[0,0],[0,1],[1,0],[1,1]])
y=np.array([0,1,1,1])
w,b=perceptron(x,y)
predictions=predict(x,w,b)
print("weight", w)
print("bias",b)
print("Predictions",predictions)
https://colab.research.google.com/drive/1fD9MhEpwJRdspg2JY7LNWfT3Ss4vNLrZ#printMode=true 1/1