[scikit-learn] function transformer

Manprit Singh manpritsinghece at gmail.com
Mon Jun 21 04:18:12 EDT 2021


 Dear Sir,
I  have made such a transformer, below given is an example that generates 3
new columns, from existing 2 columns of a numpy array , first column is for
element wise addition, second is for element wise multiplication and third
is for element wise division .

>>> import numpy as np
>>> from sklearn.preprocessing import FunctionTransformer
>>> def col_add(x):
           x1 = x[:, 0] + x[:, 1]
           x2 = x[:, 0] * x[:, 1]
           x3 = x[:, 0] / x[:, 1]
           return np.c_[x, x1, x2, x3]

>>> col_adder = FunctionTransformer(col_add)
>>> arr = np.array([[2, 7], [4, 9], [3, 5]])
>>> arr
array([[2, 7],
       [4, 9],
       [3, 5]])
>>> col_adder.transform(arr) # will add 3 columns
array([[ 2.        ,  7.        ,  9.        , 14.        ,  0.28571429],
       [ 4.        ,  9.        , 13.        , 36.        ,  0.44444444],
       [ 3.        ,  5.        ,  8.        , 15.        ,  0.6       ]])
>>>

So in this way a function transformer can be used to add new features
generated from existing columns ?

On Fri, Jun 18, 2021 at 4:15 PM Manprit Singh <manpritsinghece at gmail.com>
wrote:

> Dear sir ,
>
> Just need to know if I can use a function transformer to generate new
> columns in the data set .
>
> Just see the below written pipeline
>
> num_pipeline = Pipeline([('imputer', SimpleImputer(strategy="median")),
>                          ('attribs_adder', column_adder),
>                          ('std_scaler', StandardScaler()),
>                         ])
> This pipeline is for numerical attributes in the dataset, firstly it will
> treat all mising values in the data set using  SimpleImputer , then i have
> made a function to add three more columns in the existing data, i have made
> a function transformer with this function and then StandardScaler .
>
> The columns being added are generated from existing columns (by element
> wise division of two columns) . So Using a function transformer is ok ?
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/pipermail/scikit-learn/attachments/20210621/d04680e6/attachment.html>


More information about the scikit-learn mailing list