Created
March 26, 2020 06:37
-
-
Save ankitmishra88/7c60fad31528f78f6aa329b9d2487492 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import math | |
import matplotlib.pyplot as plt | |
#----------Pegasos Code definition------------------------# | |
def pegasos(feature_matrix,labels,T): | |
theta=np.array([0]*feature_matrix.shape[1]) | |
theta_0=0 | |
count=0 | |
L=2 #value for lambda | |
for i in range(T): | |
for j in range(len(feature_matrix)): | |
count+=1 | |
eta=1/math.sqrt(count) #decreasing eta value as iteration increase | |
z=labels[j]*(np.dot(feature_matrix[j],theta)+theta_0) | |
if(z<=1): | |
theta=theta+eta*labels[j]*feature_matrix[j]-eta*L*theta | |
theta_0=theta_0+eta*labels[j] | |
else: | |
theta=theta-eta*L*theta | |
return theta,theta_0 | |
#---------------Ends Pegsos----------------------------------# | |
if __name__=="__main__": | |
x=np.array([[1,2],[2,3],[3,4],[4,5]]) | |
y=np.array([1,1,-1,-1]) | |
theta,theta_0=pegasos(x,y,100) | |
print("theta ={} theta_0= {}".format(theta,theta_0)) | |
#----------visualization-----------------------------# | |
pos=[[],[]] | |
neg=[[],[]] | |
for i in range(len(x)): | |
if(y[i]==1): | |
pos[0].append(x[i][0]) | |
pos[1].append(x[i][1]) | |
else: | |
neg[0].append(x[i][0]) | |
neg[1].append(x[i][1]) | |
plt.scatter(neg[0],neg[1]) | |
plt.scatter(pos[0],pos[1]) | |
print(x,y) | |
x=[i for i in range(0,5)] | |
y=[-(theta[0]*i+theta_0)/theta[1] for i in x] | |
plt.plot(x,y) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment