Skip to content

MLearn Classification

Nicholas Curtis edited this page May 6, 2014 · 3 revisions

Classification

Classifiers in the MLearn.JS package attempt to identify which of a set of categories new prediction data belongs. Supervised classifiers used a labeled dataset which contains x number of unique targets designated as possible categories.

You can use the mlearn.classifier() method to retrieve a classification model. The below code creates a K-Nearest Neighbors model.

var model = mlearn.classifier('knn', {
    neighbors: 5, metric: 'euclidian', weights: true
});

Training

The classifier.train() method can be used to train a model. Training a model will prepare it for making predictions.

model.training(X, Y).then(function () {
    console.log('I am a trained model!');
});

Predicting

The classifier.predicting() method can be used to retrieve the predicted category of a new feature dataset or single feature row.

model.training(X, Y).then(function () {

   model.predicting(P).then(function (predictions) {
        predictions.each(function (prediction) {
            console.log('I predicted ', prediction);
        });
    });
  
});

Scoring

The classifier.scoring() method can be used to make multiple predictions and measure the performance of the classifier.

model.scoring(X, Y).then(function (score) {
    // gather some metrics
});

Accuracy

The score.accuracy() methods returns the percentage of predictions that were correct.

model.scoring(X, Y).then(function (score) {
    console('Accuracy: ', score.accuracy() );
});

Error Rate

The score.error() methods returns the percentage of predictions that were incorrect.

model.scoring(X, Y).then(function (score) {
    console('Error Rate: ', score.error() );
});

Incorrect Predictions

The score.misses() methods returns an array of objects containing target and prediction for all invalid predictions made during scoring.

model.scoring(X, Y).then(function (score) {
    score.misses().each(function (x) {
        console('Predicted: ' + x.prediction + ', Target: ' + x.target);
    });
});

Clone this wiki locally