<jupyter_start><jupyter_text>10 - DecisionTree + DummiesExecutar o algoritmo de `DecisionTree` utilizando o novo dataset.  Preparando o ambiente<jupyter_code>import pandas as pd
import numpy as np
import seaborn as sns
from sklearn.model_selection import train_test_split, GridSearchCV, GroupKFold, cross_validate
from sklearn.metrics import accuracy_score
from sklearn.tree import DecisionTreeClassifier
from sklearn.preprocessing import StandardScaler, MaxAbsScaler, MinMaxScaler, Normalizer, RobustScaler
SEED = 42 # Vida, universo e tudo mais
np.random.seed(SEED)<jupyter_output><empty_output><jupyter_text>Carregando os dados<jupyter_code>test = pd.read_csv('https://raw.githubusercontent.com/SalatielBairros/kaggle-titanic/main/data/processed/kaggle_test_dummies.csv')
del test['Unnamed: 0']
Xvalidation = test.drop(columns=['PassengerId'])
test.head(2)
treino = pd.read_csv('https://raw.githubusercontent.com/SalatielBairros/kaggle-titanic/main/data/processed/train_dummies.csv')
del treino['Unnamed: 0']
treino.head(2)
print(Xvalidation.shape)
print(treino.shape)
Xvalidation['Floor_T'] = 0<jupyter_output>(418, 17)
(891, 19)<jupyter_text>Separando treino e teste<jupyter_code>X = treino.drop(columns=['Survived'])
y = treino['Survived']
treino_x, teste_x, treino_y, teste_y = train_test_split(X, y, test_size = 0.2, stratify = y)
print("Treinaremos com %d elementos e testaremos com %d elementos" % (len(treino_x), len(teste_x)))<jupyter_output>Treinaremos com 712 elementos e testaremos com 179 elementos<jupyter_text>Executando a `DecisionTree`<jupyter_code>def dt_grid_search(treino_x, treino_y, teste_x, teste_y):
    criterion = ["gini", "entropy"]
    max_depth = [2, 3, 5, 7, 9, 11, 13]
    min_samples_split = [2, 8, 16, 32]
    min_samples_leaf = [1, 2, 8, 16, 32]
    resultados = []

    for c in criterion:
        for md in max_depth:
            for ms in min_samples_split:
                for ml in min_samples_leaf:
                    modelo = DecisionTreeClassifier(criterion = c, max_depth = md, min_samples_split = ms, min_samples_leaf = ml)
                    modelo.fit(treino_x, treino_y)
                    score = modelo.score(teste_x, teste_y)
                    resultados.append({
                        "criterion": c,
                        "max_depth" : md,
                        "min_samples_split": ms,
                        "min_samples_leaf": ml,
                        "score": score
                    })

    return pd.DataFrame(resultados)
resultados = dt_grid_search(treino_x, treino_y, teste_x, teste_y).sort_values(by='score', ascending=False)
resultados
def dt_grid_search_cross(X, y):
    criterion = ["gini", "entropy"]
    max_depth = [2, 3, 5, 7, 9, 11, 13]
    min_samples_split = [2, 8, 16, 32]
    min_samples_leaf = [1, 2, 8, 16, 32]
    n_splits = [2, 3, 5, 10, 15]
    resultados = []
    
    for c in criterion:
        for md in max_depth:
            for ms in min_samples_split:
                for ml in min_samples_leaf:
                    for sp in n_splits:
                        modelo = DecisionTreeClassifier(criterion = c, max_depth = md, min_samples_split = ms, min_samples_leaf = ml)
                        results = cross_validate(modelo, X, y, cv = GroupKFold(n_splits = sp), groups = X.Age, return_train_score=True)
                        resultados.append({
                            "criterion": c,
                            "max_depth" : md,
                            "min_samples_split": ms,
                            "min_samples_leaf": ml,
                            "test_score": (results['test_score'][0] + results['test_score'][1]) / 2,
                            "train_score": (results['train_score'][0] + results['train_score'][1]) / 2,
                            "n_splits": sp
                        })

    return pd.DataFrame(resultados)
resultados = dt_grid_search_cross(X, y).sort_values(by=['test_score', 'train_score'], ascending=[False, False])
resultados<jupyter_output><empty_output><jupyter_text>Treinando o melhor modelo<jupyter_code>best_params = resultados.sort_values(by=['test_score'], ascending=[False]).head(1).drop(columns=['test_score', 'train_score', 'n_splits']).to_dict(orient='records')[0]
best_params
cv = GroupKFold(n_splits = 10)
modelo = DecisionTreeClassifier().set_params(**best_params)
results = cross_validate(modelo, X, y, cv = cv, groups = X.Age, return_train_score=True)
pd.DataFrame(results)
modelo.fit(X, y)
predicoes = modelo.predict(Xvalidation)
resultado = pd.DataFrame()
resultado['PassengerId'] = test['PassengerId']
resultado['Survived'] = predicoes
resultado.to_csv('../../data/submissions/decision_tree_2.csv', index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1173728813559322, 'fraction_numerical': 0.01885593220338983, 'mean_word_length': 3.1631393298059964, 'pattern_counts': {'":': 10, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 2, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '374201', 'license': 'MIT', 'n_tokens_mistral': 1702, 'n_tokens_neox': 1590, 'n_words': 326, 'path': 'src/learning-classifiers/10 - DecisionTree + Dataset com Dummies.ipynb', 'repo_name': 'SalatielBairros/kaggle-titanic', 'size': '4720'}