<jupyter_start><jupyter_text>Dummy classifiers<jupyter_code>import pandas as pd
uri = "https://gist.githubusercontent.com/guilhermesilveira/4d1d4a16ccbf6ea4e0a64a38a24ec884/raw/afd05cb0c796d18f3f5a6537053ded308ba94bf7/car-prices.csv"
data = pd.read_csv(uri)
data.head()<jupyter_output><empty_output><jupyter_text>Renomear colunas<jupyter_code>to_rename = {
    'mileage_per_year' : 'milhas_ano',
    'model_year': 'ano_modelo',
    'price' : 'preco',
    'sold' : 'vendido'
}
data = data.rename(columns = to_rename)
data.head()
swapper = {
    'no' : 0,
    'yes': 1
}
data.vendido = data.vendido.map(swapper)
data.head()<jupyter_output><empty_output><jupyter_text>Tratar dados - tirar colunas desnecessárias e arrumar jeito de apresentar os dados<jupyter_code>from datetime import datetime
ano_atual = datetime.today().year
data['idade_do_modelo'] =  ano_atual - data.ano_modelo
data.head()
data['km_ano'] = data.milhas_ano * 1.60934
data.head()
data = data.drop(columns = ["Unnamed: 0", "milhas_ano", "ano_modelo"], axis = 1)
data.head()<jupyter_output><empty_output><jupyter_text>Treino Linear SVC com baseline Dummy Stratified<jupyter_code>x = data[['km_ano', 'preco','idade_do_modelo']]
y = data['vendido']
from sklearn.model_selection import train_test_split
from sklearn.svm import LinearSVC
from sklearn.metrics import accuracy_score
from sklearn.dummy import DummyClassifier
import numpy as np

SEED = 4
np.random.seed(SEED)
train_x, test_x, train_y, test_y = train_test_split(x , y, test_size = 0.25, stratify = y)

print(f'Número de elementos: train_X - {len(train_x)}, train_Y - {len(train_y)}  test_X - {len(test_x)}, test_Y - {len(test_y)}')
print()
model = LinearSVC()
model.fit(train_x, train_y)

predict = model.predict(test_x)
acc = accuracy_score(test_y,predict) * 100
print(f'A precisão foi de {round(acc,2)}%')


dummy_stratified = DummyClassifier()
dummy_stratified.fit(train_x, train_y)
dummy_acc = dummy_stratified.score(test_x,test_y) * 100
print(f'A precisão do dummy stratified foi de {round(dummy_acc,2)}%')<jupyter_output>Número de elementos: train_X - 7500, train_Y - 7500  test_X - 2500, test_Y - 2500

A precisão foi de 57.8%
A precisão do dummy stratified foi de 58.0%<jupyter_text>Dummy Classifier Most Frequent<jupyter_code>from sklearn.dummy import DummyClassifier
dummy_mostfrequent = DummyClassifier()
dummy_mostfrequent.fit(train_x, train_y)
dummy_acc = dummy_mostfrequent.score(test_x,test_y) * 100
print(f'A precisão do dummy most frequent foi de {round(dummy_acc,2)}%')<jupyter_output>A precisão do dummy most frequent foi de 58.0%<jupyter_text>Treino com Decision Tree<jupyter_code>from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
import numpy as np

x = data[['km_ano', 'preco','idade_do_modelo']]
y = data['vendido']

SEED = 20
np.random.seed(SEED)
train_x,test_x, train_y, test_y = train_test_split(x , y, test_size = 0.25, stratify = y)

print(f'Número de elementos: train_X - {len(train_x)}, train_Y - {len(train_y)}  test_X - {len(test_x)}, test_Y - {len(test_y)}')
print()
model = DecisionTreeClassifier(max_depth = 3)
model.fit(train_x, train_y)

predict = model.predict(test_x)
acc = accuracy_score(test_y,predict) * 100
print(f'A precisão com Desicision Tree foi de {round(acc,2)}%')<jupyter_output>Número de elementos: train_X - 7500, train_Y - 7500  test_X - 2500, test_Y - 2500

A precisão com Desicision Tree foi de 78.4%<jupyter_text>Plotando Decision Tree<jupyter_code>from sklearn.tree import export_graphviz
import graphviz

features = x.columns
dot_data = export_graphviz(model, feature_names = features,
                           filled = True, rounded = True, class_names = ['não vendido', 'vendido'])
graph = graphviz.Source(dot_data)
graph<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11929000261028452, 'fraction_numerical': 0.031323414252153486, 'mean_word_length': 6.27134724857685, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 1, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '340550', 'license': 'Apache-2.0', 'n_tokens_mistral': 1582, 'n_tokens_neox': 1483, 'n_words': 308, 'path': 'Machine_Learning_4.ipynb', 'repo_name': 'ThiagoKS-7/Python_para_Data_Science', 'size': '3831'}