dsndm82062 2016-01-26 01:21
浏览 74

在Golearn中使用.Predict()函数时获取<nil>

I'm playing around with the knnclassifier_iris.go example in the golearn examples folder. I replaced the iris dataset with one of my own, and as long as I train my data on some percentage of the data I read in, all functions work fine and I get some output. However, when I clearly mention a training and testing dataset, and then run predict on the test dataset after fitting the training dataset, I get a nil result when I try to print the predictions. I don't know why I'm getting a nil value, so I would really appreciate some help.

My code:

package main

import (
    "fmt"
    "github.com/sjwhitworth/golearn/base"
    "github.com/sjwhitworth/golearn/evaluation"
    "github.com/sjwhitworth/golearn/knn"
)

func main() {
    trainData, err := base.ParseCSVToInstances("~/Desktop/churn_train.csv", true)
    if err != nil {
        panic(err)
    }
    fmt.Println(trainData)
    testData, err := base.ParseCSVToInstances("~/Desktop/churn_test.csv", false)
    if err != nil {
        panic(err)
    }
    fmt.Println(trainData)
    fmt.Println(testData)

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

//Calculates the Euclidean distance and returns the most popular label
    predictions := cls.Predict(testData)
    fmt.Println(predictions) //GETTING <NIL> AS OUTPUT

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

}
  • 写回答

1条回答 默认 最新

  • douqiao5440 2016-07-11 22:41
    关注

    (Just in case anybody's stumbled across this on Google). The issue tends to arise when the second ParseCSVToInstances produces instances which are subtly different from the first. To ensure that this isn't the problem, use ParseCSVToTemplatedInstances, so

    testData, err := base.ParseCSVToInstances("~/Desktop/churn_test.csv", false)
    

    becomes

     testData, err := base.ParseCSVToTemplatedInstances("~/Desktop/churn_test.csv", false, trainData)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器