将结构表中的类型转换为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 !

查看全部
dpxnrx11199
dpxnrx11199
2017/05/02 08:17
  • struct
  • mongodb
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

2个回复