Skip to content

Instantly share code, notes, and snippets.

@joaopcnogueira
Last active July 11, 2019 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joaopcnogueira/11d8dec3d5514f0154eb0e351b80c945 to your computer and use it in GitHub Desktop.
Save joaopcnogueira/11d8dec3d5514f0154eb0e351b80c945 to your computer and use it in GitHub Desktop.
GridSearchCV with pipelines
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from category_encoders import OneHotEncoder
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_validate
from sklearn.model_selection import GridSearchCV
# lendo o dataset
df = pd.read_csv("train.csv")
# retirando colunas com nome, ingresso e cabine dos conjuntos
df.drop(["Name", "Ticket", "Cabin"], axis=1, inplace=True)
# criando o modelo usando pipeline
model = Pipeline(steps=[
('one-hot encoder', OneHotEncoder()),
('imputer', SimpleImputer(strategy='mean')),
('tree', DecisionTreeClassifier(max_depth=3, random_state=0))
])
# Tunando hiperparâmetros com 5-fold cross-validation e pipelines
parameters = {'tree__max_depth': [3, 4, 5]}
kfold = KFold(n_splits=5, shuffle=True, random_state=42)
grid = GridSearchCV(model, param_grid=parameters, cv=kfold, n_jobs=-1)
grid.fit(X=df.drop(['Survived'], axis=1), y=df['Survived'])
# qual o melhor parâmetro
grid.best_params_
# OUTPUT
# {'tree__max_depth': 3}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment