Error: No class Attributes are defined while using golang golearn

Viewed 25

I'm using the knn example from golearn but I'm adding an instance manually to test the iris_headers.csv. I'm getting a "No class Attributes are defined" even though the data looks to be good. I can pull in two different csv files, one with only one line but that would require me to write a file ever time I wanted to test the data.

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)
    }
    trainData, _ := base.InstancesTrainTestSplit(rawData, 0)

    // Print a pleasant summary of your data.
    fmt.Println(trainData)

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

    cls.Fit(trainData)

    //cls.Save()
    //cls.Load()
    // There is a way of creating new Instances from scratch.
    // Inside an Instance, everything's stored as float64
    newData := make([]float64, 2)
    newData[0] = 1.0
    newData[1] = 0.0

    // Let's create some attributes Sepal length, Sepal width,Petal length, Petal width, Species
    attrs := make([]base.Attribute, 5)
    attrs[0] = base.NewFloatAttribute("Sepal length")
    attrs[1] = base.NewFloatAttribute("Sepal width")
    attrs[2] = base.NewFloatAttribute("Petal length")
    attrs[3] = base.NewFloatAttribute("Petal width")

    attrs[4] = new(base.CategoricalAttribute)
    attrs[4].SetName("Species")
    attrs[4].GetSysValFromString("Iris-setosa")
    attrs[4].GetSysValFromString("Iris-versicolor")
    base.NewDenseInstances().AddClassAttribute(attrs[4])

    //attrs[4].GetSysValFromString("Iris-virginica")

    // Now let's create the final instances set
    newInst := base.NewDenseInstances()

    // Add the attributes
    newSpecs := make([]base.AttributeSpec, len(attrs))
    for i, a := range attrs {
        newSpecs[i] = newInst.AddAttribute(a)
    }

    // Allocate space
    newInst.Extend(1)

    //5.1,3.5,1.4,0.2,Iris-setosa
    // Write the data
    newInst.Set(newSpecs[0], 0, newSpecs[0].GetAttribute().GetSysValFromString("5.1"))
    newInst.Set(newSpecs[1], 0, newSpecs[1].GetAttribute().GetSysValFromString("3.5"))
    newInst.Set(newSpecs[2], 0, newSpecs[2].GetAttribute().GetSysValFromString("1.4"))
    newInst.Set(newSpecs[3], 0, newSpecs[3].GetAttribute().GetSysValFromString("0.2"))
    newInst.Set(newSpecs[4], 0, newSpecs[4].GetAttribute().GetSysValFromString("Iris-virginica"))

    fmt.Println(newInst)

    preds, err := cls.Predict(newInst)
    if err != nil {
        panic(err)
    }

    fmt.Println("Here are the predictions: ", preds)

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

}


Here is the output:

With defined Attribute view
With defined Row view
Row masking on.
Attributes:
        FloatAttribute(Sepal length)
        FloatAttribute(Sepal width)
        FloatAttribute(Petal length)
        FloatAttribute(Petal width)
*       CategoricalAttribute("Species", [Iris-setosa Iris-versicolor Iris-virginica])
Data:   7.4 2.8 6.1 1.9 Iris-virginica
        5.0 3.6 1.4 0.2 Iris-setosa
        6.3 2.8 5.1 1.5 Iris-virginica
        5.0 3.3 1.4 0.2 Iris-setosa
        6.7 2.5 5.8 1.8 Iris-virginica
        7.7 3.0 6.1 2.3 Iris-virginica
        7.2 3.2 6.0 1.8 Iris-virginica
        5.7 3.0 4.2 1.2 Iris-versicolor
        5.0 3.2 1.2 0.2 Iris-setosa
        6.2 2.8 4.8 1.8 Iris-virginica
        ...
118 row(s) undisplayed
Instances with 1 row(s) 5 attribute(s)
Attributes:
        FloatAttribute(Sepal length)
        FloatAttribute(Sepal width)
        FloatAttribute(Petal length)
        FloatAttribute(Petal width)
        CategoricalAttribute("Species", [Iris-setosa Iris-versicolor Iris-virginica])

Data:
        5.10 3.50 1.40 0.20 Iris-virginica
All rows displayed
panic: No class Attributes are defined

What am I doing wrong? What is a class attribute and how can I define it?

Thank you.

0 Answers
Related