<jupyter_start><jupyter_text>Rede Neural com TensorFlow - Personagens  Importação das bibliotecas<jupyter_code>import tensorflow as tf
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
tf.__version__<jupyter_output><empty_output><jupyter_text>Carregamento da base de dados<jupyter_code>dataset = pd.read_csv('contents/personagens.csv')
dataset.shape
dataset.head()
dataset.tail()
sns.countplot(x = 'classe', data=dataset)
sns.heatmap(dataset.corr(), annot=True)
X = dataset.iloc[:, 0:6].values
X
y = dataset.iloc[:, 6].values
y
y = (y == 'Bart')
y
from sklearn.model_selection import train_test_split
X_treinamento, X_teste, y_treinamento, y_teste = train_test_split(X, y, test_size = 0.2)
X_treinamento.shape, y_treinamento.shape
X_teste.shape, y_teste.shape<jupyter_output><empty_output><jupyter_text>Construção e treinamento da rede neural<jupyter_code>(6 + 1) / 2
# (entradas + saídas) / 2
# 6 -> 4 -> 4 -> 4 -> 1
rede_neural = tf.keras.models.Sequential()
rede_neural.add(tf.keras.layers.Dense(units=4, activation='relu', input_shape=(6,)))
rede_neural.add(tf.keras.layers.Dense(units=4, activation='relu'))
rede_neural.add(tf.keras.layers.Dense(units=4, activation='relu'))
rede_neural.add(tf.keras.layers.Dense(units=1, activation='sigmoid'))
rede_neural.summary()
rede_neural.compile(optimizer='Adam', loss='binary_crossentropy', metrics = ['accuracy'])
historico = rede_neural.fit(X_treinamento, y_treinamento, epochs=50, validation_split=0.1)<jupyter_output>Epoch 1/50
7/7 [==============================] - 0s 21ms/step - loss: 1.7763 - accuracy: 0.3524 - val_loss: 1.4644 - val_accuracy: 0.4167
Epoch 2/50
7/7 [==============================] - 0s 3ms/step - loss: 1.6354 - accuracy: 0.3333 - val_loss: 1.3618 - val_accuracy: 0.4167
Epoch 3/50
7/7 [==============================] - 0s 3ms/step - loss: 1.5059 - accuracy: 0.3048 - val_loss: 1.2634 - val_accuracy: 0.3333
Epoch 4/50
7/7 [==============================] - 0s 3ms/step - loss: 1.3822 - accuracy: 0.2905 - val_loss: 1.1728 - val_accuracy: 0.3333
Epoch 5/50
7/7 [==============================] - 0s 4ms/step - loss: 1.2719 - accuracy: 0.3048 - val_loss: 1.0894 - val_accuracy: 0.2917
Epoch 6/50
7/7 [==============================] - 0s 4ms/step - loss: 1.1623 - accuracy: 0.3238 - val_loss: 1.0155 - val_accuracy: 0.2917
Epoch 7/50
7/7 [==============================] - 0s 3ms/step - loss: 1.0664 - accuracy: 0.3286 - val_loss: 0.9485 - val_accuracy: 0.2917
Epoch 8/50
7/7 [=================[...]<jupyter_text>Avaliação da rede neural<jupyter_code>historico.history.keys()
plt.plot(historico.history['val_loss'])
plt.plot(historico.history['val_accuracy'])
previsoes = rede_neural.predict(X_teste)
previsoes
# 1 True - Bart
# 0 False - Homer
previsoes = (previsoes > 0.5)
previsoes
y_teste
from sklearn.metrics import accuracy_score
accuracy_score(previsoes, y_teste)
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_teste, previsoes)
cm
sns.heatmap(cm, annot=True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20527182866556837, 'fraction_numerical': 0.07314662273476112, 'mean_word_length': 5.6, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '223895', 'license': 'MIT', 'n_tokens_mistral': 1323, 'n_tokens_neox': 1152, 'n_words': 234, 'path': 'aulas/rede_neural.ipynb', 'repo_name': 'Biguelini/Curso-IA-Python', 'size': '3035'}