Quickstart
Install
pip install rashomon-py
Audit a model you already have
import pandas as pd
from sklearn.datasets import load_breast_cancer
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from rashomon import audit
data = load_breast_cancer(as_frame=True)
X, y = data.data.iloc[:, :10], data.target
model = make_pipeline(StandardScaler(), LogisticRegression()).fit(X, y)
report = audit(model, X, y, random_state=0)
print(report.summary())
report.plot()
Three things to look at:
Flip rate.
report.flip_rateis the share of rows for which some equally-good model predicts the other label.report.flippedis the row mask, soX[report.flipped]selects those rows.Sign stability.
report.coefficientslists each coefficient’s range across all equally-good models.sign_stable=Falsemeans the direction of that effect is not determined by this data and model class.Reliability. The header line reports the sampler’s effective sample size. If it is not labelled “reliable”, increase
n_samples.
New rows go through the same pipeline:
report.predict_ranges(X_new) # estimate, low, high, flipped per row
Change what “equally good” means
audit(model, X, y, tolerance=0.01) # models at most 1% worse than optimal
audit(model, X, y, tolerance="lr") # likelihood-ratio set, alpha = 0.05
audit(model, X, y, tolerance=("absolute", 0.002))
The default "cv" uses one standard error of the cross-validated loss; see Choosing the tolerance (epsilon).
Weights
If the model uses class_weight, nothing changes: the weights are read from the model. If it was fitted with sample_weight, pass the same array so the audited objective is the one the model minimized:
model = LogisticRegression(class_weight="balanced").fit(X, y, sample_weight=w)
report = audit(model, X, y, sample_weight=w)
Weights change which models count as equally good; the flip rate is still a plain fraction of rows.
Regression
Ridge, RidgeCV and LinearRegression work the same way. Regression has no label to flip, so pass a decision cutoff if you have one:
from sklearn.linear_model import Ridge
report = audit(Ridge(alpha=1.0).fit(X, y), X, y, threshold=50_000) # "approved if predicted income > 50k"
Expert API
from rashomon import RashomonSet
rs = RashomonSet.from_sklearn(model, X, y, epsilon=0.02, random_state=0)
rs.coef_extremes() # exact coefficient ranges (intercept first)
rs.functional_range(x_row) # exact range of one row's logit
samples = rs.sample_hitandrun(2000, ellipsoid_mix=0.5)
What to read next
When this toolkit is useful: what this answers and what it does not
Core Concepts: the ε-Rashomon set and the quantities computed over it
Case study: the breast-cancer audit and the intervals it corresponds to: a full case study
API Reference: API documentation