Metadata-Version: 2.4
Name: ebm-rs
Version: 0.2.3
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Rust
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Typing :: Typed
Requires-Dist: numpy>=1.23
License-File: LICENSE
Summary: Fast inference for InterpretML Explainable Boosting Machines, written in Rust
Keywords: ebm,explainable-boosting-machine,interpretml,interpretability,inference
Author: Jérémy Lugand
License-Expression: MIT
Requires-Python: >=3.10
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Changelog, https://github.com/jeremylugand/ebm-rs/blob/main/CHANGELOG.md
Project-URL: Issues, https://github.com/jeremylugand/ebm-rs/issues
Project-URL: Repository, https://github.com/jeremylugand/ebm-rs

# ebm-rs for Python

Fast inference for [InterpretML](https://github.com/interpretml/interpret) Explainable
Boosting Machines, written in Rust. Train with `interpret`, then score with `ebm_rs`.
Results match `interpret`'s to floating-point rounding.

```bash
pip install ebm-rs
```

## Getting a model

**From a JSON file.** Save the model where you train, load it where you predict.
`interpret` doesn't need to be installed where you predict.

```python
# Where you train
fitted_ebm.to_json("model.json", detail="minimal")
```

```python
# Where you predict
from ebm_rs import Ebm

model = Ebm.load("model.json")
```

**From a fitted `interpret` model, in memory.** No file, when both run in the same
process:

```python
from ebm_rs import Ebm

model = Ebm.from_interpret(fitted_ebm)
```

`Ebm.from_json(text)` loads the same JSON from a string, for example one read from a
database or object store. `detail="minimal"` gives the smallest file; `to_json()`'s other
detail levels load too.

## Predicting

```python
# Batches: pandas, polars, pyarrow, numpy, or a dict of columns
proba = model.predict_proba(df)  # (n, n_classes)
labels = model.predict(df)
scores = model.decision_function(df)
terms = model.explain(df)  # per-term contributions, like interpret's eval_terms

# One row: a dict keyed by feature name, or a list in feature order
model.predict_proba_one({"age": 42, "color": "blue", "visits": 3})
model.explain_one([42, "blue", 3])  # {"age": ..., "color": ..., "age & color": ...}
```

## Inputs

| input | columns matched | notes |
|---|---|---|
| pandas `DataFrame` | by name | numeric, nullable (`Int64`, `Float64`), `category`, `string` and object columns |
| polars `DataFrame` | by name | including `Categorical` and `Enum`; call `.collect()` on a `LazyFrame` first |
| pyarrow `Table` / `RecordBatch`, DuckDB results, anything with `__arrow_c_stream__` | by name | read without copying |
| numpy 2-D array or list of rows | by position, in `model.feature_names` order | typed arrays are fastest; lists and object arrays are read value by value, like single rows |
| `dict` of 1-D arrays | by name | |

Extra columns are ignored. Missing values can be `None`, `NaN`, `pd.NA` or nulls.

Integers stay exact (Python and numpy ints, and integer columns of every input type),
so on a categorical feature an ID-like code such as `12345678901234567` finds its
category the way it does in `interpret`, even past the range where floats are exact.

### Known differences with interpret

ebm-rs reads each value the same way whatever the other rows hold. `interpret`
doesn't quite: in a continuous column of a numpy object array that also contains
text, it converts every value to text first, so `True` becomes `"True"` (unknown
bin, instead of 1) and `np.float32(0.1)` becomes `"0.1"` (instead of its exact
value). ebm-rs reads them as 1 and as the exact value. Lists, tuples, dicts and sets
are refused as values for any feature; `interpret` refuses them on categorical
features and scores them as unknown on continuous ones.

## Speed

Batch methods release the GIL. With `parallel=True` (the default) they use every core
for batches large enough to gain; pass `parallel=False` to stay on one thread.

Measured on an Apple M5 against `interpret` 0.7.8 (see `BENCHMARKS.md` in the
repository):

- **One row:** `predict_proba_one` takes 0.5–1.5 µs, 195–290× faster than
  `interpret`'s `predict_proba` on a 1-row DataFrame.
- **100,000 rows on all cores:** 2.7–10.5× faster than `interpret`, depending on the
  model and the input type.

Use the `*_one` methods for single rows. Passing a 1-row DataFrame works, but pandas
overhead then dominates, and it's only about 1.6× faster than `interpret`.

## Building from source

Building needs a Rust toolchain and [maturin](https://www.maturin.rs). From the
repository root:

```bash
pip install maturin
```

```bash
maturin develop --release -m python/Cargo.toml
```

```bash
pip install --group python/pyproject.toml:test && pytest python/tests
```

## Code quality and security checks

CI runs these on every push. Run them from the repository root:

```bash
pip install --group python/pyproject.toml:dev  # needs pip 25.1+
```

```bash
ruff check . && ruff format --check .
```

```bash
cd python && mypy
```

```bash
cargo audit
```

```bash
pip-audit ./python && pip-audit
```

- **pip-audit** checks the package's runtime dependencies (`./python`), then everything
  installed in your environment, including the `dev` group.
- **mypy** runs in strict mode on the package, with relaxed settings for the tests.
- **cargo audit** fails on any known vulnerability in a Rust dependency. "Unmaintained"
  notices are only reported: `chrono` and `chrono-tz`, which arrive through Arrow, are
  currently flagged as unmaintained, with no vulnerability and no replacement
  available.
- The `Security audit` workflow also runs weekly, to catch new advisories against
  dependencies that haven't changed.

The package needs Python 3.10 or newer. One wheel per platform covers every Python
version from 3.10 up.

## License

MIT. ebm-rs is an independent project, not affiliated with or endorsed by Microsoft or
the InterpretML project.

