Skip to content

mljs/confusion-matrix

Repository files navigation

confusion-matrix

NPM version build status npm download

Confusion matrix for supervised classification. Compute metrics on your classification like accuracy, sensitivity, specificity. The list of implemented metrics were inspired from the confusion matrix wikipedia page. See API documentation for the list of metrics.

Installation

$ npm install --save ml-confusion-matrix

Usage

Load the library

// CommonJS
const { ConfusionMatrix } = require('ml-confusion-matrix');

// ES6 module syntax
import { ConfusionMatrix } from 'ml-confusion-matrix';

Instanciate from the list of true and predicted labels

Handy if you want a confusion matrix from a cross-validation or from the test data set prediction results.

const trueLabels = [0, 1, 0, 1, 1, 0];
const predictedLabels = [1, 1, 1, 1, 0, 0];

// The order of the arguments are important !!!
const CM2 = ConfusionMatrix.fromLabels(trueLabels, predictedLabels);
console.log(CM2.getAccuracy()); // 0.5

Instanciate from confusion matrix

You can call the constructor directly with the confusion matrix and the labels corresponding to each rows/columns.

const CM1 = new ConfusionMatrix(
  [
    [13, 2],
    [10, 5],
  ],
  ['cat', 'dog'],
);
console.log(CM1.getTruePositiveCount('cat')); // 13

License

MIT