[][src]Trait random_world::cp::ConfidencePredictor

pub trait ConfidencePredictor<T> {
    fn train(
        &mut self,
        inputs: &ArrayView2<T>,
        targets: &ArrayView1<usize>
    ) -> LearningResult<()>;
fn update(
        &mut self,
        inputs: &ArrayView2<T>,
        targets: &ArrayView1<usize>
    ) -> LearningResult<()>;
fn calibrate(
        &mut self,
        inputs: &ArrayView2<T>,
        targets: &ArrayView1<usize>
    ) -> LearningResult<()>;
fn predict(
        &mut self,
        inputs: &ArrayView2<T>
    ) -> LearningResult<Array2<bool>>;
fn predict_confidence(
        &mut self,
        inputs: &ArrayView2<T>
    ) -> LearningResult<Array2<f64>>;
fn set_epsilon(&mut self, epsilon: f64); }

A Confidence Predictor (either transductive or inductive)

This trait is parametrized over T, the element type. It provides all the methods for making a confidence prediction.

Required methods

fn train(
    &mut self,
    inputs: &ArrayView2<T>,
    targets: &ArrayView1<usize>
) -> LearningResult<()>

Trains a Conformal Predictor on a training set.

Pedantic note: because CP is a transductive method, it never actually trains a model. This function, however, structures the training data so that it can be easily used in the prediction phase.

Arguments

  • inputs - Matrix (Array2) with values of type T of training vectors.
  • targets - Vector (Array1) of labels corresponding to the training vectors.

Examples

Please, see CP.

fn update(
    &mut self,
    inputs: &ArrayView2<T>,
    targets: &ArrayView1<usize>
) -> LearningResult<()>

Updates a Conformal Predictor with more training data.

After calling train() once, update() allows to add inputs to the Conformal Predictor's training data, which will be used for future predictions.

Arguments

  • inputs - Matrix (Array2) with values of type T of training vectors.
  • targets - Vector (Array1) of labels corresponding to the training vectors.

Examples

Please, see CP.

fn calibrate(
    &mut self,
    inputs: &ArrayView2<T>,
    targets: &ArrayView1<usize>
) -> LearningResult<()>

Calibrates an Inductive CP.

fn predict(&mut self, inputs: &ArrayView2<T>) -> LearningResult<Array2<bool>>

Returns candidate labels (region prediction) for test vectors.

The return value is a matrix of bool (Array2<bool>) with shape (n_inputs, n_labels), where n_inputs = inputs.rows() and n_labels is the number of possible labels; in such matrix, each column y corresponds to a label, each row i to an input object, and the value at [i,y] is true if the label conforms the distribution, false otherwise.

Examples

Please, see CP.

fn predict_confidence(
    &mut self,
    inputs: &ArrayView2<T>
) -> LearningResult<Array2<f64>>

Returns the p-values for test vectors.

The return value is a matrix of f64 (Array2<f64>) with shape (n_inputs, n_labels), where n_inputs = inputs.rows() and n_labels is the number of possible labels; in such matrix, each column y corresponds to a label, each row i to an input object, and the value at [i,y] is the p-value obtained when assuming y as a label for the i-th input object.

Examples

Please, see CP.

fn set_epsilon(&mut self, epsilon: f64)

Sets the significance level.

Arguments

  • epsilon - Significance level in [0,1].
Loading content...

Implementors

impl<T, N> ConfidencePredictor<T> for CP<T, N> where
    T: Clone + Sync + Copy,
    N: NonconformityScorer<T> + Sync
[src]

fn set_epsilon(&mut self, epsilon: f64)[src]

Sets the significance level.

Arguments

  • epsilon - Significance level in [0,1].

fn train(
    &mut self,
    inputs: &ArrayView2<T>,
    targets: &ArrayView1<usize>
) -> LearningResult<()>
[src]

Trains a Conformal Predictor on a training set.

Pedantic note: because CP is a transductive method, it never actually trains a model. This function, however, structures the training data so that it can be easily used in the prediction phase.

Arguments

  • inputs - Matrix (Array2) with values of type T of training vectors.
  • targets - Vector (Array1) of labels corresponding to the training vectors.

Examples

#[macro_use(array)]
extern crate ndarray;
extern crate random_world;

use random_world::cp::*;
use random_world::ncm::*;

// Train a CP
let ncm = KNN::new(2);
let n_labels = 3;
let epsilon = 0.1;
let mut cp = CP::new(ncm, n_labels, Some(epsilon));
let train_inputs = array![[0., 0.],
                          [1., 0.],
                          [0., 1.],
                          [1., 1.],
                          [2., 2.],
                          [1., 2.]];
let train_targets = array![0, 0, 1, 1, 2, 2];

cp.train(&train_inputs.view(), &train_targets.view())
  .expect("Failed to train model");

Panics

  • if the number of training examples is not consistent with the number of respective labels.
  • If labels are not numbers starting from 0 and containing all numbers up to n_labels-1.

fn update(
    &mut self,
    inputs: &ArrayView2<T>,
    targets: &ArrayView1<usize>
) -> LearningResult<()>
[src]

Updates a Conformal Predictor with more training data.

After calling train() once, update() allows to add inputs to the Conformal Predictor's training data, which will be used for future predictions.

Arguments

  • inputs - Matrix (Array2) with values of type T of training vectors.
  • targets - Vector (Array1) of labels corresponding to the training vectors.

Examples

The following examples creates two CPs, cp and cp_alt, one train()-ed and update()d on partial data, the other one train()-ed on full data; these CPs are equivalent (i.e., their training data is identical).

#[macro_use(array)]
extern crate ndarray;
extern crate random_world;

use random_world::cp::*;
use random_world::ncm::*;

// Train a CP
let ncm = KNN::new(2);
let n_labels = 3;
let epsilon = 0.1;
let mut cp = CP::new(ncm, n_labels, Some(epsilon));
let train_inputs_1 = array![[0., 0.],
                            [0., 1.],
                            [2., 2.]];
let train_targets_1 = array![0, 1, 2];
let train_inputs_2 = array![[1., 1.]];
let train_targets_2 = array![0];
let train_inputs_3 = array![[1., 2.],
                            [2., 1.]];
let train_targets_3 = array![1, 2];

// First, train().
cp.train(&train_inputs_1.view(), &train_targets_1.view())
  .expect("Failed to train model");
// Update with new data.
cp.update(&train_inputs_2.view(), &train_targets_2.view())
  .expect("Failed to train model");
cp.update(&train_inputs_3.view(), &train_targets_3.view())
  .expect("Failed to train model");

// All this is identical to training the
// CP on all data once.
let ncm_alt = KNN::new(2);
let mut cp_alt = CP::new(ncm_alt, n_labels, Some(0.1));

let train_inputs = array![[0., 0.],
                          [0., 1.],
                          [2., 2.],
                          [1., 1.],
                          [1., 2.],
                          [2., 1.]];
let train_targets = array![0, 1, 2, 0, 1, 2];

cp_alt.train(&train_inputs.view(), &train_targets.view())
      .expect("Failed to train model");

// The two CPs are equivalent.
let preds = cp.predict(&train_inputs.view())
              .expect("Failed to predict");
let preds_alt = cp_alt.predict(&train_inputs.view())
                      .expect("Failed to predict");
assert!(preds == preds_alt);

Panics

  • if the number of training examples is not consistent with the number of respective labels.
  • if labels are not numbers starting from 0 and containing all numbers up to n_labels-1.
  • if train() hasn't been called once before (i.e., if self.train_inputs is None.

fn predict(&mut self, inputs: &ArrayView2<T>) -> LearningResult<Array2<bool>>[src]

Returns candidate labels (region prediction) for test vectors.

The return value is a matrix of bool (Array2<bool>) with shape (n_inputs, n_labels), where n_inputs = inputs.rows() and n_labels is the number of possible labels; in such matrix, each column y corresponds to a label, each row i to an input object, and the value at [i,y] is true if the label conforms the distribution, false otherwise.

Examples

#[macro_use(array)]
extern crate ndarray;
extern crate random_world;

use random_world::cp::*;
use random_world::ncm::*;

// Construct a deterministic CP with k-NN nonconformity measure (k=2).
let ncm = KNN::new(2);
let n_labels = 2;
let mut cp = CP::new(ncm, n_labels, Some(0.3));
let train_inputs = array![[0., 0.],
                          [1., 0.],
                          [0., 1.],
                          [1., 1.],
                          [2., 2.],
                          [1., 2.]];
let train_targets = array![0, 0, 0, 1, 1, 1];
let test_inputs = array![[2., 1.],
                         [2., 2.]];

// Train and predict
cp.train(&train_inputs.view(), &train_targets.view())
  .expect("Failed prediction");
let preds = cp.predict(&test_inputs.view())
              .expect("Failed to predict");
assert!(preds == array![[false, true],
                        [false, true]]);

fn predict_confidence(
    &mut self,
    inputs: &ArrayView2<T>
) -> LearningResult<Array2<f64>>
[src]

Returns the p-values for test vectors.

The return value is a matrix of f64 (Array2<f64>) with shape (n_inputs, n_labels), where n_inputs = inputs.rows() and n_labels is the number of possible labels; in such matrix, each column y corresponds to a label, each row i to an input object, and the value at [i,y] is the p-value obtained when assuming y as a label for the i-th input object.

Examples

#[macro_use(array)]
extern crate ndarray;
extern crate random_world;

use random_world::cp::*;
use random_world::ncm::*;

// Construct a deterministic CP with k-NN nonconformity measure (k=2).
let ncm = KNN::new(2);
let n_labels = 2;
let mut cp = CP::new(ncm, n_labels, Some(0.1));
let train_inputs = array![[0., 0.],
                          [1., 0.],
                          [0., 1.],
                          [1., 1.],
                          [2., 2.],
                          [1., 2.]];
let train_targets = array![0, 0, 0, 1, 1, 1];
let test_inputs = array![[2., 1.],
                         [2., 2.]];

// Train and predict p-values
cp.train(&train_inputs.view(), &train_targets.view()).unwrap();
let pvalues = cp.predict_confidence(&test_inputs.view())
                .expect("Failed prediction");
assert!(pvalues == array![[0.25, 1.],
                          [0.25, 1.]]);
}
Loading content...