#machine-learning #data-science #analysis #false-positives

confusion_matrix

Confusion matrix implementation for storing results from a classification experiment and providing statistical information

3 stable releases

1.1.0 Nov 22, 2023
1.0.1 Nov 10, 2023
1.0.0 Feb 28, 2021

#88 in Machine learning

Download history 16/week @ 2024-01-01 14/week @ 2024-01-08 37/week @ 2024-01-15 127/week @ 2024-01-22 160/week @ 2024-01-29 280/week @ 2024-02-05 39/week @ 2024-02-12 75/week @ 2024-02-19 76/week @ 2024-02-26 58/week @ 2024-03-04 97/week @ 2024-03-11 30/week @ 2024-03-18 7/week @ 2024-03-25

194 downloads per month

MIT license

37KB
421 lines

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")] = 3;
    cm[("neg", "neg")] = 20;
    cm[("neg", "pos")] = 5;
    
    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.

No runtime deps