<jupyter_start><jupyter_text>Usando un perceptron<jupyter_code>#-- Definimos el Perceptron

# Inicializamos los pesos en 0
b = 0
w_1 = 0
w_2 = 0

# Inicializamos el número de épocas y la rata de aprendizaje
n_epochs = 200 # Número de épocas
n = 0.5      # Tasa de aprendizaje

# Esta es la función nuestro perceptron encontrará al final de todas las épocas
def y(x):
 return -(b + w_1*x)/w_2

# Entrenamos el Perceptron
for epoch in range(n_epochs):
  for i,j,k in zip(x1,x2,Y):
    
    # Función de Suma
    z = i*w_1 + j*w_2 + b

    # Función de Activación
    sig_z = act(z)

    # Evaluamos la salida (output)
    if sig_z >= 0.5:
      out = 1
    if sig_z < 0.5:
      out = 0
    
    # Calculamos el error
    error = k - out

    # Actualizamos los pesos
    b = b + n*error
    w_1 += n*error*i
    w_2 += n*error*j

  print('Epoch [{}/{}], bias: {}, w1: {}, w2: {}'.format(epoch+1,n_epochs,b,w_1,w_2))

fig = plt.figure(figsize=(4,4))
plt.plot(x1[c0],x2[c0],'o', c='green', label='0')
plt.plot(x1[c1],x2[c1],'o', c='red', label='1')
plt.plot(np.sort(x1),y(np.sort(x1)),'-', c='green',)
plt.legend()
plt.show()<jupyter_output>Epoch [1/200], bias: 0.0, w1: 0.9500000000000002, w2: -0.1499999999999999
Epoch [2/200], bias: 0.0, w1: 1.9000000000000004, w2: -0.2999999999999998
Epoch [3/200], bias: 0.0, w1: 2.8500000000000005, w2: -0.44999999999999973
Epoch [4/200], bias: 0.0, w1: 3.8000000000000007, w2: -0.5999999999999996
Epoch [5/200], bias: 0.0, w1: 4.450000000000001, w2: -0.7499999999999996
Epoch [6/200], bias: -0.5, w1: 2.950000000000001, w2: -2.3999999999999995
Epoch [7/200], bias: -0.5, w1: 3.9000000000000012, w2: -2.5499999999999994
Epoch [8/200], bias: -0.5, w1: 4.850000000000001, w2: -2.699999999999999
Epoch [9/200], bias: -0.5, w1: 5.500000000000002, w2: -2.849999999999999
Epoch [10/200], bias: -1.0, w1: 4.200000000000002, w2: -4.15
Epoch [11/200], bias: -1.0, w1: 5.150000000000002, w2: -4.300000000000001
Epoch [12/200], bias: -1.0, w1: 6.100000000000002, w2: -4.450000000000001
Epoch [13/200], bias: -1.5, w1: 4.8000000000000025, w2: -5.750000000000002
Epoch [14/200], bias: -1.5, w1: 5.750000000000003, [...]<jupyter_text>Usando MLP<jupyter_code>from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

data = pd.read_csv('iris.data', names=['setl','setw','petl','petw','class'])
keys = ['setl','setw','petl','petw']

X = data[keys]
Y = np.array(data['class'])

Y[Y=='Iris-setosa'] = 0
Y[Y=='Iris-versicolor'] = 1
Y[Y=='Iris-virginica'] = 2

Y = np.array(Y, dtype=int)

X_train, X_test, Y_train, Y_test = train_test_split(X,Y)<jupyter_output><empty_output><jupyter_text>Dos Capas<jupyter_code>model = MLPClassifier(hidden_layer_sizes=(3, 3), random_state=1)
model.fit(X_train,Y_train)
Y_pred = model.predict(X_test)
accuracy_score(Y_pred,Y_test)
plt.hist(Y_pred, histtype='step', label='pred')
plt.hist(Y_test, histtype='step', label='test')
plt.legend(loc=2)
plt.show()<jupyter_output><empty_output><jupyter_text>Tres Capas<jupyter_code>model = MLPClassifier(hidden_layer_sizes=(3, 3, 3), random_state=1)
model.fit(X_train,Y_train)
Y_pred = model.predict(X_test)
accuracy_score(Y_pred,Y_test)
plt.hist(Y_pred, histtype='step', label='pred')
plt.hist(Y_test, histtype='step', label='test')
plt.legend(loc=2)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17467378410438908, 'fraction_numerical': 0.17941874258600238, 'mean_word_length': 4.917543859649123, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 26, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '596166', 'license': 'MIT', 'n_tokens_mistral': 1894, 'n_tokens_neox': 1496, 'n_words': 303, 'path': 'soluciones/Entrega3_sol.ipynb', 'repo_name': 'aherrera3/IntroDeepLearning_202102', 'size': '3372'}