dpxnrx11199 2017-05-02 08:17
浏览 60
已采纳

将结构表中的类型转换为GO中的base.FixedDataGrid

i'm having a trouble converting my struct table to fixedDataGrid, because i need my data to be a fixedDataGrid so that i can use machine learning methods from GoLearn lib.

My struct is like this:

type dataStruct struct{
    Sepal_length string
    Sepal_width string
    Petal_length string
    Petal_width string
    Species string
}

So when i get my data from my mongo db, i get them like this:

var results []dataStruct
err := col.Find(nil).All(&results)

Is there a way to convert my "results" from []dataStruct type to base.FixedDataGrid ??

CreateModel function:

func CreateModel(c echo.Context) error {
    fmt.Println("====> Entry CreateModel function");
    //var results []dataStruct
    var Success bool = false

    Db := db.MgoDb{}
    Db.Init()
    defer Db.Close()

    col := Db.C(db.TrainingDataCollection)
    var results dataStruct
    if err := col.Find(nil).All(results); err != nil {
        fmt.Println("ERROR WHILE GETTING THE TRAINING DATA")
    } else {
       //fmt.Println("Results All: ", results) 
       Success = true
    }

fmt.Println("=============",results)

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

//Do a training-test split
trainData, testData := base.InstancesTrainTestSplit(results, 0.55)
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))

return c.JSON(http.StatusOK, Success)

}

Thank you in advance for your help !

  • 写回答

2条回答 默认 最新

  • doupo6967 2017-05-04 08:47
    关注

    Here is how i solved the issue: Actually there is a function InstancesFromMat64(row int, col int, matrix) than creates instances from a float64 matrix, and this is what i used:

    func CreateModel(c echo.Context) error {
        fmt.Println("====> Entry CreateModel function");
        var Success bool = false
    
        Db := db.MgoDb{}
        Db.Init()
        defer Db.Close()
    
        col := Db.C(db.TrainingDataCollection)
        var results dataStruct
        if err := col.Find(nil).All(&results); err != nil {
               fmt.Println("ERROR WHILE GETTING THE TRAINING DATA")
        } else {
               Success = true
         }
    
       Data := make([]float64, len(results*nbAttrs)
    
       /**** Filling the Data var with my dataset data *****/
    
       mat := mat64.NewDense(row,nbAttrs,Data)
       inst := base.InstancesFromMat64(row,nbAttrs,mat)
    
       //Selecting the class attribute for our instance
       attrs := inst.AllAttributes()
       inst.AddClassAttribute(attrs[4])
    
       //Initialise a new KNN classifier
       cls := knn.NewKnnClassifier("manhattan","linear",3)
    
       //Training-tessting split
       trainData, testData := base.InstancesTrainTestSplit(inst,0.7)
    
       /*******          Continue the Model creation                ******/
    

    I'll be glad if my answer helps someone.

    Thanks a lot @mkopriva for your help !

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?