pub struct ConfusionMatrix<A> { /* private fields */ }
Expand description

Confusion matrix for multi-label evaluation

A confusion matrix shows predictions in a matrix, where rows correspond to target and columns to predicted. Diagonal entries are correct predictions, and everything off the diagonal is a miss-classification.

Implementations§

source§

impl<A> ConfusionMatrix<A>

source

pub fn precision(&self) -> f32

Precision score, the number of correct classifications for the first class divided by total number of items in the first class

§Binary confusion matrix

For binary confusion matrices (2x2 size) the precision score is calculated for the first label and corresponds to

true-label-1 / (true-label-1 + false-label-1)
§Multilabel confusion matrix

For multilabel confusion matrices, the precision score is averaged over all classes (also known as macro averaging) A more precise controlled evaluation can be done by first splitting the confusion matrix with split_one_vs_all and then applying a different averaging scheme.

§Examples
use linfa::prelude::*;
use ndarray::array;

// create dummy classes 0 and 1
let prediction = array![0, 1, 1, 1, 0, 0, 1];
let ground_truth = array![0, 0, 1, 0, 1, 0, 1];

// create confusion matrix
let cm = prediction.confusion_matrix(&ground_truth).unwrap();

// print precision for label 0
println!("{:?}", cm.precision());
source

pub fn recall(&self) -> f32

Recall score, the number of correct classifications in the first class divided by the number of classifications in the first class

§Binary confusion matrix

For binary confusion matrices (2x2 size) the recall score is calculated for the first label and corresponds to

true-label-1 / (true-label-1 + false-label-2)
§Multilabel confusion matrix

For multilabel confusion matrices the recall score is averaged over all classes (also known as macro averaging). A more precise evaluation can be achieved by first splitting the confusion matrix with split_one_vs_all and then applying a different averaging scheme.

§Example
use linfa::prelude::*;
use ndarray::array;

// create dummy classes 0 and 1
let prediction = array![0, 1, 1, 1, 0, 0, 1];
let ground_truth = array![0, 0, 1, 0, 1, 0, 1];

// create confusion matrix
let cm = prediction.confusion_matrix(&ground_truth).unwrap();

// print recall for label 0
println!("{:?}", cm.recall());
source

pub fn accuracy(&self) -> f32

Accuracy score

The accuracy score is the ratio of correct classifications to all classifications. For multi-label confusion matrices this is the sum of diagonal entries to the sum of all entries.

source

pub fn f_score(&self, beta: f32) -> f32

F-beta-score

The F-beta-score averages between precision and recall. It is defined as

(1.0 + b*b) * (precision * recall) / (b * b * precision + recall)
source

pub fn f1_score(&self) -> f32

F1-score, this is the F-beta-score for beta=1

source

pub fn mcc(&self) -> f32

Matthew Correlation Coefficients

Estimates the normalized cross-correlation between target and predicted variable. The MCC is more significant than precision or recall, because all four quadrants are included in the evaluation. A generalized evaluation for multiple labels is also included.

source

pub fn split_one_vs_all(&self) -> Vec<ConfusionMatrix<bool>>

Split confusion matrix in N one-vs-all binary confusion matrices

source

pub fn split_one_vs_one(&self) -> Vec<ConfusionMatrix<bool>>

Split confusion matrix in N*(N-1)/2 one-vs-one binary confusion matrices

Trait Implementations§

source§

impl<A: Clone> Clone for ConfusionMatrix<A>

source§

fn clone(&self) -> ConfusionMatrix<A>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<A: Display> Debug for ConfusionMatrix<A>

Print a confusion matrix

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<A: PartialEq> PartialEq for ConfusionMatrix<A>

source§

fn eq(&self, other: &ConfusionMatrix<A>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<A> StructuralPartialEq for ConfusionMatrix<A>

Auto Trait Implementations§

§

impl<A> RefUnwindSafe for ConfusionMatrix<A>
where A: RefUnwindSafe,

§

impl<A> Send for ConfusionMatrix<A>
where A: Send,

§

impl<A> Sync for ConfusionMatrix<A>
where A: Sync,

§

impl<A> Unpin for ConfusionMatrix<A>

§

impl<A> UnwindSafe for ConfusionMatrix<A>
where A: RefUnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
§

unsafe fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V