<jupyter_start><jupyter_text>Classificador de cores (sensor TCS230) usando MLPAbaixo temos o código de treinamento da rede neural que classifica cores. Como podemos ver pela matriz de confusão, o resultado é satisfatório.<jupyter_code># Importando as bibliotecas e iniciando as variaveis com o caminho do dataset
import numpy as np
import os
import tarfile
from six.moves import urllib
import csv

FILE_TO_DOWNLOAD =  "objetosECores.csv"
DOWNLOAD_ROOT = "https://raw.githubusercontent.com/rodolfostark/ColorSensorMachineLearning/"
DATA_PATH = "master/"
DATA_URL = DOWNLOAD_ROOT + DATA_PATH + FILE_TO_DOWNLOAD
# Função que faz o download dos dados
def fetch_data(data_url=DATA_URL, data_path=DATA_PATH, file_to_download=FILE_TO_DOWNLOAD):
  if not os.path.isdir(data_path):
    os.makedirs(data_path)
  urllib.request.urlretrieve(data_url, data_path+file_to_download)
  
fetch_data()
!ls master
# Prepadando os dados
leitura = csv.reader(open('master/objetosECores.csv','r'))
leitura = list(leitura)

objetos_coloridos = list()
y = list()

for l in range(1, len(leitura)):
  y.append(int(leitura[l][3]) + 1)

for i in range(1, len(leitura)):
  objetos_coloridos.append(leitura[i])
  
for j in range(0, len(objetos_coloridos)):
  objetos_coloridos[j].pop()
  for k in range(0, len(objetos_coloridos[j])):
    objetos_coloridos[j][k] = float(objetos_coloridos[j][k]) 
    
Y = np.array(y)
X = np.array(objetos_coloridos)
# Separando subconjuntos para treino e teste
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size = 0.25, random_state = 0)
# Importado classificador
from sklearn.neural_network import MLPClassifier
clf = MLPClassifier(solver='sgd', alpha=1e-5, hidden_layer_sizes=(15),activation = 'relu' ,random_state=1, verbose = True)
clf.fit(X_train, y_train)
clf.predict(X_test)
print(clf.score(X_train,y_train))
#importando matrix de confusão
from sklearn.metrics import confusion_matrix

y_pred = clf.predict(X_test)
cm = confusion_matrix(y_test, y_pred)

print(cm)<jupyter_output>[[4 0 0]
 [0 6 0]
 [0 0 4]]
{'fraction_non_alphanumeric': 0.10594439117929051, 'fraction_numerical': 0.012943432406519654, 'mean_word_length': 6.246527777777778, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '47142', 'license': 'MIT', 'n_tokens_mistral': 820, 'n_tokens_neox': 800, 'n_words': 186, 'path': 'machinelearningcrashcourse/My_MLPColorSensorClassifier.ipynb', 'repo_name': 'mtxslv/AprendendoMachineLearning', 'size': '2086'}