<jupyter_start><jupyter_text><NAME> Introdução ao Reconhecimento de Padrões (2021-1) L05c - Classificador centroid mais próximo - S01L05c - 16/10 - Atividade   **Importando as bibliotecas necessárias**<jupyter_code>from sklearn.datasets import load_breast_cancer
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
import numpy as np
import pandas as pd<jupyter_output><empty_output><jupyter_text>**Leitura do dataset**<jupyter_code>data = load_breast_cancer()
df = pd.DataFrame(data.data, columns = data.feature_names)
df['target'] = data.target 
df.head ()<jupyter_output><empty_output><jupyter_text>**Exibindo informações importante sobre os dados***   569 rows com valores diferentes de nulo para cada uma das 31 columns (0, 30)*   Todos os valores são numéricos<jupyter_code>df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 569 entries, 0 to 568
Data columns (total 31 columns):
 #   Column                   Non-Null Count  Dtype  
---  ------                   --------------  -----  
 0   mean radius              569 non-null    float64
 1   mean texture             569 non-null    float64
 2   mean perimeter           569 non-null    float64
 3   mean area                569 non-null    float64
 4   mean smoothness          569 non-null    float64
 5   mean compactness         569 non-null    float64
 6   mean concavity           569 non-null    float64
 7   mean concave points      569 non-null    float64
 8   mean symmetry            569 non-null    float64
 9   mean fractal dimension   569 non-null    float64
 10  radius error             569 non-null    float64
 11  texture error            569 non-null    float64
 12  perimeter error          569 non-null    float64
 13  area error               569 non-null    float64
 14  smoothness error         5[...]<jupyter_text>**Separando os dados*** **x** representam os dados de origem (informações necessárias para a classificação)* **y** os dados de destino (resultados)* Divisão entre **test** e **train** realizada pelo sklearn<jupyter_code>X = data.data
y = data.target

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, shuffle=True, random_state=123, stratify=y)
np.bincount(y_train)<jupyter_output><empty_output><jupyter_text>**Realizando treinamento**<jupyter_code>scaler = StandardScaler()
scaler.fit(X_train)
X_train_std = scaler.transform(X_train)
X_test_std = scaler.transform(X_test)
X_train_std

knn_model = KNeighborsClassifier(n_neighbors=3)
knn_model.fit(X_train_std, y_train)<jupyter_output><empty_output><jupyter_text>**Previsão do conjunto de teste**<jupyter_code>y_pred = knn_model.predict(X_test_std)
print(y_pred)
print(y_test)<jupyter_output>[0 0 1 1 1 0 1 0 1 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 0 1 1 1 1 0 1 1 0 1 1 0 1
 1 1 0 1 0 1 1 0 1 1 0 1 1 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 1 0 1 1 0 1 0 0 1
 1 0 1 0 0 0 1 0 1 1 0 1 0 1 1 0 0 1 1 0 0 1 0 1 1 0 1 1 1 0 1 1 1 0 0 1 1
 1 1 1]
[0 0 1 1 1 0 1 0 1 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 0 1 1 1 1 0 1 1 0 1 1 0 1
 1 0 0 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 0 0 0 0 0 1 1 1 0 0 1 0 1 1 0 1 0 0 1
 1 0 1 0 0 0 1 0 1 1 0 1 0 1 1 0 0 1 1 0 0 1 0 1 1 0 1 1 1 0 1 1 1 0 0 1 1
 1 1 1]<jupyter_text>**Exibindo score da previsão**<jupyter_code>knn_model.score(X_test, y_test)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07972732661529341, 'fraction_numerical': 0.10580912863070539, 'mean_word_length': 2.910776361529548, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '194852', 'license': 'MIT', 'n_tokens_mistral': 1537, 'n_tokens_neox': 1217, 'n_words': 475, 'path': 'breast_cancer.ipynb', 'repo_name': 'BrendaCopatti/pattern_recognition', 'size': '3390'}