from numpy import *
import numpy as np
import matplotlib . pyplot as plt
import math
# Load the data set
data_l = loadtxt('tutorial1/linear.data')
data_nl = loadtxt('tutorial1/nonlinear.data')
data_l
data_nl
# Separate the input from the output
X = data_l[:,:-1]
Y = data_l[:,-1]
N, d = X.shape
# Separate the positive from the negative class positive_class = X[Y==1. , :]
positive_class = X[Y==1., :]
negative_class = X[Y==-1., :]
positive_class
positive_class.shape
#print(X)
print(Y)
positive_class
#linearly separable.
x_p = positive_class[:,0]
y_p = positive_class[:,1]
x_n = negative_class[:,0]
y_n = negative_class[:,1]
plt.plot(x_p,y_p, '.b')
plt.plot(x_n,y_n, '.r')
plt.show()
len(X)
print(X[1])
len(X[0])
w = np.zeros(len(X[0]))
b = 0
n = 0.1
def f(x):
return -w[0]/w[1]*x + b
it = 0
while it < 15:
for i in range(len(X)):
pred = 0
if np.dot(w,X[i]) + b < 0 and Y[i] < 0:
sign = -1
elif np.dot(w,X[i]) + b > 0 and Y[i] > 0:
sign = 1
else: sign = 0
if np.dot(w,X[i]) + b >= 0:
pred = 1
else: pred = -1
#print(n*(Y[i] - sign)*X[i])
b = b + n*(Y[i] - sign)
w = w + n*(Y[i] - sign)*X[i]
it += 1
#print("W:",
print(b)
print(w)
x = np.linspace(0,1,1000,endpoint=False)
y = -(w[0]/w[1])*x - b/w[1]
plt.plot(x,y,'.g',markersize=0.2)
plt.plot(x_p,y_p, '.b')
plt.plot(x_n,y_n, '.r')
plt.show()
print(b)
def error_rate(w, b, X, Y):
miss = 0
for i in range(len(X)):
if np.dot(w,X[i]) + b < 0 and Y[i] > 0:
miss += 1
elif np.dot(w,X[i]) + b >= 0 and Y[i] < 0:
miss += 1
return miss/len(X)
def func_marg(w, b, X):
min_marg = Y[0]*(np.dot(w,X[0]) + b)
for i in range(1,len(X)):
actual_marg = Y[i]*(np.dot(w,X[i]) + b)
if actual_marg < min_marg:
min_marg = actual_marg
return min_marg
error_rate(w, b, X, Y)
def perceptron(X, Y, b, n):
w = np.zeros(len(X[0]))
nb_epoch = 0
print(w, b, n)
while func_marg(w, b, X) <= 0:
for i in range(len(X)):
pred = 0
if np.dot(w,X[i]) + b < 0 and Y[i] < 0:
sign = -1
elif np.dot(w,X[i]) + b > 0 and Y[i] > 0:
sign = 1
else: sign = 0
if np.dot(w,X[i]) + b >= 0:
pred = 1
else: pred = -1
b = b + n*(Y[i] - pred)
w = w + n*(Y[i] - sign)*X[i]
nb_epoch += 1
print("epoch nb:", nb_epoch)
print("we needed", nb_epoch,"epoch(s)")
return nb_epoch
perceptron(X, Y, 0, 0.1)
x = np.linspace(0,1,1000,endpoint=False)
y = -(w[0]/w[1])*x - b/w[1]
plt.plot(x,y,'.g',markersize=0.2)
plt.plot(x_p,y_p, '.b')
plt.plot(x_n,y_n, '.r')
plt.show()
def best_learning_rate(range):
for i in range:
print("learning rate is :", i)
#nb_epoch =
perceptron(X, Y, 0, i)
print("\n")
#print("we needed", nb_epoch, "epoch(s)")
best_learning_rate([0.000000001])
w_1 = np.zeros(len(X[0]))
nb_epoch = 0
n_1 = 0.000000000000000000000000000000000000000000001
b_1 = 0
while func_marg(w_1, b_1, X) <= 0:
for i in range(len(X)):
pred_1 = 0
if np.dot(w_1,X[i]) + b_1 < 0 and Y[i] < 0:
sign_1 = -1
elif np.dot(w_1,X[i]) + b_1 > 0 and Y[i] > 0:
sign_1 = 1
else: sign_1 = 0
if np.dot(w_1,X[i]) + b_1 >= 0:
pred_1 = 1
else: pred_1 = -1
b_1 = b_1 + n_1*(Y[i] - pred_1)
w_1 = w_1 + n_1*(Y[i] - sign_1)*X[i]
nb_epoch += 1
print(func_marg(w_1, b_1, X))
print("epoch nb:", nb_epoch)
print("we needed", nb_epoch,"epoch(s)")
X_nl = data_nl[:,:-1]
Y_nl = data_nl[:,-1]
def perceptron_non_lin_sep(X, Y, b, n):
w = np.zeros(len(X[0]))
#print(error_rate(w, b, X, Y))
nb_epoch = 0
while error_rate(w, b, X, Y) > 0.1:
print(error_rate(w, b, X, Y))
for i in range(len(X)):
pred = 0
if np.dot(w,X[i]) + b < 0 and Y[i] < 0:
sign = -1
elif np.dot(w,X[i]) + b > 0 and Y[i] > 0:
sign = 1
else: sign = 0
if np.dot(w,X[i]) + b >= 0:
pred = 1
else: pred = -1
b = b + n*(Y[i] - pred)
w = w + n*(Y[i] - sign)*X[i]
nb_epoch += 1
print("epoch nb:", nb_epoch)
print(error_rate(w, b, X, Y))
print("we needed", nb_epoch,"epoch(s)")
return w,b
w_nl, b_nl = perceptron_non_lin_sep(X_nl, Y_nl, 0, 0.1)
w_nl
perceptron_non_lin_sep(X, Y, 0, 0.1)
error_rate(np.zeros(2), 0, X_nl, Y_nl)
positive_class_nl = X_nl[Y_nl==1., :]
negative_class_nl = X_nl[Y_nl==-1., :]
#linearly separable.
x_p_nl = positive_class_nl[:,0]
y_p_nl = positive_class_nl[:,1]
x_n_nl = negative_class_nl[:,0]
y_n_nl = negative_class_nl[:,1]
plt.plot(x_p_nl,y_p_nl, '.b')
plt.plot(x_n_nl,y_n_nl, '.r')
plt.show()
x_nl = np.linspace(0,1,1000,endpoint=False)
y_nl = -(w_nl[0]/w_nl[1])*x_nl - b_nl/w_nl[1]
plt.plot(x_nl, y_nl,'.g',markersize=0.2)
plt.plot(x_p_nl, y_p_nl, '.b')
plt.plot(x_n_nl, y_n_nl, '.r')
plt.show()
def log_reg(X, Y, b, n):
w = np.zeros(len(X[0]))
#print(error_rate(w, b, X, Y))
nb_epoch = 0
while error_rate(w, b, X, Y) > 0.1:
print(error_rate(w, b, X, Y))
for i in range(len(X)):
pred = 0
if np.dot(w,X[i]) + b >= 0:
pred = 1
else: pred = -1
f = 1/(1+exp(-np.dot(w,X[i])))
b = b + n*(Y[i] - pred)
w = w + n*(Y[i] - )*X[i]
nb_epoch += 1
print("epoch nb:", nb_epoch)
print(error_rate(w, b, X, Y))
print("we needed", nb_epoch,"epoch(s)")
return w,b