Search code examples
javascriptp5.jstensorflow.jsml5.js

how to generate classification report using javascript


I'm using ml5 for a classification problem and I'm applying KNN classifier. How do I get the accuracy and the classification report of the model?


Solution

  • Notice the documentation mentions this about classify()'s output:

    Object: It returns an object with a top classIndex and label, confidences mapping all class indices to their confidence, and confidencesByLabel mapping all classes' confidence by label.

    Bellow there are examples as well, such as the ml5/p5/KNNClassification_Video example.

    If you print the result in gotResults() you'll see something like this:

    {
        "classIndex": 2,
        "label": "Scissor",
        "confidences": {
            "0": 0.3333333333333333,
            "1": 0,
            "2": 0.6666666666666666
        },
        "confidencesByLabel": {
            "Rock": 0.3333333333333333,
            "Paper": 0,
            "Scissor": 0.6666666666666666
        }
    }
    
    • classIndex will be the index of the top class
    • label is the string associated with that class
    • confidences returns object where the key is the class index and the value is the confidence (as a normalised value (e.g. 0.0 = 0%, 0.5 = 50%, 1.0 = 100%, etc.)
    • confidencesByLabel provides the same confidence values as above, but instead of the class indices the keys are the class labels.

    In short, once you call classify(), in the result handler, you're after result.confidences or result.confidencesByLabel depending on your application.