Metadata-Version: 2.4
Name: py-evofe
Version: 0.3.0
Summary: Python implementation of Evolutionary Feature Engineering (evoFE)
Keywords: machine-learning,feature-engineering,evolutionary-algorithms,genetic-programming,scikit-learn
Author: Gustavo Pereira
Author-email: Gustavo Pereira <tanopereira@gmail.com>
License-Expression: MIT
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Dist: lightgbm>=4.6.0
Requires-Dist: optuna>=4.9.0
Requires-Dist: pandas>=3.0.3
Requires-Dist: polars>=1.36.1
Requires-Dist: pyarrow>=24.0.0
Requires-Dist: scikit-learn>=1.6.1
Requires-Dist: scipy>=1.13.1
Requires-Dist: umap-learn>=0.5.12
Requires-Dist: xgboost>=2.1.4
Requires-Python: >=3.11
Project-URL: Homepage, https://github.com/tanopereira/py-evoFE
Project-URL: Repository, https://github.com/tanopereira/py-evoFE
Project-URL: Issues, https://github.com/tanopereira/py-evoFE/issues
Description-Content-Type: text/markdown

# py-evofe: Evolutionary Feature Engineering in Python

[![PyPI version](https://img.shields.io/pypi/v/py-evofe)](https://pypi.org/project/py-evofe/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Python Version](https://img.shields.io/pypi/pyversions/py-evofe)](https://pypi.org/project/py-evofe/)

**py-evofe** is a Python library that uses a genetic algorithm to automatically discover, combine, and optimize feature transformations for tabular datasets. Instead of manually engineering interaction terms, ratios, or binning strategies, `py-evofe` searches the space of possible feature recipes to maximize the predictive performance of LightGBM or XGBoost models.

It implements a scikit-learn compatible interface (`fit`, `transform`, `predict`), allowing seamless integration into standard ML pipelines.

---

## Features

* **Scikit-Learn Interface:** Compatible with scikit-learn's `Pipeline`, `GridSearchCV`, and cross-validation tools.
* **Genetic Algorithm Optimization:** Searches the feature transformation space using selection, crossover, and mutation.
* **Hierarchical Chaining:** Evolved features can build on top of other proven features from previous generations (e.g., `log(ratio(x1, x2))`).
* **Stateful Transformers:** Includes PCA, SVD, UMAP, MCA/FAMD family, Genie & Lumbermark clustering, UMAP-cluster composites, and MST anomaly scoring.
* **Performance Caching:** Features are cached using matrix-hashing to avoid redundant computations (like $K$-NN search or UMAP projections) during cross-validation folds.
* **Flexible Evaluation:** Cross-validation (`cv`), stratified train/val/holdout splits (`split`), explicit `split_ids`, time-based and group-based CV, plus a `holdout_frac` untouched confirmation holdout with reported `search_gap`.
* **Multi-Fidelity Screening:** Cheap low-fidelity CV screens the population during warm-up; only the most promising half is re-evaluated at full fidelity.
* **Feature Masks:** Raw-column activity masks with sigmoid importance-centred initialization, geometric multi-toggle mutation, and minimum-active floors.
* **Island Model:** Multi-population search with ring/torus/grid/hypercube/tiered/complete topologies and Gibbs push/pull or tiered-admission migration policies (gene-only or full-individual payloads), heterogeneous per-island evaluators, row-split islands, and per-island validation.
* **Ensembling:** Greedy Caruana selection over island winners' out-of-fold predictions with weighted prediction blending.
* **Alternative & Custom Metrics:** LogLoss, AUC, F1, MAE/RMSE, calibrated RMSE/MAE, custom callables, and the Temperature Scaled Refinement (`ts_refinement`) metric.

---

## Installation

You can install the released version of **py-evofe** from PyPI with:

```bash
pip install py-evofe
```

Or using `uv`:

```bash
uv pip install py-evofe
```

---

## Quick Start

Here is a quick example using the Breast Cancer dataset for a binary classification task:

```python
import polars as pl
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from evofe import EvoFE

# Load dataset and rename columns to be clean
bc = load_breast_cancer(as_frame=True)
feature_cols = bc.feature_names[:8].tolist()  # use first 8 features for speed
df = pl.from_pandas(bc.frame[feature_cols + ["target"]])

X = df.drop("target")
y = df["target"].to_numpy()

# Split into train/test
X_train, X_test, y_train, y_test = train_test_split(
    X.to_numpy(), y, test_size=0.25, random_state=42, stratify=y
)
X_train_df = pl.DataFrame(X_train, schema=X.columns)
X_test_df = pl.DataFrame(X_test, schema=X.columns)

# 1. Create and configure EvoFE
evo = EvoFE(
    task="classification",          # "classification" | "multiclass" | "regression"
    evaluator="lightgbm",          # "lightgbm" | "xgboost"
    pop_size=10,                   # population size
    n_generations=5,               # max evolutionary generations
    cv_folds=3,                    # CV folds per fitness evaluation
    verbose=True
)

# 2. Fit: Runs evolution to discover best features
evo.fit(X_train_df, y_train)

# 3. Get evolved feature recipe
recipe = evo.get_recipe()
print(f"Best Fitness (exp(-log_loss)): {recipe.fitness:.4f}")
print("Evolved genes:")
for gene in recipe.genes:
    print(f"  • {gene.to_formula()} -> {gene.output_col}")

# 4. Transform: Get numeric features array for downstream sklearn estimators
X_test_arr = evo.transform(X_test_df)
print(f"Selected features array shape: {X_test_arr.shape}")

# Alternatively, get the enriched Polars DataFrame with original + new features:
X_test_enriched = evo.transform_df(X_test_df)
print(f"Enriched DataFrame columns: {X_test_enriched.columns}")

# 5. Predict using the best evolved model
predictions = evo.predict(X_test_df)
probabilities = evo.predict_proba(X_test_df)
```

---

## Supported Transformers

| Category | Transformers |
| :--- | :--- |
| **Arithmetic & Math** | `log`, `sqrt`, `reciprocal`, `power`, `displaced_log`, `add`, `subtract`, `multiply`, `divide`, `normalized_difference`, `log_ratio` |
| **Group-by Aggregations** | `groupby_mean`, `groupby_median`, `groupby_sd`, `groupby_max`, `groupby_min`, `groupby_ratio`, `groupby_zscore`, `groupby_quantile` |
| **Encoding & Binning** | `target_encode`, `target_encode_multiclass`, `pooled_target_encode`, `woe_encode`, `target_quantile_encode`, `cat_interaction_target_encode`, `frequency_encode`, `one_hot_encode`, `quantile_binning`, `quantile_binning_cat`, `log_binning`, `log_binning_cat`, `rank_transform`, `datetime_extract`, `datetime_cyclic`, `date_diff`, `feature_hash`, `concat` |
| **String Similarity** | `similarity_encode`, `minhash_encode`, `gap_encode` |
| **Dimensionality Reduction** | `pca`, `truncated_svd`, `random_projection`, `umap`, `mca`, `famd`, `between_group_pca`, `supervised_bgpca`, `supervised_mca`, `supervised_famd` |
| **Graph & Clustering** | `genie`, `genie_centroid_dist`, `lumbermark`, `lumbermark_centroid_dist`, `umap_genie`, `umap_lumbermark`, `mst_score`, `deadwood` |

## Scikit-Learn Pipeline Integration

Since `EvoFE.transform()` returns a standard 2D numeric numpy array of selected features, you can drop it directly into standard scikit-learn Pipelines:

```python
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from evofe import EvoFE

pipeline = Pipeline([
    ("scaler", StandardScaler()),
    ("evofe", EvoFE(task="classification", pop_size=10, n_generations=5, random_state=42))
])

# Fits scaler on data, then runs evolutionary feature search on scaled outputs
pipeline.fit(X_train, y_train)

# Scores the entire pipeline on test data
score = pipeline.score(X_test, y_test)
print(f"Accuracy: {score:.4f}")
```

## Reproducibility

Pass the `random_state` parameter to `EvoFE` to ensure evolutionary search runs are deterministic and reproducible:

```python
evo = EvoFE(
    task="classification",
    random_state=42, # seeds random and numpy modules for reproducibility
    pop_size=10,
    n_generations=5
)
```

---

## Islands, Ensembling & the Evolution Viewer

Run several populations in parallel with migration, then blend the winners:

```python
from evofe import EvoFE, ensemble_islands

evo = EvoFE(
    task="classification",
    islands=4,
    migration_topology="ring",     # ring | torus | grid | hypercube | tiered | complete
    migration_interval=2,
    row_split_islands=False,       # True: each island trains on a row shard
    record=True,                   # keep an evolution log for replay
    random_state=42,
)
evo.fit(X_train_df, y_train)

# Greedy Caruana ensemble over island winners' OOF predictions
from evofe.evolution.ensemble import predict_model_ensemble

ens = ensemble_islands(evo.get_recipe(), data=X_train_df, y=y_train)
blended = predict_model_ensemble(ens, X_test_df)   # weighted island blend
```

Replay the recorded search in a browser (self-contained HTML, no server needed):

```python
from evofe import view
view(evo.get_recipe())             # writes and opens an interactive replay page
```

---

## License

This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details.
