A Versatile Toolkit for Automated Feature Engineering in Machine Learning
Project description
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
- Beaver FE
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% |
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:
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 afitmethod.scoring(str): Evaluation metric (e.g.,"accuracy","f1","roc_auc").direction(str, optional): Optimization direction:"maximize"or"minimize". Default is"maximize".cv(intor 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 isNone.timeout(intorNone, optional): Time budget in seconds for the Bayesian optimisation search. Default is600. Set toNoneto disable the time limit.n_trials(intorNone, optional): Maximum number of Optuna trials. Default is100. Set toNoneto disable the trial limit.verbose(bool, optional): Whether to display progress logs. Default isTrue.
Execution Order:
Transformations are applied in the following canonical order:
- Datetime feature extraction
- Missing value indicators
- Missing value imputation (
fill_0,mean,median,most_frequent,knn) - Cyclical feature expansion
- Outlier handling (
iqrat 1.5 and 2.0,zscore,iforest,lof) - 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) - Spline transformations (knots: 5/10 · degrees: 2/3)
- Numerical binning (
quantile,uniform· 5 or 10 bins) - Categorical encoding (method selected by column cardinality)
- Normalisation — exclusive per-column choice:
yeo_johnson,log,box_cox(skewed/positive columns),min_max,standard,max_abs,robust×2 ranges,quantileuniform/normal - 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 toBeaverPipeline.
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 isNone.order(list[PipelineBlock], optional): Custom execution order for the pipeline blocks. When provided, transformation dicts are sorted by this order before being applied. Defaults toNone(preserves input order). UseCANONICAL_ORDERfrombeaverfe.pipeline_blocksfor 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
- Returns:
-
transform(X, y=None)Applies each fitted transformation in sequence.- Returns: Transformed feature matrix (
pd.DataFrame)
- Returns: Transformed feature matrix (
-
fit_transform(X, y=None)Combinesfitandtransformfor each transformation.- Returns: Transformed feature matrix.
-
get_params(deep=True)Retrieves the pipeline parameters (inherited fromsklearn.BaseEstimator).- Returns: Dictionary of parameters.
-
set_params(**params)Sets or updates the pipeline parameters (inherited fromsklearn.BaseEstimator).- Returns:
self
- Returns:
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(intor callable, optional): Cross-validation strategy. Default isNone.groups(np.ndarray, optional): Group labels for cross-validation. Default isNone.plot_file(strorNone, optional): Path where the score evolution chart is saved. Default is"performance_evolution.png". Set toNoneto skip plotting.max_steps(intorNone, optional): Limit evaluation to the first N transformations. Useful for large recipes or big datasets. Default isNone(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_stepsto 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!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file beaverfe-0.7.0.tar.gz.
File metadata
- Download URL: beaverfe-0.7.0.tar.gz
- Upload date:
- Size: 2.7 MB
- Tags: Source
- Uploaded using 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}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
239ec6051c840a8771fb3ad739135bc52ae2603ba708f903ced4e7490792819b
|
|
| MD5 |
6a32b7804eb437e59404037579308392
|
|
| BLAKE2b-256 |
b8780ecaef0ddaaaa59161cb30559012b8ade3840e36555157a37e7ed8d01087
|
File details
Details for the file beaverfe-0.7.0-py3-none-any.whl.
File metadata
- Download URL: beaverfe-0.7.0-py3-none-any.whl
- Upload date:
- Size: 73.3 kB
- Tags: Python 3
- Uploaded using 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}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
da00b94dbdaf0ede4a72723d88a135cc7293ffc37cb912accb3e8a98d2019a6d
|
|
| MD5 |
6f45ee33b77adf17513ad6cef6a9f017
|
|
| BLAKE2b-256 |
563bd9cb84daabecd0e96ed756be3832ffb1b532452f7ae5dc33f87d4e470c87
|