A customer scikit-learn transfomer example with parameter.
It takes a pandas DataFrame with a column called input as input, and returns a DataFrame with a column called output containing the transformed data:
define custom transformer
import pandas as pd |
To use this transformer, we would first instantiate it with the desired power parameter, and then call its fit_transform method on a DataFrame with an input column. For example:
generate some sample data and dataframe
import numpy as np |
input
0 -1.460135
1 0.188532
2 -0.272600
3 0.306880
4 -0.221020
use the transfomer, with parameter intilization
# Create the transformer |
output
0 2.131994
1 0.035544
2 0.074311
3 0.094175
4 0.048850
possible error: Parameter sees none value
the above example shows some practices: (1) the transformer have an default value in the init function (2) when calling the transfomer make sure to have the ···parameter=value format
For example, We can skip the parameter name like this, it will work for one time use:transformer = PowerTransformer(2)
But it will raise None error in using with other methos such as sklearn.model_selection.cross_val_score