0
$\begingroup$

I have a number of raw features that go into a scikit-learn model. I've already got a number of preprocessing steps (such as PolynomialFeatures) that creates additional features as part of my pipeline. However, I know that certain linear combinations of my raw features are also likely to be important in their own right. My question is whether such hand-crafted features (such as the sum of feature1 and feature2) can be created as part of scikit-learn's pipeline setup? Obviously I can create them at the Pandas dataframe level before passing them into the pipeline, but trying to figure out the cleanest way to do this in my codebase.

$\endgroup$

1 Answer 1

1
$\begingroup$

Yes, you can create custom features as part of a scikit-learn pipeline by using a custom transformer class. A transformer is a class that implements the fit() and transform() methods, and can be used to perform any custom preprocessing or feature engineering steps that you need.

Here's an example of how you might create a transformer class to create custom features as part of a pipeline:

from sklearn.base import BaseEstimator, TransformerMixin

class CustomFeatureTransformer(BaseEstimator, TransformerMixin):
    def __init__(self):
        # Initialize any parameters you need here
        pass
    
    def fit(self, X, y=None):
        # Fit the transformer to the data (e.g. compute any necessary statistics)
        return self
    
    def transform(self, X):
        # Create the custom features
        X['custom_feature1'] = X['feature1'] + X['feature2']
        X['custom_feature2'] = X['feature3'] - X['feature4']
        
        return X

You can then include this transformer in your pipeline like any other preprocessing step:

from sklearn.pipeline import Pipeline

# Create the pipeline
pipeline = Pipeline([
    ('custom_features', CustomFeatureTransformer()),
    # Other preprocessing steps go here
    ('model', MyModel())
])

When you call fit() on the pipeline, the transformer's fit() and transform() methods will be called in sequence, allowing you to create and include the custom features in your model's training data.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.