feat: Group Transformer

Open
#290 8 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
30/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale

Research direction

The issue names no files, so start by locating where the library's existing sklearn-compatible transformers are defined and how they follow fit and transform. The proposal is a GroupTransform that stores per-category aggregates in fit and merges them back in transform, but it leaves open whether inputs are DataFrames or ndarrays, and how unseen categories are handled. Settle those choices with the maintainers first; done means a fitted transformer returns the grouped aggregate merged onto the input, with tests for the chosen behaviour.

Written by the indexing model from the issue text.

Description

Is your feature request related to a problem? Please describe.
Aggregating variables by a single or multiple category is a simple task

df.groupby('cat')["num_var"].transform("mean")  

however to make it compatible with a Sklearn Pipeline, a Transformer with some requirements is needed. Feature Engine could provide a transformer with these functionality.

Describe the solution you'd like

A GroupTransform class with a fit() that stores that computes and stores the aggregated information
and a transform method that merges this information.

Class GroupTransform:

    def fit(X, y=None):
        self.X_agg_ = X.groupby('cat')["num_var"].transform("mean") 

    def transform(X, y=None):
        X.merge(self.X_agg_)

Describe alternatives you've considered
To make it 100% compatible with sklearn the fit and transform methods should takes as input an ndarray.
To pass the information to a new dataframe the merge method is needed.

Dominant language
Python
Stars
2.3k
Forks
391
Avg merge
11h 34m
Merged PRs (30d)
11

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from feature-engine/feature_engine

All issues in feature-engine/feature_engine

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.