Last active
February 11, 2020 00:13
-
-
Save mariogarcia-ar/b2bec6133a366208881c4864155315f4 to your computer and use it in GitHub Desktop.
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
# -*- coding: utf-8 -*- | |
import numpy as np | |
def predict(x,theta,theta_0=0): | |
''' | |
Funcion de prediccion | |
x un vector del training example | |
theta es el vector normal al plano | |
theta_0 es el offset (no lo uso por ahora) | |
retorna el dot product o inner product | |
''' | |
return np.dot(x, theta) | |
# test predict | |
#t=[-1,-1]; x=[-1,-1]; print(predict(t,x)) | |
#t=[-1,-1]; x=[10,2]; print(predict(t,x)) | |
def run(theta,theta_0, X, Y, start=1, T=10): | |
''' | |
Funcion de ejecucion del Perceptron | |
Itera mostrando los sucesivos valores de theta | |
theta es el vector normal al plano | |
theta_0 es el offset (no lo uso por ahora) | |
X matrix con los training examples | |
Y matrix con los labels | |
T cantidad de iteraciones | |
retorna el theta optimo | |
''' | |
num_rows, num_cols = X.shape | |
print("\n\n****************************\nPerceptron by Mario :) \n****************************") | |
print(theta, "<----- Theta Inicial") | |
print("Arranco en el punto ",start) | |
t = 0 | |
total_thetas = 0 | |
# itero T veces | |
while t < T: | |
t += 1 | |
print("\n-------------------\nIteration ", t , "\n-------------------") | |
i = start - 1 # arranco por el punto que definieron, es menos 1 por el indice del array de python | |
# itero por cada punto | |
for k in range(num_rows): | |
#print("paso por punto ", i+1) | |
if Y[i] * predict( X[i] , theta ) <= 0 : | |
# actualizo theta | |
theta = np.add( theta, Y[i] * X[i] ) | |
total_thetas += 1 | |
print(theta, "<----- Nueva Theta, Nro de Actualizacion #", total_thetas, " en el punto x",i+1) | |
i += 1 | |
i %= num_rows # para que no se pase de ese | |
print("\n\n\n\n" ,"Theta Optima", theta) | |
print( " Cantidad de actualizaciones", total_thetas) | |
return theta | |
# print(theta, theta_0) | |
# ------------------------------------------------------------- | |
# 1. (a) | |
X = np.array([[-1,-1],[1,0],[-1,1.5]]) | |
Y = np.array([1,-1,1]) | |
# x1 arranca como theta inicial | |
theta = [0,0] #X[0] | |
theta_0 = 0 | |
start = 1 | |
run(theta, theta_0, X, Y, start ) | |
# x2 arranca como theta inicial | |
theta = [0,0] #X[0] | |
theta_0 = 0 | |
start = 2 | |
run(theta, theta_0, X, Y, start ) | |
# ------------------------------------------------------------- | |
# 1. (c) | |
X = np.array([[-1,-1],[1,0],[-1,10]]) | |
Y = np.array([1,-1,1]) | |
# x1 arranca como theta inicial | |
theta = [0,0] #X[0] | |
theta_0 = 0 | |
start = 1 | |
run(theta, theta_0, X, Y, start ) | |
# x2 arranca como theta inicial | |
theta = [0,0] #X[0] | |
theta_0 = 0 | |
start = 2 | |
run(theta, theta_0, X, Y, start ) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment