Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Interpretml Interpret ROC And PR

From Leeroopedia


Knowledge Sources
Domains Machine_Learning, Interpretability
Last Updated 2026-02-07 12:00 GMT

Overview

ROC and PR are performance evaluation explainers that produce Receiver Operating Characteristic curves and Precision-Recall curves respectively for binary classification models.

Description

This module provides two performance-focused explainer classes:

  • ROC: Computes and visualizes the ROC curve for a binary classification model. It calculates the False Positive Rate (FPR) and True Positive Rate (TPR) at various threshold values using scikit-learn's roc_curve function, and computes the Area Under the Curve (AUC) using auc. The resulting plot includes a diagonal baseline for reference.
  • PR: Computes and visualizes the Precision-Recall curve for a binary classification model. It calculates precision and recall at various thresholds using scikit-learn's precision_recall_curve function, and computes the Average Precision (AP) using average_precision_score.

Both classes accept a model (or prediction function) and produce explanation objects that contain the curve data, thresholds, scores, and residual density histograms:

  • ROCExplanation: Renders the ROC curve with FPR on the x-axis, TPR on the y-axis, and a diagonal baseline.
  • PRExplanation: Renders the PR curve with Recall on the x-axis and Precision on the y-axis.

Both explainers only support binary classification. They automatically handle class detection and prediction function unification through the determine_classes and unify_predict_fn utilities.

Usage

Use ROC when you want to evaluate a binary classifier's trade-off between true positive rate and false positive rate across thresholds. Use PR when class imbalance makes precision-recall analysis more informative than ROC analysis. Both are intended for model evaluation after training.

Code Reference

Source Location

Signature

class ROC(ExplainerMixin):
    available_explanations = ["perf"]
    explainer_type = "perf"

    def __init__(self, model, feature_names=None, feature_types=None):
    def explain_perf(self, X, y, name=None):


class PR(ExplainerMixin):
    available_explanations = ["perf"]
    explainer_type = "perf"

    def __init__(self, model, feature_names=None, feature_types=None):
    def explain_perf(self, X, y, name=None):

Import

from interpret.perf import ROC, PR

I/O Contract

Constructor Inputs

Name Type Required Description
model model or callable Yes A trained model or prediction function (predict_proba for classification)
feature_names list of str No List of feature names
feature_types list of str No List of feature types

explain_perf Inputs

Name Type Required Description
X numpy array or compatible Yes Feature matrix to evaluate against
y numpy array Yes True binary labels (1-dimensional)
name str No User-defined explanation name

ROC explain_perf Outputs

Name Type Description
explanation ROCExplanation Contains FPR/TPR curve data, thresholds, AUC score, and residual density

PR explain_perf Outputs

Name Type Description
explanation PRExplanation Contains precision/recall curve data, thresholds, average precision, and residual density

Usage Examples

ROC Curve Example

from interpret.perf import ROC
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split

X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

model = RandomForestClassifier().fit(X_train, y_train)

roc = ROC(model)
roc_exp = roc.explain_perf(X_test, y_test, name="Random Forest ROC")
roc_exp.visualize()  # Plotly ROC curve with AUC

Precision-Recall Curve Example

from interpret.perf import PR
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split

X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

model = RandomForestClassifier().fit(X_train, y_train)

pr = PR(model)
pr_exp = pr.explain_perf(X_test, y_test, name="Random Forest PR")
pr_exp.visualize()  # Plotly PR curve with Average Precision

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment