Skip to content

Instantly share code, notes, and snippets.

@ankitmishra88
Created March 24, 2020 21:38
Show Gist options
  • Save ankitmishra88/41c19d41ddc7f5a4a435e10801c49e6c to your computer and use it in GitHub Desktop.
Save ankitmishra88/41c19d41ddc7f5a4a435e10801c49e6c 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
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]
#print(theta)
return (list(theta),theta0)
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