Skip to content

Instantly share code, notes, and snippets.

@ankitmishra88
Last active March 25, 2020 04:22
Show Gist options
  • Save ankitmishra88/7e9f61dde78577fe73e15dffc04fc320 to your computer and use it in GitHub Desktop.
Save ankitmishra88/7e9f61dde78577fe73e15dffc04fc320 to your computer and use it in GitHub Desktop.
import numpy as np
def perceptron(features,labels):
theta=np.array([0]*len(features[0]))
theta0=0
t=10
count=1
sum_theta=theta
sum_theta_0=theta0
while(t):
t=t-1
for i in range(len(features)):
if (np.dot(features[i],theta)+theta0)*labels[i]<=0:
#print('mistake')
theta=theta+np.multiply(labels[i],features[i])
theta0=theta0+labels[i]
sum_theta=sum_theta+theta
sum_theta_0=sum_theta_0_theta_0
count+=1
#print(theta)
#We return Average theta value and Average theta0 value
return (list(theta/count),theta0/count)
if __name__=="__main__":
x=[[1,2],[2,3],[3,4],[4,5]]
features=x
y=[1,1,-1,-1]
theta,theta0=perceptron(features,y)
print('theta={},theta0={}'.format(theta,theta0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment