<jupyter_start><jupyter_text>Explorando classificação com histogramas normalizados pela quantidade de píxels de cada amostra Importando biblioteca<jupyter_code>import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

from sklearn.svm import SVC
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import (accuracy_score,
                             classification_report,
                             confusion_matrix,
                             f1_score,
                             plot_confusion_matrix)

TARGET_NAMES = {'nao_faixa': 0,
                'faixa_frente': 1,
                'faixa_diagonal_direita': 2,
                'faixa_diagonal_esquerda': 3}
                
# Gerando 30 sementes aleatórias dentro do intervalo [0, 8000)
seed_arr = np.random.randint(8000, size=30)<jupyter_output><empty_output><jupyter_text>Realizando 30 rodadas de classificação<jupyter_code>df = pd.read_csv('./csvs/otsu-norm_n_pixels-lbp_faixas.csv').tail(1000)
y = df.pop('target').values
X = df.values

acc_e_f1_por_rodada = []
confu_matrices = []

for rodada, seed in enumerate(seed_arr):
  # separando dados
  X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=seed)

  clf = make_pipeline(StandardScaler(), SVC()).fit(X_train, y_train)
  y_pred = clf.predict(X_test)
  
  # Adicionando resultados à respectiva lista
  acc_e_f1_por_rodada.append(
    [accuracy_score(y_test, y_pred)]
    + list(f1_score(y_test, y_pred, average=None)))

  confu_matrices.append(
    confusion_matrix(y_test, y_pred, normalize='true'))

  print(f'Rodada {rodada}\t-\tSeed {seed}')
  print(
    classification_report(
      y_true=y_test,
      y_pred=y_pred,
      target_names=TARGET_NAMES))

  plot_confusion_matrix(
    estimator=clf,
    X=X_test,
    y_true=y_test,
    display_labels=TARGET_NAMES,
    xticks_rotation=30,
    normalize='true')
  
  plt.show()<jupyter_output>Rodada 0	-	Seed 316
                         precision    recall  f1-score   support

              nao_faixa       0.88      0.98      0.92        81
           faixa_frente       0.95      0.87      0.91        83
 faixa_diagonal_direita       0.98      0.94      0.96        69
faixa_diagonal_esquerda       0.97      0.99      0.98        67

               accuracy                           0.94       300
              macro avg       0.95      0.94      0.94       300
           weighted avg       0.94      0.94      0.94       300<jupyter_text>Métricas do experimento<jupyter_code>confusion_matrix_mean = np.mean(confu_matrices, axis=0)
confusion_matrix_std = np.std(confu_matrices, axis=0)

sns.heatmap(
    data=confusion_matrix_mean,
    cmap='Blues',
    vmin=0,
    vmax=1,
    annot=True,
    fmt='.4f',
    mask=confusion_matrix_mean == 0,
    xticklabels=TARGET_NAMES,
    yticklabels=TARGET_NAMES
    ).set(
        title='Matriz de confusão média',
        xlabel='Predicted label',
        ylabel='True label')

plt.show()

sns.heatmap(
    data=confusion_matrix_std,
    cmap='Blues',
    annot=True,
    fmt='.4f',
    mask=confusion_matrix_std == 0,
    xticklabels=TARGET_NAMES,
    yticklabels=TARGET_NAMES
    ).set(
        title='Desvio padrão da matriz de confusão',
        xlabel='Predicted label',
        ylabel='True label')

None
acc_df = pd.DataFrame(
  data=acc_e_f1_por_rodada,
  columns=['Acurácia'] + [f'f1-{name}' for name in TARGET_NAMES])

print(acc_df.describe())

acc_df.boxplot(rot=30)

acc_df.style.hide_index().background_gradient(cmap='Blues')<jupyter_output>Acurácia  f1-nao_faixa  f1-faixa_frente  f1-faixa_diagonal_direita  \
count  30.000000     30.000000        30.000000                  30.000000   
mean    0.943444      0.943900         0.916223                   0.936947   
std     0.009201      0.015895         0.017777                   0.017745   
min     0.923333      0.920245         0.888889                   0.900000   
25%     0.937500      0.932453         0.906396                   0.928987   
50%     0.946667      0.940787         0.912023                   0.939496   
75%     0.946667      0.956028         0.925000                   0.949803   
max     0.963333      0.978723         0.966443                   0.968553   

       f1-faixa_diagonal_esquerda  
count                   30.000000  
mean                     0.975400  
std                      0.011439  
min                      0.934426  
25%                      0.969355  
50%                      0.979129  
75%                      0.981538  
max      [...]
{'fraction_non_alphanumeric': 0.08542605709379696, 'fraction_numerical': 0.0892895471131144, 'mean_word_length': 1.945638432364096, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '515653', 'license': 'MIT', 'n_tokens_mistral': 1863, 'n_tokens_neox': 1637, 'n_words': 299, 'path': 'ML/f04-textura/faixas/07-hist_norm_n_pixels-svm.ipynb', 'repo_name': 'newtmagalhaes/Machine-Learning', 'size': '4659'}