classifier.SplitConformalClassifier

classifier.SplitConformalClassifier(
    base_estimator=None,
    alpha=0.1,
    calibration_size=0.2,
    nonconformity_function=None,
    mondrian=False,
    random_state=None,
    verbose=0,
)

Split conformal classifier with automatic data splitting.

A meta-estimator that accepts an unfitted base estimator, automatically splits the data into training and calibration sets, fits the base estimator on the training set, and calibrates using split conformal prediction on the calibration set.

This is a convenience wrapper around ConformalClassifier that handles the complete workflow when you have a single dataset and want to perform split conformal prediction.

Parameters

base_estimator : estimator = None

Base classifier that implements fit() and predict_proba(). Can be a simple estimator or a sklearn Pipeline. Will be cloned before fitting.

alpha : float = 0.1

Significance level. Target coverage is 1 - alpha.

calibration_size : float = 0.2

Proportion of data to use for calibration. Must be in (0, 1).

nonconformity_function : callable = None

Function to compute nonconformity scores. Signature: func(y_true, y_prob) -> scores. If None, defaults to LAC (Least Ambiguous Classifier).

mondrian : bool = False

Whether to use Mondrian (class-conditional) conformal prediction. If True, separate quantiles are computed for each class.

random_state : int = None

Random seed for data splitting.

verbose : int = 0

Verbosity level: - 0: Silent (default) - 1: Key events (fit start/end, split sizes) - 2: Statistics (qhat, score distributions) - 3+: Debug (detailed information)

Attributes

model_ : estimator

The fitted base estimator.

conformal_ : ConformalClassifier

The calibrated conformal classifier wrapper.

qhat_ : float or dict

Calibrated quantile threshold(s).

classes_ : ndarray

Unique class labels.

n_classes_ : int

Number of classes.

n_train_ : int

Number of training samples used.

n_calib_ : int

Number of calibration samples used.

Examples

>>> from sklearn.ensemble import RandomForestClassifier
>>> from conformalpy.classifier import SplitConformalClassifier
>>>
>>> # Basic usage with simple estimator
>>> scc = SplitConformalClassifier(
...     base_estimator=RandomForestClassifier(n_estimators=100),
...     alpha=0.1,
...     calibration_size=0.2,
... )
>>> scc.fit(X, y)
>>> pred_sets = scc.predict(X_test)
>>>
>>> # Works with sklearn Pipeline
>>> from sklearn.pipeline import Pipeline
>>> from sklearn.preprocessing import StandardScaler
>>> pipe = Pipeline([
...     ('scaler', StandardScaler()),
...     ('clf', RandomForestClassifier()),
... ])
>>> scc = SplitConformalClassifier(base_estimator=pipe, alpha=0.1)
>>> scc.fit(X, y)

See Also

ConformalClassifier : Conformal classifier for pre-fitted models. CVPlusClassifier : Cross-validation based conformal classifier.

Notes

Use SplitConformalClassifier when:

  • You have a single dataset and want end-to-end conformal prediction
  • You want automatic train/calibration splitting
  • You’re building a sklearn Pipeline

Use ConformalClassifier when:

  • You have already fitted your model
  • You want explicit control over train/calibration splits
  • You’re integrating with an existing training workflow

Methods

Name Description
fit Fit the split conformal classifier.
get_qhat Get the calibrated quantile threshold.
predict Predict conformal prediction sets.
predict_p_values Compute conformal p-values for each class.
predict_proba Get class probabilities from base model.
predict_top_k Predict top-K prediction sets.
score Compute empirical coverage on test data.

fit

classifier.SplitConformalClassifier.fit(X, y)

Fit the split conformal classifier.

Splits the data, fits the base estimator on the training set, and calibrates on the calibration set.

Parameters

X : array-like of shape (n_samples, n_features)

Training features.

y : array-like of shape (n_samples,)

Training labels.

Returns

self : SplitConformalClassifier

Returns self for method chaining.

get_qhat

classifier.SplitConformalClassifier.get_qhat()

Get the calibrated quantile threshold.

Returns

: float or dict

Quantile threshold. Dict if Mondrian mode.

predict

classifier.SplitConformalClassifier.predict(X, *, alpha=None)

Predict conformal prediction sets.

Parameters

X : array-like of shape (n_samples, n_features)

Test features.

alpha : float, list of float, or None = None

Significance level(s) for prediction. If None, uses self.alpha. If scalar, returns list[list[int]]. If array-like, returns (n_samples, n_classes, n_alphas) bool array.

Returns

: list of list of int or np.ndarray

If alpha is None or scalar: list of prediction sets per sample. If alpha is array: boolean array of shape (n_samples, n_classes, n_alphas).

predict_p_values

classifier.SplitConformalClassifier.predict_p_values(X)

Compute conformal p-values for each class.

Parameters

X : array-like of shape (n_samples, n_features)

Input features.

Returns

: ndarray of shape (n_samples, n_classes)

P-values for each class.

predict_proba

classifier.SplitConformalClassifier.predict_proba(X)

Get class probabilities from base model.

Parameters

X : array-like of shape (n_samples, n_features)

Test features.

Returns

: ndarray of shape (n_samples, n_classes)

Class probabilities.

predict_top_k

classifier.SplitConformalClassifier.predict_top_k(X, k)

Predict top-K prediction sets.

Parameters

X : array-like of shape (n_samples, n_features)

Input features.

k : int

Number of classes to include in each prediction set.

Returns

: list of list of int

Prediction sets with exactly k classes each.

score

classifier.SplitConformalClassifier.score(X, y)

Compute empirical coverage on test data.

Parameters

X : array-like of shape (n_samples, n_features)

Test features.

y : array-like of shape (n_samples,)

True labels.

Returns

: float

Empirical coverage (proportion of true labels in prediction sets).