Last active
July 11, 2019 17:41
-
-
Save joaopcnogueira/11d8dec3d5514f0154eb0e351b80c945 to your computer and use it in GitHub Desktop.
GridSearchCV with pipelines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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