Last active
July 11, 2019 21:02
-
-
Save joaopcnogueira/47eda046fa13296eaf09ed279a8da3eb to your computer and use it in GitHub Desktop.
Using Pipelines and ColumnTransform to compose different data pre-processing steps
This file contains hidden or 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 | |
from sklearn.compose import ColumnTransformer | |
# 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) | |
# pipeline para pré-processamento das variáveis Age e Fare | |
num_transformer = Pipeline(steps=[ | |
('imputer', SimpleImputer(strategy='median')) | |
]) | |
# pipeline para pré-processamento das variáveis Sex e Embarked | |
cat_transformer = Pipeline(steps=[ | |
('one-hot encoder', OneHotEncoder()) | |
]) | |
# Compondo os pré-processadores | |
preprocessor = ColumnTransformer(transformers=[ | |
('num', num_transformer, ['Age', 'Fare']), | |
('cat', cat_transformer, ['Sex', 'Embarked']) | |
]) | |
# criando o modelo usando pipeline | |
model = Pipeline(steps=[ | |
('preprocessor', preprocessor), | |
('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, return_train_score=True) | |
grid.fit(X=df.drop(['Survived'], axis=1), y=df['Survived']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment