Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Ensure that the output of FunctionTransformer is 2D when validate=True #9595

@glemaitre

Description

@glemaitre

Description

Validate ensure that X is 2D as it is imposed in any other transformer. However, the output of the FunctionTransformer can be 1D if the provided function does not ensure it.

I think that we should ensure that the output is 2D since it could be useful when chaining transformer and estimator. validate=False will still allow for 1D in this case.

Example

import numpy as np                                                                                                   
from sklearn.preprocessing import FunctionTransformer                                                                
                                                                                                                     
                                                                                                                     
def func(y):                                                                                                         
    return np.sqrt(y[:, 0] ** 2 + y[:, 1] ** 2)                                                                      
                                                                                                                     
                                                                                                                     
transformer = FunctionTransformer(func=func, validate=True)                                                          
X = np.random.random((100, 2))                                                                                       
print(transformer.fit_transform(X).shape) 
(100,)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions