Library to construct a confusion matrix and retrieve statistical information from it. https://crates.io/crates/confusion_matrix

Peter Lane 84e94631c2 corrected mistake in example 10 months ago
examples 84e94631c2 corrected mistake in example 10 months ago
src 84e94631c2 corrected mistake in example 10 months ago
Cargo.toml 66557da109 renamed about.md as README.md 10 months ago
README.md 84e94631c2 corrected mistake in example 10 months ago

README.md

Confusion Matrix

For storing results from a classification experiment and providing statistical information.

A confusion matrix is used in data-mining as a summary of the performance of a classification algorithm. Each row represents the actual class of an instance, and each column represents the predicted class of that instance, i.e. the class that they were classified as. Numbers at each (row, column) reflect the total number of instances of actual class "row" which were predicted to fall in class "column".

A two-class example is:

    Predicted       Predicted     | 
    Positive        Negative      | Actual
    ------------------------------+------------
        a               b         | Positive
        c               d         | Negative

Here, the value:

  • a is the number of true positives (those labelled positive and classified positive)
  • b is the number of false negatives (those labelled positive but classified negative)
  • c is the number of false positives (those labelled negative but classified positive)
  • d is the number of true negatives (those labelled negative and classified negative)

From this table we can calculate statistics like:

  • true positive rate = a/(a+b)
  • positive recall = a/(a+c)

Features:

  • incrementally add results to build up a confusion matrix
  • calculate any of a range of statistics from the matrix at any time
  • output the matrix to text as a simple table

Example

The following example shows how to create a confusion matrix, add some results, and then print some statistics and the table itself.

use confusion_matrix;

fn main() {
    let mut cm = confusion_matrix::new();

    cm[("pos", "pos")] = 10;
    cm[("pos", "neg")] = 5;
    cm[("neg", "neg")] = 20;
    cm[("neg", "pos")] = 3;
    
    println!("Precision: {}", cm.precision("pos"));
    println!("Recall: {}", cm.recall("pos"));
    println!("MCC: {}", cm.matthews_correlation("pos"));
    println!("");
    println!("{}", cm);
}

Output:

Precision: 0.7692307692307693
Recall: 0.6666666666666666
MCC: 0.5524850114241865

Predicted |
neg pos   | Actual
----------+-------
 20   3   | neg
  5  10   | pos

MIT Licence

Copyright (c) 2021-23, Peter Lane peterlane@gmx.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.