Python package for concise, transparent, and accurate predictive modeling.
All sklearn-compatible and easy to use.
Check out our new packages! Interpretability in text: imodelsX, interpretability tools for tabular data with agents: agentic-imodels
Modern machine-learning models are increasingly complex, often making them difficult to interpret. This package provides a simple interface for fitting and using state-of-the-art interpretable models, all compatible with scikit-learn. These models can often replace black-box models (e.g. random forests) with simpler models (e.g. rule lists) while improving interpretability and computational efficiency, all without sacrificing predictive accuracy! Simply import a classifier or regressor and use the fit and predict methods, same as standard scikit-learn models.
from imodels import get_clean_dataset, viz, HSTreeClassifierCV # import any imodels model here
from sklearn.model_selection import train_test_split
# prepare data (a sample clinical dataset)
X, y, feature_names = get_clean_dataset('csi_pecarn_pred')
X_train, X_test, y_train, y_test = train_test_split(
X, y, random_state=42)
# fit the model
model = HSTreeClassifierCV(max_leaf_nodes=4) # initialize a tree model and specify only 4 leaf nodes
model.fit(X_train, y_train, feature_names=feature_names) # fit model
preds = model.predict(X_test) # discrete predictions: shape is (n_test, 1)
preds_proba = model.predict_proba(X_test) # predicted probabilities: shape is (n_test, n_classes)
viz.draw(model, X_train, y_train, feature_names=feature_names, class_names=["no CSI", "CSI"],
title="Cervical spine injury", orientation="LR").save("model.svg") # draw the model (or print(model) for text, viz.interactive(model) for interaction)
Installation
Install with pip install imodels (see here for help).
Supported models
🗂️ Docs 📄 Research paper 🔗 Reference code implementation
| Model | Reference | Description |
|---|---|---|
| Rulefit rule set | 🗂️, 📄, 🔗 | Fits a sparse linear model on rules extracted from decision trees |
| Skope rule set | 🗂️, 🔗 | Extracts rules from gradient-boosted trees, deduplicates them, then linearly combines them based on their OOB precision |
| Boosted rule set | 🗂️, 📄, 🔗 | Sequentially fits a set of rules with Adaboost |
| Slipper rule set | 🗂️, 📄 | Sequentially learns a set of rules with SLIPPER |
| Bayesian rule set | 🗂️, 📄, 🔗 | Finds concise rule set with Bayesian sampling (slow) |
| Bayesian rule list | 🗂️, 📄, 🔗 | Fits compact rule list distribution with Bayesian sampling (slow) |
| Greedy rule list | 🗂️, 🔗 | Uses CART to fit a list (only a single path), rather than a tree |
| FFTree rule list | 🗂️ 🔗 | Heuristic for fitting fast and frugal rule lists |
| OneR rule list | 🗂️, 📄 | Fits rule list restricted to only one feature |
| Greedy rule tree | 🗂️, 📄, 🔗 | Greedily fits tree using CART |
| C4.5 rule tree | 🗂️, 📄, 🔗 | Greedily fits tree using C4.5 |
| Fast small tree | 🗂️,ㅤ📄 | Fast implementation of optimal tree for a given penalty per leaf |
| TAO rule tree | 🗂️, 📄 | Fits tree using alternating optimization |
| Iterative random forest |
🗂️, 📄, 🔗 | Repeatedly fit random forest, giving features with high importance a higher chance of being selected |
| Sparse integer linear model |
🗂️, 📄 | Sparse linear model with integer coefficients |
| Fast risk score | 🗂️,ㅤ📄 | Sparse integer risk score (a few features, small integer points), fit fast |
| Tree GAM | 🗂️, 📄, 🔗 | Generalized additive model fit with short boosted trees |
| GP GAM | 🗂️,ㅤ📄 | Adaptive GAM based on Gaussian processes |
| Greedy tree sums (FIGS) |
🗂️,ㅤ📄 | Sum of small trees with very few total rules (FIGS) |
| Hierarchical shrinkage wrapper |
🗂️, 📄 | Improve a decision tree, random forest, or gradient-boosting ensemble with ultra-fast, post-hoc regularization |
| RF+ (MDI+) | 🗂️, 📄 | Flexible random forest-based feature importance |
| Distillation wrapper |
🗂️ | Train a black-box model, then distill it into an interpretable model |
| AutoML wrapper | 🗂️ | Automatically fit and select an interpretable model |
| More models | ⌛ | (Coming soon!) Lightweight Rule Induction, MLRules, <Your model!> |
Demo notebooks
Demos are contained in the notebooks folder
Quickstart demo
Shows how to fit, predict, and visualize with different interpretable modelsAutogluon demo
Fit/select an interpretable model automatically using Autogluon AutoMLClinical decision rule notebook
Shows an example of usingimodels for deriving a clinical decision rule
Posthoc analysis
We also include some demos of posthoc analysis, which occurs after fitting models: posthoc.ipynb shows different simple analyses to interpret a trained model and uncertainty.ipynb contains basic code to get uncertainty estimates for a modelModel categories
The final form of the above models takes one of the following forms, which aim to be simultaneously simple to understand and highly predictive:

Ex. RuleFit vs. SkopeRules
RuleFit and SkopeRules differ only in the way they prune rules: RuleFit uses a linear model whereas SkopeRules heuristically deduplicates rules sharing overlap.Ex. Bayesian rule lists vs. greedy rule lists
Bayesian rule lists and greedy rule lists differ in how they select rules; bayesian rule lists perform a global optimization over possible rule lists while Greedy rule lists pick splits sequentially to maximize a given criterion.Ex. FPSkope vs. SkopeRules
FPSkope and SkopeRules differ only in the way they generate candidate rules: FPSkope uses FPgrowth whereas SkopeRules extracts rules from decision trees.Support for different tasks
Different models support different machine-learning tasks. Current support for different models is given below (each of these models can be imported directly from imodels (e.g. from imodels import RuleFitClassifier):
All of these models follow the standard sklearn estimator API, which is checked for every model in tests/model_api_test.py: fit returns the estimator, predict returns labels drawn from classes_ (strings included), predict_proba returns an (n_samples, n_classes) matrix whose rows sum to 1, DataFrame input sets feature_names_in_,, models can be cloned and configured with get_params/set_params, and every model works inside sklearn pipelines and grid searches.
| Model | Binary classification | Regression | Notes |
|---|---|---|---|
| Rulefit rule set | RuleFitClassifier | RuleFitRegressor | |
| Skope rule set | SkopeRulesClassifier | ||
| FPSkope rule set | FPSkopeClassifier | Like Skope, but generates candidate rules with FPGrowth; requires discretized features | |
| Rulefit rule set (XGBoost) | pass tree_generator=XGBClassifier(...) to RuleFitClassifier |
pass tree_generator=XGBRegressor(...) to RuleFitRegressor |
Requires xgboost |
| FPLasso rule set | FPLassoClassifier | FPLassoRegressor | Lasso over rules mined with FPGrowth; requires discretized features |
| Boosted rule set | BoostedRulesClassifier | BoostedRulesRegressor | |
| SLIPPER rule set | SlipperClassifier | ||
| Bayesian rule set | BayesianRuleSetClassifier | Fails for large problems | |
| Bayesian rule list | BayesianRuleListClassifier | ||
| Greedy rule list | GreedyRuleListClassifier | ||
| OneR rule list | OneRClassifier | ||
| Greedy rule tree (CART) | GreedyTreeClassifier | GreedyTreeRegressor | |
| C4.5 rule tree | C45TreeClassifier | ||
| Optimal rule tree | FastSmallTreeClassifier | Certifiably optimal rather than greedy; needs numba | |
| CCP-pruned rule tree | DecisionTreeCCPClassifier | DecisionTreeCCPRegressor | Prunes a tree to a target complexity via cost-complexity pruning |
| TAO rule tree | TaoTreeClassifier | TaoTreeRegressor | |
| Iterative random forest | IRFClassifier | IRFRegressor | Usage and interaction stability |
| Sparse integer linear model | SLIMClassifier | SLIMRegressor | Requires extra dependencies for speed |
| Sparse integer risk score | FastRiskScoreClassifier | Binary targets; binarizes features itself; needs numba | |
| Tree GAM | TreeGAMClassifier | TreeGAMRegressor | |
| GP GAM | GPGamRegressor | GAM with pairwise interactions; nothing to tune, and deterministic | |
| Greedy tree sums (FIGS) | FIGSClassifier | FIGSRegressor | |
| Hierarchical shrinkage | HSTreeClassifierCV | HSTreeRegressorCV | Wraps any sklearn tree-based model |
| Marginal shrinkage linear model |
MarginalShrinkageLinearRegressor | Linear model shrunk towards its marginal effects | |
| BART | BART | Bayesian additive regression trees (slow) | |
| Distillation | [DistilledRegressor](https://csinva.io/imodels/util/distillation.html#imodels.util.distillati |
