Skip to main content

PyPI version PyPI Downloads

Beaver FE Logo


A Versatile Toolkit for Automated Feature Engineering in Machine Learning

Beaver FE is a Python library that streamlines feature engineering for machine learning. It provides robust tools for preprocessing tasks such as scaling, normalization, feature creation (e.g., binning, mathematical operations), and encoding. It improves data quality and boosts model performance with minimal manual effort.

Table of Contents

Getting Started

Install Beaver FE using pip:

pip install beaverfe

Usage Examples

Automated Feature Engineering

Automatically optimize feature transformations using a given model and metric:

from beaverfe import auto_feature_pipeline, BeaverPipeline
from sklearn.neighbors import KNeighborsClassifier

model = KNeighborsClassifier()
transformations = auto_feature_pipeline(x, y, model, scoring="accuracy", direction="maximize")

bfe = BeaverPipeline(transformations)
x_train = bfe.fit_transform(x_train, y_train)
x_test = bfe.transform(x_test, y_test)

Manual Transformations

from beaverfe import BeaverPipeline
from beaverfe.transformations import (
    MathematicalOperations,
    NumericalBinning,
    OutliersHandler,
    ScaleTransformation,
)

# Define transformations
transformations = [
    OutliersHandler(
        transformation_options={
            "sepal length (cm)": ("median", "iqr"),
            "sepal width (cm)": ("cap", "zscore"),
        },
        thresholds={
            "sepal length (cm)": 1.5,
            "sepal width (cm)": 2.5,
        },
    ),
    ScaleTransformation(
        transformation_options={
            "sepal length (cm)": "min_max",
            "sepal width (cm)": "robust",
        },
        quantile_range={
            "sepal width (cm)": (25.0, 75.0),
        },
    ),
    NumericalBinning(
        transformation_options={
            "sepal length (cm)": ("uniform", 5),
        }
    ),
    MathematicalOperations(
        operations_options=[
            ("sepal length (cm)", "sepal width (cm)", "add"),
        ]
    ),
]

bfe = BeaverPipeline(transformations)

x_train = bfe.fit_transform(x_train, y_train)
x_test = bfe.transform(x_test, y_test)

Saving and Loading Transformations

Save your pipeline for reuse across sessions:

import pickle
from beaverfe import BeaverPipeline

bfe = BeaverPipeline(transformations)

# Save pipeline parameters
with open("beaverfe_transformations.pkl", "wb") as f:
    pickle.dump(bfe.get_params(), f)

# Load pipeline parameters
with open("beaverfe_transformations.pkl", "rb") as f:
    params = pickle.load(f)

bfe.set_params(**params)

Benchmark Results

Beaver FE was evaluated on several datasets and models to assess its impact on model performance. The table below compares baseline accuracy versus accuracy after applying Beaver FE transformations:

Dataset Model Baseline BeaverFE Improvement
adult
LDA 0.842 0.910 +8.08%
LogisticRegression 0.815 0.914 +12.15%
XGBoost 0.921 0.923 +0.22%
bank
LDA 0.873 0.911 +4.35%
LogisticRegression 0.852 0.913 +7.16%
XGBoost 0.927 0.931 +0.43%
credit
LDA 0.717 0.771 +7.53%
LogisticRegression 0.722 0.775 +7.34%
XGBoost 0.760 0.761 +0.13%

Benchmark Performance Chart


Transformation Evaluation

To better understand the impact of each transformation applied with Beaver FE, you can use the function evaluate_transformations. This utility evaluates the model performance after each incremental transformation and generates a plot showing the score evolution step by step.

Example

from beaverfe import evaluate_transformations

scores = evaluate_transformations(
    transformations,     # list of Beaver transformations
    X,                   # input features
    y,                   # labels
    model,               # estimator to evaluate
    scoring="accuracy",  # evaluation metric
    cv=5,                # cross-validation folds
    groups=None,         # optional group labels for grouped CV
    plot_file="performance_evolution.png",
    max_steps=None,      # limit evaluation to first N transformations
)

print(scores)

Output

  • Scores list: A list of dictionaries with the score after each step, starting with the baseline (no transformations):
[
    {"name": "Baseline", "score": 0.663}
    {"name": "OutliersHandler", "score": 0.658}
    {"name": "MathematicalOperations", "score": 0.658}
    {"name": "SplineTransformation", "score": 0.658}
    {"name": "NumericalBinning", "score": 0.652}
    {"name": "NonLinearTransformation", "score": 0.663}
    {"name": "Normalization", "score": 0.674}
    {"name": "ScaleTransformation", "score": 0.658}
    {"name": "QuantileTransformation", "score": 0.922}
    {"name": "DimensionalityReduction", "score": 0.955}
    {"name": "ColumnSelection", "score": 0.955}
]
  • Evolution plot: The function also generates a line chart saved to performance_evolution.png. Each transformation is enumerated to avoid duplicate names, making it clear how performance evolves:

Performance Evolution


Core API

auto_feature_pipeline

Automatically finds and applies optimal transformations to improve model performance using Bayesian optimisation (Optuna).

from beaverfe import auto_feature_pipeline

Parameters:

  • X (pd.DataFrame): Feature matrix.
  • y (np.ndarray): Target variable.
  • model: A scikit-learn-compatible estimator implementing a fit method.
  • scoring (str): Evaluation metric (e.g., "accuracy", "f1", "roc_auc").
  • direction (str, optional): Optimization direction: "maximize" or "minimize". Default is "maximize".
  • cv (int or callable, optional): Cross-validation strategy (e.g., number of folds or a custom splitter). Default is 5.
  • groups (np.ndarray, optional): Group labels for cross-validation. Useful for grouped CV. Default is None.
  • timeout (int or None, optional): Time budget in seconds for the Bayesian optimisation search. Default is 600. Set to None to disable the time limit.
  • n_trials (int or None, optional): Maximum number of Optuna trials. Default is 100. Set to None to disable the trial limit.
  • verbose (bool, optional): Whether to display progress logs. Default is True.

Execution Order:

Transformations are applied in the following canonical order:

  1. Datetime feature extraction
  2. Missing value indicators
  3. Missing value imputation (fill_0, mean, median, most_frequent, knn)
  4. Cyclical feature expansion
  5. Outlier handling (iqr at 1.5 and 2.0, zscore, iforest, lof)
  6. Mathematical operations (binary: add, subtract, multiply, divide, modulus, hypotenuse, mean, power, min, max, log_ratio; unary: square, cube, sqrt, cbrt, reciprocal, abs; arbitrarily nested/chained expressions, e.g. (a + b) * c)
  7. Spline transformations (knots: 5/10 · degrees: 2/3)
  8. Numerical binning (quantile, uniform · 5 or 10 bins)
  9. Categorical encoding (method selected by column cardinality)
  10. Normalisation — exclusive per-column choice: yeo_johnson, log, box_cox (skewed/positive columns), min_max, standard, max_abs, robust ×2 ranges, quantile uniform/normal
  11. Dimensionality reduction (pca, lda, truncated_svd)

Returns:

  • list[dict]: A list of transformation configurations (each with "name" and "params" keys) that can be passed directly to BeaverPipeline.

BeaverPipeline

A scikit-learn compatible pipeline that applies a sequence of transformations.

from beaverfe import BeaverPipeline

Constructor Parameters:

  • transformations (list[dict], optional): List of transformation dictionaries (each with "name" and "params" keys), or a list of initialised transformer objects. Default is None.
  • order (list[PipelineBlock], optional): Custom execution order for the pipeline blocks. When provided, transformation dicts are sorted by this order before being applied. Defaults to None (preserves input order). Use CANONICAL_ORDER from beaverfe.pipeline_blocks for the recommended production order.

Public Methods:

  • fit(X, y=None) Fits each transformation in the pipeline to the dataset sequentially, passing the transformed output of each step as input to the next.

    • Returns: self
  • transform(X, y=None) Applies each fitted transformation in sequence.

    • Returns: Transformed feature matrix (pd.DataFrame)
  • fit_transform(X, y=None) Combines fit and transform for each transformation.

    • Returns: Transformed feature matrix.
  • get_params(deep=True) Retrieves the pipeline parameters (inherited from sklearn.BaseEstimator).

    • Returns: Dictionary of parameters.
  • set_params(**params) Sets or updates the pipeline parameters (inherited from sklearn.BaseEstimator).

    • Returns: self

evaluate_transformations

Evaluates a model by incrementally applying transformations and plots the score evolution.

from beaverfe import evaluate_transformations

Parameters:

  • transformations (list[dict]): List of transformations in Beaver format.
  • X (pd.DataFrame): Feature matrix.
  • y (np.ndarray): Labels.
  • model: Scikit-learn-compatible estimator.
  • scoring (str): Evaluation metric (e.g., "accuracy", "roc_auc").
  • cv (int or callable, optional): Cross-validation strategy. Default is None.
  • groups (np.ndarray, optional): Group labels for cross-validation. Default is None.
  • plot_file (str or None, optional): Path where the score evolution chart is saved. Default is "performance_evolution.png". Set to None to skip plotting.
  • max_steps (int or None, optional): Limit evaluation to the first N transformations. Useful for large recipes or big datasets. Default is None (evaluate all steps).

Returns:

  • list[dict]: A list of {"name": str, "score": float} dicts, one per step starting from the baseline.

Note: This function runs one full cross-validation per step, so for T transformations with cv=5 it requires 5T + 5 model fits. Use max_steps to control evaluation cost.


Available Transformations

For the full reference of all transformers, parameters, and code examples see TRANSFORMATIONS.md.


Contributing

We welcome contributions! Please submit pull requests, open issues, or share suggestions to improve Beaver FE.


License

Beaver FE is open-source software distributed under the MIT License.


🚀 Power up your ML workflows with intelligent, flexible feature engineering — with just a few lines of code. Try Beaver FE today!

Release files for beaverfe 0.7.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for beaverfe 0.7.0
File Size Uploaded
beaverfe-0.7.0.tar.gz 2.7 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for beaverfe 0.7.0
File Interpreter ABI Platform
beaverfe-0.7.0-py3-none-any.whl Python 3 none any Details

Total release size: 2.8 MB

Release files / beaverfe-0.7.0.tar.gz

Download URL beaverfe-0.7.0.tar.gz
Size 2.7 MB
Tags Source
SHA-256 checksum
How to use checksums
239ec6051c840a8771fb3ad739135bc52ae2603ba708f903ced4e7490792819b
BLAKE2b-256 checksum
How to use checksums
b8780ecaef0ddaaaa59161cb30559012b8ade3840e36555157a37e7ed8d01087
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / beaverfe-0.7.0-py3-none-any.whl

Download URL beaverfe-0.7.0-py3-none-any.whl
Size 73.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
da00b94dbdaf0ede4a72723d88a135cc7293ffc37cb912accb3e8a98d2019a6d
BLAKE2b-256 checksum
How to use checksums
563bd9cb84daabecd0e96ed756be3832ffb1b532452f7ae5dc33f87d4e470c87
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

0.7.0 This release

2 release files

0.6.0

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page