<jupyter_start><jupyter_text>**<NAME>** Introdução ao Reconhecimento de Padrões (2021-1) 28/11 - Atividade - Decision Trees  **Importando as bibliotecas**<jupyter_code>from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.pipeline import Pipeline
from sklearn.decomposition import PCA
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import GridSearchCV
from sklearn.tree import plot_tree
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>**Manipulando os dados**<jupyter_code>y = df['target'].values
y
X = df.iloc[:, 0:29].values
X
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, shuffle=True, random_state=123, stratify=y)
print('Labels count in y', np.bincount(y))
print('Labels count in y_train', np.bincount(y_train))
print('Labels count in y_test', np.bincount(y_test))<jupyter_output>Labels count in y [212 357]
Labels count in y_train [170 285]
Labels count in y_test [42 72]<jupyter_text>**Criando pipeline**<jupyter_code>pipe = Pipeline([
                 ('z-score', StandardScaler()),
                 ('reduce_dim', PCA(n_components=2)),
                 ('classify', DecisionTreeClassifier())
])<jupyter_output><empty_output><jupyter_text>**Realizando GridSearch**<jupyter_code>param_grid = {
    'reduce_dim__n_components': [1, 2, 3, 4],
    'classify__random_state': [2, 3, 4, 5, 6, 7],
}

grid = GridSearchCV(pipe, cv=2, n_jobs=1, param_grid=param_grid, scoring='accuracy')
grid.fit(X_train, y_train)<jupyter_output><empty_output><jupyter_text>Verificando média de score obtido<jupyter_code>grid.cv_results_['mean_test_score']<jupyter_output><empty_output><jupyter_text>Verificando melhor score e melhores parâmetros<jupyter_code>print(grid.best_score_)
print(grid.best_params_)<jupyter_output>0.9274093824870546
{'classify__random_state': 7, 'reduce_dim__n_components': 2}<jupyter_text>Atribuindo o melhor classificador encontrado e realizando predição<jupyter_code>classifier=grid.best_estimator_
y_test_pred=classifier.predict(X_test)<jupyter_output><empty_output><jupyter_text>Analise arvore de decisão> Comentei essa parte pois estava ocorrendo um problema que eu não consegui resolver, acredito que está faltando um parâmetro no pipeline, mas eu não consegui identificar qualMensagem de erro'Pipeline' object has no attribute 'tree_'<jupyter_code>#plt.figure(figsize=(20,20))

#plot_tree(classifier, filled=True, rounded=True, class_names=df.columns)<jupyter_output><empty_output><jupyter_text>Realizando teste com um dado de amostra da base selecionado<jupyter_code>print(np.shape(X_test))
print(np.shape(y_test))

X_test1 = X_test[1,:]
y_test1 = y_test[1]

print(np.shape(X_test1))
print(np.shape(y_test1))

X_test1 = np.reshape(X_test1,(1,-1))
y_test1 = np.reshape(y_test1,(1))

print(np.shape(X_test1))
print(np.shape(y_test1))
y_test1_pred = new_classifier.predict(X_test1)

y_test1_pred
accuracy_score(y_test1, y_test1_pred)
X_test1<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11457708458308338, 'fraction_numerical': 0.026994601079784044, 'mean_word_length': 8.037940379403794, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 41, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '59000', 'license': 'MIT', 'n_tokens_mistral': 1309, 'n_tokens_neox': 1246, 'n_words': 238, 'path': 'decision_trees.ipynb', 'repo_name': 'BrendaCopatti/pattern_recognition', 'size': '3350'}