Skip to main content

Hierarchical Genetic Programming Library

CI codecov PyPI version Python versions License Docs

A Python library for explainable rule-based classification. It evolves human-readable boolean rule trees via hierarchical genetic programming, with automatic binarization and parallel benchmarking.

Full documentation: https://fii-optim-lab.github.io/hgp-lib/

What it does

hgp_lib evolves boolean rules that classify tabular data. A rule is a tree of logical operators (And, Or) over literals, for example And(age < 50, Or(income >= 30k, employed)). Rules are readable, so a trained classifier can be inspected and explained.

The method is genetic programming. A population of candidate rules is scored against the data, the best rules are selected, and crossover and mutation produce the next generation. Over many epochs the population converges toward rules with high fitness. Hierarchical GP extends this with child populations that evolve on sampled subsets of features, then combine into larger rules.

Boolean GP operates on boolean data. Numeric and categorical columns are binarized first, so a numeric feature becomes a set of boolean bins. See Data Preparation for details.

The model is a single boolean rule, so it is readable on its own and needs no separate explanation. See Theory for how the search works and Interpretability for why this matters.

Installation

pip install hgp-lib
# or
pip install "hgp-lib[dev]"

Editable install:

git clone https://github.com/fii-optim-lab/hgp-lib.git
cd hgp-lib
# Create a python virtual environment first, and activate it
pip install -e ".[dev]"
# Now you are able to change the code and run the tests
python -m pytest

Quickstart

BooleanRuleClassifier is the fastest way to train an interpretable rule end to end. It binarizes the raw data for you, evolves a rule, and applies the same binarization when predicting. The example below is fully runnable on the scikit-learn breast_cancer dataset.

from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split

from hgp_lib import BooleanRuleClassifier
from hgp_lib.configs import BooleanGPConfig, TrainerConfig

X, y = load_breast_cancer(return_X_y=True, as_frame=True)
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, stratify=y, random_state=0
)
X_train, X_val, y_train, y_val = train_test_split(
    X_train, y_train, test_size=0.25, stratify=y_train, random_state=0
)

config = TrainerConfig(
    gp_config=BooleanGPConfig(), num_epochs=1000, val_every=100
)
clf = BooleanRuleClassifier(config)  # StandardBinarizer by default; pass binarizer=... to customize
clf.fit(X_train, y_train, X_val, y_val)  # validation data is binarized internally too

predictions = clf.predict(X_test)  # raw data is binarized internally
print(clf.format_rule())           # the evolved rule as plain logic

Validation data is optional; when given, it is binarized with the same fitted binarizer and used to track a validation score during training. clf.format_rule() prints the rule with the binarized column names, so the model reads as plain logic. To binarize and train manually with GPTrainer, see Training; the Data Preparation guide shows how to avoid leaking data between splits.

Benchmarking

GPBenchmarker runs multiple independent experiments and aggregates the results. Each run takes a stratified train/test split, performs k-fold cross-validation on the training set, and evaluates the best rule on the held-out test set. Runs execute in parallel by default.

The benchmarker binarizes data internally, per fold, so you pass a raw pandas.DataFrame and skip manual binarization.

import numpy as np
from sklearn.datasets import load_breast_cancer
from hgp_lib.configs import BenchmarkerConfig, BooleanGPConfig, TrainerConfig
from hgp_lib.benchmarkers import GPBenchmarker

X, y = load_breast_cancer(return_X_y=True, as_frame=True)

gp_config = BooleanGPConfig()
trainer_config = TrainerConfig(gp_config=gp_config, num_epochs=1000, val_every=100)
config = BenchmarkerConfig(
    data=X,
    labels=y.to_numpy(),
    trainer_config=trainer_config,
    num_runs=30,
    n_folds=5,
    test_size=0.2,
    n_jobs=-1,
)
benchmarker = GPBenchmarker(config)
result = benchmarker.fit()

test_scores = result.test_scores
print(f"Test score: {np.mean(test_scores):.4f} ± {np.std(test_scores):.4f}")

# Human-readable best rule
print(result.best_rule.to_str(result.best_run.feature_names))

# sklearn-style predict on raw data (binarized internally with the best run's binarizer)
predictions = benchmarker.predict(X)

See Benchmarking for scorer optimization, custom binarizers, and the aggregated result fields.

Customizing the algorithm

The population, mutation, and crossover behavior is configured through factories passed to BooleanGPConfig. The default factories cover the common case. To use custom initialization strategies or mutations, subclass a factory and override its construction hook.

from hgp_lib.populations import PopulationGeneratorFactory

factory = PopulationGeneratorFactory(population_size=100)

The Configuring HGP guide covers the built-in factories and hierarchical GP. The Extending HGP guide covers custom strategies, mutations, and low-level use of BooleanGP directly.

Documentation

Contributing

See CONTRIBUTING.md.

Metadata

Release files for hgp-lib 1.2.4

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

Source distribution (sdist)

Source distribution for hgp-lib 1.2.4
File Size Uploaded Provenance Actions
hgp_lib-1.2.4.tar.gz 123.1 kB With provenance Details for hgp_lib-1.2.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for hgp-lib 1.2.4
File Interpreter ABI Platform Actions
hgp_lib-1.2.4-py3-none-any.whl Python 3 none any Details for hgp_lib-1.2.4-py3-none-any.whl

Total release size: 223.8 kB

Release files / hgp_lib-1.2.4.tar.gz

Download URL hgp_lib-1.2.4.tar.gz
Size 123.1 kB
Tags Source
SHA-256 checksum
How to use checksums
191108ae0cf29c36f071ced80f517b5f47093089e59061088e3a6adc113cb54c
BLAKE2b-256 checksum
How to use checksums
d2c14d3a7c0c3d5ccadd4aa7c5eb007885f6048c3b36d2d16ad1d0b53eb7dbcc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 11, 2026.

Transparency log

Release files / hgp_lib-1.2.4-py3-none-any.whl

Download URL hgp_lib-1.2.4-py3-none-any.whl
Size 100.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a82dc46af60fd3fb3bfe826520daa04fa895f07b7c6591f8f32b85e848e05055
BLAKE2b-256 checksum
How to use checksums
b6e8aa74aad32cdc2bd15b90c6cdffcafcd736667d0c2271034d6073b61a84e0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 11, 2026.

Transparency log

Security for hgp-lib 1.2.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for hgp_lib-1.2.4.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 11, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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