mojolearn.CalibratedClassifierCV#

class mojolearn.CalibratedClassifierCV(estimator=None, *, method='sigmoid', cv=None, n_jobs=None, ensemble=True)[source]#

Bases: _TreesWrapperBase

Calibrate a classifier’s probabilities with cross-validation.

Follows scikit-learn’s CalibratedClassifierCV. The calibrator reads the estimator’s decision_function, else its predict_proba (the positive column for a binary problem), and fits one calibrator per class (one-vs-rest): Platt’s sigmoid or isotonic regression (clipped outside the fitted range). Multiclass probabilities are normalized per row; a row of zeros becomes uniform. The folds fit one after another.

Differs from scikit-learn: Platt’s sigmoid minimizes scikit-learn’s objective with Newton steps and a backtracking line search instead of L-BFGS, so its parameters can differ in the last digits.

Parameters:
  • estimator (estimator, default None) – The mojolearn classifier to calibrate; it is cloned, never fitted in place. None uses LinearSVC(), as scikit-learn does.

  • method ({“sigmoid”, “isotonic”}, default “sigmoid”) – Platt’s sigmoid or isotonic regression. Any other value raises ValueError.

  • cv (int, splitter or iterable of (train, test), default None) – An int is the number of unshuffled stratified folds (>= 2); None means 5. A splitter object’s split(X, y) or an iterable of index pairs is used as given; with ensemble=False their test sets must cover every row exactly once, else ValueError.

  • n_jobs (None) – Only None is accepted; any other value raises NotImplementedError.

  • ensemble (bool, default True) – True: one (classifier, calibrators) pair per fold, the classifier fitted on the fold’s training rows and calibrated on its test rows; predict_proba averages the pairs. False: one set of calibrators fitted on the out-of-fold scores, and the classifier refitted on all rows. Values other than True or False raise NotImplementedError.

Variables:
  • calibrated_classifiers_ (list) – One (fitted classifier, calibrators) pair per fold, or a single pair when ensemble=False. The classifiers are fitted on integer class codes.

  • classes_ (array) – The class labels seen in fit, sorted.

  • n_features_in_ (int) – Number of features seen in fit.

Examples

>>> import numpy as np, mojolearn
>>> rng = np.random.default_rng(0)
>>> X = rng.random((1000, 8), dtype=np.float32)
>>> y = (X[:, 0] + 0.2 * rng.random(1000) > 0.6).astype(np.int32)
>>> model = mojolearn.CalibratedClassifierCV(mojolearn.GaussianNB(), cv=3).fit(X, y)
>>> model.predict_proba(X[:5]).shape
(5, 2)
__init__(estimator=None, *, method='sigmoid', cv=None, n_jobs=None, ensemble=True)[source]#

Methods

fit(X, y[, sample_weight])

Fit the classifier and its calibrators over the cross-validation folds.

get_params([deep])

numeric_mode_used()

The numeric mode this estimator runs in.

predict(X)

Predict the class with the highest calibrated probability.

predict_proba(X)

Calibrated class probabilities.

set_params(**params)

Set constructor parameters.

vendor_used()

The GPU backend this estimator runs on.

fit(X, y, sample_weight=None)[source]#

Fit the classifier and its calibrators over the cross-validation folds.

Parameters:
  • X (array of shape (n_samples, n_features)) – Training data, converted to float32.

  • y (array of shape (n_samples,)) – Class labels; fewer than two classes raise ValueError.

  • sample_weight (None) – Only None is accepted; weights raise NotImplementedError.

Returns:

self

Raises:

ValueError – When cv asks for more folds than some class has members.

predict_proba(X)[source]#

Calibrated class probabilities.

With ensemble=True the average of every fold’s calibrated probabilities.

Returns:

array of shape (n_samples, n_classes)

predict(X)[source]#

Predict the class with the highest calibrated probability.

Returns:

array of shape (n_samples,) – Labels from classes_.