Created
March 3, 2016 02:14
-
-
Save pabloem/7c35622dac833fae6504 to your computer and use it in GitHub Desktop.
Code for perceptron
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
# We need this to initialize python packages |
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 random | |
class Perceptron: | |
def __init__(self,input_number,step_size=0.1): | |
self._ins = input_number | |
self._w = [random.random() for _ in range(input_number)] | |
self._eta = step_size | |
def predict(self,inputs): | |
weighted_average = sum(w*elm for w,elm in zip(self._w,inputs)) | |
if weighted_average > 0: | |
return 1 | |
return 0 | |
def train(self,inputs,ex_output): | |
output = self.predict(inputs) | |
error = ex_output - output | |
if error != 0: | |
self._w = [w+self._eta*error*x for w,x in zip(self._w,inputs)] | |
return error |
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
#!/usr/bin/env python | |
from perceptron import Perceptron | |
## Datos de hombres y mujeres | |
input_data = [[170,56,1], # Mujer de 1.70m y 56kg | |
[172,63,0],# Hombre de 1.72m y 63kg | |
[160,50,1], # Mujer de 1.60m y 50kg | |
[170,63,0], # Hombre de 1.70m y 63kg | |
[174,66,0],# Hombre de 1.74m y 66kg | |
[158,55,1],# Mujer de 1.58m y 55kg | |
[183,80,0],# Hombre de 1.83m y 80kg | |
[182,70,0],# Hombre de 1.82m y 70kg | |
[165,54,1]]# Mujer de 1.65m y 54kg | |
## Creamos el perceptron | |
pr = Perceptron(3,0.1) # Perceptron con 3 entradas | |
weights = [] # Lista con los pesos | |
errors = [] # Lista con los errores | |
## Fase de entrenamiento | |
for _ in range(100): | |
# Vamos a entrenarlo varias veces sobre los mismos datos | |
# para que los 'pesos' converjan | |
for person in input_data: | |
output = person[-1] | |
inp = [1] + person[0:-1] # Agregamos un uno por default | |
weights.append(pr._w) | |
err = pr.train(inp,output) | |
errors.append(err) | |
h = float(raw_input("Introduce tu estatura en centimetros.- ")) | |
w = float(raw_input("Introduce tu peso en kilogramos.- ")) | |
if pr.predict([1,h,2]) == 1: print "Mujer" | |
else: print "Hombre" | |
#print """ | |
#Nota: El resultado puede estar incorrecto. | |
#Esto puede ser debido a sesgo en la muestra, o porque es imposible separar | |
#a hombres y mujeres perfectamente basados unicamente en talla y peso.""" | |
## Fase de graficacion | |
import imp | |
can_plot = True | |
try: | |
imp.find_module('matplotlib') | |
except: | |
can_plot = False | |
if not can_plot: | |
print "No es posible graficar los resultados porque no tienes matplotlib" | |
sys.exit(0) | |
pass | |
import matplotlib.pyplot as plt | |
plt.plot(errors) | |
plt.show() |
hop
commented
Oct 22, 2017
en esta parte del codigo:
def predict(self,inputs):
weighted_average = sum(w*elm for w,elm in zip(self._w,inputs))
que es lo siguiente? no entiendo, te agradeceria si me respondieras
zip(self._w,inputs)
es que quiero tomar este codigo tuyo para usarlo como base pero tengo que transcribirlo a php y ese codigo que te mencione arriba no se que ase. osea la suma y las multiplicaciones si solo la parte donde dice zip
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment