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 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办