nonconformist.icp
.IcpClassifier¶
-
class
nonconformist.icp.
IcpClassifier
(nc_function, condition=None, smoothing=True)¶ Inductive conformal classifier.
Parameters: nc_function : BaseScorer
Nonconformity scorer object used to calculate nonconformity of calibration examples and test patterns. Should implement
fit(x, y)
andcalc_nc(x, y)
.smoothing : boolean
Decides whether to use stochastic smoothing of p-values.
See also
References
[R55] Papadopoulos, H., & Haralambous, H. (2011). Reliable prediction intervals with regression neural networks. Neural Networks, 24(8), 842-851. Examples
>>> import numpy as np >>> from sklearn.datasets import load_iris >>> from sklearn.tree import DecisionTreeClassifier >>> from nonconformist.icp import IcpClassifier >>> from nonconformist.nc import ClassifierNc, MarginErrFunc >>> iris = load_iris() >>> idx = np.random.permutation(iris.target.size) >>> train = idx[:int(idx.size / 3)] >>> cal = idx[int(idx.size / 3):int(2 * idx.size / 3)] >>> test = idx[int(2 * idx.size / 3):] >>> nc = ProbEstClassifierNc(DecisionTreeClassifier, MarginErrFunc()) >>> icp = IcpClassifier(nc) >>> icp.fit(iris.data[train, :], iris.target[train]) >>> icp.calibrate(iris.data[cal, :], iris.target[cal]) >>> icp.predict(iris.data[test, :], significance=0.10) ... array([[ True, False, False], [False, True, False], ..., [False, True, False], [False, True, False]], dtype=bool)
Attributes
cal_x (numpy array of shape [n_cal_examples, n_features]) Inputs of calibration set. cal_y (numpy array of shape [n_cal_examples]) Outputs of calibration set. nc_function (BaseScorer) Nonconformity scorer object used to calculate nonconformity scores. -
__init__
(nc_function, condition=None, smoothing=True)¶
-
calibrate
(x, y, increment=False)¶ Calibrate conformal predictor based on underlying nonconformity scorer.
Parameters: x : numpy array of shape [n_samples, n_features]
Inputs of examples for calibrating the conformal predictor.
y : numpy array of shape [n_samples, n_features]
Outputs of examples for calibrating the conformal predictor.
increment : boolean
If
True
, performs an incremental recalibration of the conformal predictor. The suppliedx
andy
are added to the set of previously existing calibration examples, and the conformal predictor is then calibrated on both the old and new calibration examples.Returns: None
-
fit
(x, y)¶ Fit underlying nonconformity scorer.
Parameters: x : numpy array of shape [n_samples, n_features]
Inputs of examples for fitting the nonconformity scorer.
y : numpy array of shape [n_samples]
Outputs of examples for fitting the nonconformity scorer.
Returns: None
-
get_params
(deep=True)¶ Get parameters for this estimator.
Parameters: deep : boolean, optional
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Returns: params : mapping of string to any
Parameter names mapped to their values.
-
predict
(x, significance=None)¶ Predict the output values for a set of input patterns.
Parameters: x : numpy array of shape [n_samples, n_features]
Inputs of patters for which to predict output values.
significance : float or None
Significance level (maximum allowed error rate) of predictions. Should be a float between 0 and 1. If
None
, then the p-values are output rather than the predictions.Returns: p : numpy array of shape [n_samples, n_classes]
If significance is
None
, then p contains the p-values for each sample-class pair; if significance is a float between 0 and 1, then p is a boolean array denoting which labels are included in the prediction sets.
-
predict_conf
(x)¶ Predict the output values for a set of input patterns, using the confidence-and-credibility output scheme.
Parameters: x : numpy array of shape [n_samples, n_features]
Inputs of patters for which to predict output values.
Returns: p : numpy array of shape [n_samples, 3]
p contains three columns: the first column contains the most likely class for each test pattern; the second column contains the confidence in the predicted class label, and the third column contains the credibility of the prediction.
-
set_params
(**params)¶ Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as pipelines). The latter have parameters of the form
<component>__<parameter>
so that it’s possible to update each component of a nested object.Returns: self
-