conformal.evaluation¶
Evaluation module contains methods for evaluation of conformal predictors.
Function run()
produces Results of an appropriate type by using a Sampler on a given data set
to split it into a training and testing set.
Structure:
- Sampler (sampling methods)
- Results (evaluation results)
- Evaluation methods
-
class
conformal.evaluation.
Sampler
(data)[source]¶ Bases:
object
Base class for various data sampling/splitting methods.
-
data
¶ Data set for sampling.
Type: Table
-
n
¶ Size of the data set.
Type: int
Examples
>>> s = CrossSampler(Table('iris'), 4) >>> for train, test in s.repeat(3): >>> print(train)
-
-
class
conformal.evaluation.
RandomSampler
(data, a, b)[source]¶ Bases:
conformal.evaluation.Sampler
Randomly samples a subset of data in proportion a:b.
-
k
¶ Size of the selected subset.
Type: float
Examples
>>> s = RandomSampler(Table('iris'), 3, 2) >>> train, test = next(s)
-
-
class
conformal.evaluation.
CrossSampler
(data, k)[source]¶ Bases:
conformal.evaluation.Sampler
Sample the data in
k
folds. Shuffle the data before determining the folds.-
k
¶ Number of folds.
Type: int
Examples
>>> s = CrossSampler(Table('iris'), 4) >>> for train, test in s: >>> print(train)
-
-
class
conformal.evaluation.
LOOSampler
(data)[source]¶ Bases:
conformal.evaluation.CrossSampler
Leave-One-Out sampler is a cross sampler with the number of folds equal to the size of the data set.
Examples
>>> s = LOOSampler(Table('iris')) >>> for train, test in s: >>> print(len(test))
-
class
conformal.evaluation.
Results
[source]¶ Bases:
object
Contains results of an evaluation of a conformal predictor returned by the
run()
function.Examples
>>> cp = CrossClassifier(InverseProbability(LogisticRegressionLearner()), 5) >>> r = run(cp, 0.1, RandomSampler(Table('iris'), 2, 1)) >>> print(r.accuracy())
-
accuracy
()[source]¶ Compute the accuracy of the predictor averaging verdicts of individual predictions. This is the fraction of instances that contain the actual/reference class among the predicted ones for classification and the fraction of instances that contain the actual value within the predicted range for regression.
-
-
class
conformal.evaluation.
ResultsClass
[source]¶ Bases:
conformal.evaluation.Results
Results of evaluating a conformal classifier. Provides classification specific efficiency measures.
Examples
>>> cp = CrossClassifier(InverseProbability(LogisticRegressionLearner()), 5) >>> r = run(cp, 0.1, RandomSampler(Table('iris'), 2, 1)) >>> print(r.singleton_criterion())
-
accuracy
(class_value=None, eps=None)[source]¶ Compute accuracy for test instances with a given class value. If this parameter is not given, compute accuracy over all instances, regardless of their class.
-
confusion
(actual, predicted)[source]¶ Compute the number of singleton predictions of class predicted when the actual class is actual.
Examples
Drawing a confusion matrix.
>>> data = Table('iris') >>> cp = CrossClassifier(InverseProbability(LogisticRegressionLearner()), 3) >>> r = run(cp, 0.1, RandomSampler(data, 2, 1)) >>> values = data.domain.class_var.values >>> form = '{: >20}'*(len(values)+1) >>> print(form.format('actual\predicted', *values)) >>> for a in values: >>> c = [r.confusion(a, p) for p in values] >>> print(('{: >20}'*(len(c)+1)).format(a, *c)) actual\predicted Iris-setosa Iris-versicolor Iris-virginica Iris-setosa 18 0 0 Iris-versicolor 0 14 4 Iris-virginica 0 0 12
-
-
class
conformal.evaluation.
ResultsRegr
[source]¶ Bases:
conformal.evaluation.Results
Results of evaluating a conformal regressor. Provides regression specific efficiency measures.
Examples
>>> ir = InductiveRegressor(AbsErrorKNN(Euclidean(), 10, average=True)) >>> r = run(ir, 0.1, RandomSampler(Table('housing'), 2, 1)) >>> print(r.interdecile_range())
-
conformal.evaluation.
run
(cp, eps, sampler, rep=1)[source]¶ Run method is used to repeat an experiment one or more times with different splits of the dataset into a training and testing set. The splits are defined by the provided sampler. The conformal predictor itself might further split the testing set internally for its computations (e.g. inductive or cross predictors).
Run the conformal predictor cp on the datasets defined by the provided sampler and number of repetitions and construct the results. Fit the conformal predictor on each training set returned by the sampler and evaluate it on the corresponding test set. Inductive conformal predictors use one third of the training set (random subset) for calibration.
For more control over the exact datasets used for training, testing and calibration see
run_train_test()
.Returns: ResultsClass
orResultsRegr
Examples
>>> cp = CrossClassifier(InverseProbability(LogisticRegressionLearner()), 5) >>> r = run(cp, 0.1, CrossSampler(Table('iris'), 4), rep=3) >>> print(r.accuracy(), r.empty_criterion())
The above example uses a
CrossSampler
to define training and testing datasets. Each fold is used as the test set and the rest as a training set. The entire process is repeated three times with different fold splits and results in 3*n predictions, where n is the size of the dataset.
-
conformal.evaluation.
run_train_test
(cp, eps, train, test, calibrate=None)[source]¶ Fits the conformal predictor cp on the training dataset and evaluates it on the testing set. Inductive conformal predictors use the provided calibration set or default to extracting one third of the training set (random subset) for calibration.
Returns: ResultsClass
orResultsRegr
Examples
>>> tab = Table('iris') >>> cp = CrossClassifier(InverseProbability(LogisticRegressionLearner()), 4) >>> r = run_train_test(cp, 0.1, tab[:100], tab[100:]) >>> print(r.accuracy(), r.singleton_criterion())