Awesome Open Source
Awesome Open Source


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.


$ npm install --save ml-confusion-matrix


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



Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
Javascript (1,117,147
Metrics (13,663
Classification (13,400
Labels (11,497
Matrix (10,854