How do you get the result from golearn models?

Viewed 55

In the golearn examples it shows you how to train, test and get stats on the accuracy, but it doesn't explain how to use the model in practice. For example, the knnclassifier_iris.go example in the golearn github outputs all the data and accuracy, but doesn't show how to use it.

CVS DATA:

5.1,3.5,1.4,0.2,Iris-setosa
4.9,3.0,1.4,0.2,Iris-setosa
4.7,3.2,1.3,0.2,Iris-setosa
4.6,3.1,1.5,0.2,Iris-setosa
5.0,3.6,1.4,0.2,Iris-setosa

Program:

package main

import (
    "fmt"

    "github.com/sjwhitworth/golearn/base"
    "github.com/sjwhitworth/golearn/evaluation"
    "github.com/sjwhitworth/golearn/knn"
)

func main() {
    rawData, err := base.ParseCSVToInstances("../datasets/iris_headers.csv", true)
    if err != nil {
        panic(err)
    }

    //Initialises a new KNN classifier
    cls := knn.NewKnnClassifier("euclidean", "linear", 2)

    //Do a training-test split
    trainData, testData := base.InstancesTrainTestSplit(rawData, 0.50)
    cls.Fit(trainData)

    //Calculates the Euclidean distance and returns the most popular label
    predictions, err := cls.Predict(testData)
    if err != nil {
        panic(err)
    }
    fmt.Println(predictions)

    // Prints precision/recall metrics
    confusionMat, err := evaluation.GetConfusionMatrix(testData, predictions)
    if err != nil {
        panic(fmt.Sprintf("Unable to get confusion matrix: %s", err.Error()))
    }
    fmt.Println(evaluation.GetSummary(confusionMat))
}

Result:

Overall accuracy: 0.9545

But I'd like to feed the model a single line of data without the label and get a result. eg: 5.0,3.6,1.4,0.2

Also, can you save the model, or should you so that you're not re-learning it every time?

1 Answers

You can save model with method Save and load model with method Load from cls

Related