mlfeatures
Links
AI / Agents
Developers
Community
Meta
Requires: Python >=3.10
Provides-Extra: yaml, pyarrow, scipy, holidays, test, examples
Package Info
A framework for declaring, estimating, and applying feature-engineering pipelines. It is a Python port of the R recipes package.
Why mlfeatures
Preprocessing quietly goes wrong when statistics leak from test data, the same cleanup lives in two places, or hard-coded column lists drift from the data. A recipe declares it once, sequentially as a series of operations done on a selection of features. Fits on training data, and applies it identically everywhere.
- Train/serve parity: statistics are estimated once on training data and reused, so test data never leaks into the fit.
- Selectors: pick columns by name, role, or type instead of hard-coded lists, so steps keep working when the schema changes or an earlier step adds columns.
- Lineage: see which columns each step resolved to and where every output column came from.
- Serialization: save a fitted recipe to JSON or YAML and load it back for deployment.
- Your stack: pandas and polars through narwhals, scikit-learn pipelines with RecipeTransformer, and a
df.mlfaccessor for polars. - Built-in steps: the operations most models need, from imputing missing values and encoding categories to scaling, date features, and dimensionality reduction.
Installation
Install mlfeatures from PyPI:
pip install mlfeaturesmlfeatures requires Python 3.10 or later, and installs narwhals, pandas, polars, NumPy, and scikit-learn with it.
A few features need an optional package. Install the matching extra, for example pip install "mlfeatures[scipy]":
| Extra | Needed for |
|---|---|
yaml |
to_yaml() and from_yaml() |
scipy |
StepOrderNorm, StepSplineNatural, and StepSplineMonotone |
holidays |
StepHoliday, when flagging named holidays |
pyarrow |
Date columns in pandas data frames |
Lifecycle
mlfeatures separates feature engineering into three phases:
- Specify: declare a sequence of steps against a schema of variables. No data is touched.
- Fit: estimate any required statistics from a training dataset.
- Transform: apply the fitted recipe identically to train, test, or new data.
import pandas as pd
from mlfeatures import Recipe, StepDropCorr, StepNormalize, StepPca
from mlfeatures import Selectors as s
df = pd.DataFrame(
{
"a": [1.0, 2.0, 3.0, 4.0, 5.0],
"b": [2.0, 4.0, 6.0, 8.0, 10.0], # perfectly correlated with `a`
"c": [5.0, 1.0, 3.0, 1.0, 9.0],
}
)
# specify -> fit -> transform
recipe = (
Recipe()
.add(StepDropCorr(s.numeric_features(), threshold=0.9))
.add(StepNormalize(s.numeric_features()))
.add(StepPca(s.numeric_features(), num_comp=1))
)
fitted = recipe.fit(df)
result = fitted.transform(df)
print(result) PC1
0 -0.640881
1 -1.038822
2 -0.169031
3 -0.144394
4 1.993128
Learn more
- Introduction: a quickstart.
- Recipe: specify, fit, transform: the three phases in detail.
- Selectors and roles: how steps choose their columns.
- Arguments shared across steps:
columns, keep_original_cols,role, andid. - With polars and with scikit-learn pipelines: using recipes in the rest of your stack.
- Examples: six complete workflows, each with an R and Python side-by-side.
- Vocabulary: R recipes and scikit-learn terms, side by side.
- Reference: every step and class.
Contributing
Contributions are welcome. See CONTRIBUTING for setup, the checks CI runs, and how to build the docs.
License
MIT