Created
October 25, 2016 13:12
-
-
Save felipessalvatore/cb819242c32757a26bd889cc33fc2f12 to your computer and use it in GitHub Desktop.
Exercise LFD and PLA (Perceptron learning Algorithm)
This file contains hidden or 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 matplotlib.pyplot as plt | |
import numpy as np | |
import scipy as sp | |
import os | |
from sklearn.linear_model import LogisticRegression | |
from six.moves import cPickle as pickle | |
from sklearn.datasets import load_iris | |
%matplotlib inline | |
def randomize(dataset, labels): | |
permutation = np.random.permutation(labels.shape[0]) | |
shuffled_dataset = dataset[permutation] | |
shuffled_labels = labels[permutation] | |
print(permutation.shape, len(permutation)) | |
return shuffled_dataset, shuffled_labels | |
def randomize_in_place(list1,list2, init): | |
np.random.seed(seed=init) | |
np.random.shuffle(list1) | |
np.random.seed(seed=init) | |
np.random.shuffle(list2) | |
def sign(x): | |
if x<0: | |
return -1 | |
else: | |
return +1 | |
def h(theta,x): | |
return sign(theta.dot(x)) | |
def diffe(w,vX,vy): | |
l = [] | |
for i,e in enumerate(vX): | |
if h(w,e)!=vy[i]: | |
l.append([e,vy[i]]) | |
return l | |
def draw_line(w,vX): | |
if w[2]==0: | |
print("not defined") | |
return vX*0 | |
else: | |
return (vX*-w[1])/w[2] -(w[0]/w[2]) | |
W = np.random.random(50,)*-25 +29 | |
Z = np.random.random(50,)*246 +45 | |
R = np.random.random(50,)*-19 -22 | |
S = np.random.random(50,)*177 -90 | |
points1 = [[i,e] for i,e in zip(W,Z)] | |
points2 = [[i,e] for i,e in zip(R,S)] | |
qX = np.array(points1 + points2) | |
X = np.array(np.insert(qX, 0, 1, axis=1)) | |
y = np.array([1]*50 +[-1]*50) | |
randomize_in_place(X,y,0) | |
# B x1 x2 | |
wg = np.array([-40,9,1]) | |
grafX = np.arange(-100,100,0.1) | |
grafY = draw_line(wg,grafX) | |
plt.plot(grafX,grafY,W,Z,'o',R,S,'x') | |
plt.show() | |
print(len(diffe(wg,X,y))) | |
# B x1 x2 | |
w0 = np.array([20,3.4,1]) | |
grafY = draw_line(w0,grafX) | |
plt.plot(grafX,grafY,W,Z,'o',R,S,'x') | |
plt.show() | |
print(len(diffe(w0,X,y))) | |
# B x1 x2 | |
w1 = np.array([0,0,0]) | |
grafY = draw_line(w1,grafX) | |
plt.plot(grafX,grafY,W,Z,'o',R,S,'x') | |
plt.show() | |
def PLA(w,vX,vy): | |
while diffe(w,vX,vy)!=[]: | |
w = w + (diffe(w,vX,vy)[0][1])*(diffe(w,vX,vy)[0][0]) | |
grafY = draw_line(w,grafX) | |
plt.plot(grafX,grafY,W,Z,'o',R,S,'x') | |
plt.show() | |
return w | |
print(PLA(w1,X,y)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment