Skip to content

Instantly share code, notes, and snippets.

@tiaplagata
Created November 18, 2020 00:31
Show Gist options
  • Save tiaplagata/3fe3c4093dcf1571a2f3e53033d5ee25 to your computer and use it in GitHub Desktop.
Save tiaplagata/3fe3c4093dcf1571a2f3e53033d5ee25 to your computer and use it in GitHub Desktop.
FunctionTransformer in Pipeline
from sklearn.preprocessing import FunctionTransformer
# The function we want to convert to a class for our pipeline
def transform_yes_no(X):
X['international plan'] = X['international plan'].apply(lambda x: 1 if x.lower() == 'yes' else 0)
X['voice mail plan'] = X['voice mail plan'].apply(lambda x: 1 if x.lower() == 'yes' else 0)
return X
# Convert to class for pipeline
YesNoTransformer = FunctionTransformer(transform_yes_no)
# Define the pipeline
pipeline = Pipeline(steps= [
("TransformCategorical", YesNoTransformer),
("SMOTE", SMOTE()),
("GradientBooster", GradientBoostingClassifier())
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment