FunctionTransformer
A FunctionTransformer
is used to define a user-defined function that consumes the data from the pipeline and returns the result of this function to the next stage of the pipeline. This is used for stateless transformations, such as taking the square or log of numbers, defining custom scaling functions, and so on.
In the following example, we will build a pipeline using the CustomLog
function and the predefined preprocessing method StandardScaler
:
- We import all the required libraries as we did in our previous examples. The only addition here is the
FunctionTransformer
method from thesklearn.preprocessing
library. This method is used to execute a custom transformer function and stitch it together to other stages in a pipeline:
import numpy as np from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn import preprocessing from sklearn.pipeline import make_pipeline from sklearn.preprocessing import FunctionTransformer from sklearn...