<jupyter_start><jupyter_text>K Neighbors ClassifierNeste notebook usamos o classificador K Neighbors (K-vizinhos mais próximos) para determinar de acordo com as características do anúncio do carro se a coluna 'price' (preço) está em uma das seguintes faixas de preço:- very low price- low price- average low price- average price- average high price- high price- very high price<jupyter_code>import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

import sklearn
from sklearn.neighbors import KNeighborsClassifier
from sklearn import neighbors

from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from Utils import run_classifier_and_track, eval_classifier_metrics

KNEIGHBORS_CLASSIFIER_EXPERIMENT = "KNeighborsClassifier"
##Preparação do Dataframe

train = pd.read_csv('../datasets/classifier/train_classifier_vehicles.csv')
test = pd.read_csv('../datasets/classifier/test_classifier_vehicles.csv')
valid = pd.read_csv('../datasets/classifier/validation_classifier_vehicles.csv')

def set_categories(df, categories):
    df[categories] = df[categories].astype('category')

def set_codes(df, categories):    
    for category in categories:
        df[category] = df[category].cat.codes
    return df

categories = ['manufacturer', 'fuel', 'title_status', 'transmission', 'drive', 'type', 'state','pricecategory']
categories2 = ['manufacturer', 'fuel', 'title_status', 'transmission', 'drive', 'type', 'state']

set_categories(train, categories)
train['pricecode'] = train.pricecategory.cat.codes
set_codes(train, categories2)

set_categories(test, categories)
test['pricecode'] = test.pricecategory.cat.codes
set_codes(test, categories2)

set_categories(valid, categories)
valid['pricecode'] = valid.pricecategory.cat.codes
set_codes(valid, categories2)
##Divisão de entre treino e teste
x_train = train.drop(['pricecategory'],axis = 1)
y_train = train.pricecode

x_test = test.drop(['pricecategory'],axis = 1)
y_test = test.pricecode
params = {'n_neighbors': 5}
model = KNeighborsClassifier(n_neighbors=params['n_neighbors'])
run_classifier_and_track(x_train, 
                         'train_classifier_vehicles.csv',
                         x_test, 
                         'test_classifier_vehicles.csv',
                         model, 
                         KNEIGHBORS_CLASSIFIER_EXPERIMENT, 
                         'pricecode', 
                         'KNeighborsClassifier',
                         params)
y_model = model.predict(x_test.drop(columns=['pricecode']))
mat = confusion_matrix(y_test, y_model)

sns.heatmap(mat, square=True, annot=True, cbar=False, )
plt.xlabel('predicted value')
plt.ylabel('true value');<jupyter_output><empty_output><jupyter_text>Otimizando os parâmetrosEscolhemos os seguintes parâmetros para serem otimizados:- n_neighbors- weights- leaf_size<jupyter_code>import optuna
def objective(trial):
    x_df = valid.drop(columns=['pricecategory', 'pricecode'])
    target = valid['pricecode']
    
    classifier_name = trial.suggest_categorical("classifier", ["KNeighborsClassifier"])
    n_neighbors = trial.suggest_int('n_neighbors', 1, 20, step=1)
    weights = trial.suggest_categorical('weights', ['uniform', 'distance'])
    leaf_size = trial.suggest_int('leaf_size', 10, 60)
    
    classifier_obj = KNeighborsClassifier(n_neighbors=n_neighbors, weights=weights, leaf_size=leaf_size)
    classifier_obj.fit(x_df, target)
    predict = classifier_obj.predict(x_df)

    metrics = eval_classifier_metrics(target, predict, False)
    accuracy = metrics['test_accuracy']
    return accuracy

study = optuna.create_study(direction="maximize")
study.optimize(objective, n_trials=200)
print(study.best_trial)<jupyter_output>[32m[I 2020-11-17 20:27:04,204][0m A new study created in memory with name: no-name-46a13ee7-df87-4737-93c8-f064e582c3bf[0m
[32m[I 2020-11-17 20:27:04,945][0m Trial 0 finished with value: 0.9987193745155528 and parameters: {'classifier': 'KNeighborsClassifier', 'n_neighbors': 13, 'weights': 'distance', 'leaf_size': 44}. Best is trial 0 with value: 0.9987193745155528.[0m
[32m[I 2020-11-17 20:27:05,416][0m Trial 1 finished with value: 0.9986688235095879 and parameters: {'classifier': 'KNeighborsClassifier', 'n_neighbors': 1, 'weights': 'distance', 'leaf_size': 41}. Best is trial 0 with value: 0.9987193745155528.[0m
[32m[I 2020-11-17 20:27:05,947][0m Trial 2 finished with value: 0.9987193745155528 and parameters: {'classifier': 'KNeighborsClassifier', 'n_neighbors': 8, 'weights': 'distance', 'leaf_size': 15}. Best is trial 0 with value: 0.9987193745155528.[0m
[32m[I 2020-11-17 20:27:08,650][0m Trial 3 finished with value: 0.43864792909378897 and parameters: {'classifier': 'K[...]<jupyter_text>De acordo com a análise do optuna, os melhores parâmetros são:- n_neighbors = 13- weights = 'distance'- leaf_size = 44Vamos usá-los para executar mais uma vez o KNeighborsClassifier<jupyter_code>params = {'n_neighbors': 13, 'weights': 'distance', 'leaf_size': 44}
model = KNeighborsClassifier(n_neighbors=params['n_neighbors'],
                             weights=params['weights'],
                             leaf_size=params['leaf_size'])

run_classifier_and_track(x_train, 
                         'train_classifier_vehicles.csv',
                         x_test, 
                         'test_classifier_vehicles.csv',
                         model, 
                         KNEIGHBORS_CLASSIFIER_EXPERIMENT, 
                         'pricecode', 
                         'KNeighborsClassifierWithTunning',
                         params)<jupyter_output>train_Accuracy: 0.9975

test_Accuracy: 0.5118
{'fraction_non_alphanumeric': 0.11320422535211268, 'fraction_numerical': 0.052464788732394366, 'mean_word_length': 4.104222821203953, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '296444', 'license': 'MIT', 'n_tokens_mistral': 2119, 'n_tokens_neox': 1834, 'n_words': 400, 'path': 'Parte 2/notebooks/3-k-neighbors-classifier.ipynb', 'repo_name': 'mbs8/IF679-ciencia-de-dados', 'size': '5680'}