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 python使用pulp线性优化时报错
  • ¥15 开源或低价数据中台哪个最好
  • ¥15 arduino编程出现字符串疑似覆盖现象
  • ¥15 我的b站在没有碰到屏幕的情况下偶尔会自动跳出进度条,就像在屏幕上点了一下一样,但我并没有点。而且视频进度并没有变。这可能是什么原因造成的?
  • ¥30 STK matlab python仿真
  • ¥15 关于IMageEnView 图标定位问题
  • ¥20 求解答(matlab)
  • ¥30 ffmpeg库使用过程中遇到的问题
  • ¥15 pyqt5 中python如何通过Qtwebchannel主动发消息给web前端
  • ¥15 关于HTML中title获取xml内容的问题