将类型从struct table转换为base.FixedDataGrid在GO中。

huangapple go评论183阅读模式
英文:

Convert type from struct table to base.FixedDataGrid in GO

问题

我遇到了一个问题,无法将我的结构表转换为FixedDataGrid,因为我需要将我的数据转换为FixedDataGrid,以便我可以使用GoLearn库中的机器学习方法。

我的结构如下:

  1. type dataStruct struct{
  2. Sepal_length string
  3. Sepal_width string
  4. Petal_length string
  5. Petal_width string
  6. Species string
  7. }

所以当我从MongoDB获取数据时,我会这样获取:

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

有没有办法将我的"results"从[]dataStruct类型转换为base.FixedDataGrid类型?

CreateModel函数:

  1. func CreateModel(c echo.Context) error {
  2. fmt.Println("====> 进入CreateModel函数");
  3. //var results []dataStruct
  4. var Success bool = false
  5. Db := db.MgoDb{}
  6. Db.Init()
  7. defer Db.Close()
  8. col := Db.C(db.TrainingDataCollection)
  9. var results dataStruct
  10. if err := col.Find(nil).All(results); err != nil {
  11. fmt.Println("获取训练数据时出错")
  12. } else {
  13. //fmt.Println("Results All: ", results)
  14. Success = true
  15. }
  16. fmt.Println("=============",results)
  17. //初始化一个新的KNN分类器
  18. cls := knn.NewKnnClassifier("euclidean", "linear", 2)
  19. //进行训练集和测试集的拆分
  20. trainData, testData := base.InstancesTrainTestSplit(results, 0.55)
  21. cls.Fit(trainData)
  22. //计算欧氏距离并返回最常见的标签
  23. predictions, err := cls.Predict(testData)
  24. if err != nil {
  25. panic(err)
  26. }
  27. fmt.Println(predictions)
  28. //打印精确度/召回率指标
  29. confusionMat, err := evaluation.GetConfusionMatrix(testData, predictions)
  30. if err != nil {
  31. panic(fmt.Sprintf("无法获取混淆矩阵:%s", err.Error()))
  32. }
  33. fmt.Println(evaluation.GetSummary(confusionMat))
  34. return c.JSON(http.StatusOK, Success)

}

非常感谢您的帮助!

英文:

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:

  1. type dataStruct struct{
  2. Sepal_length string
  3. Sepal_width string
  4. Petal_length string
  5. Petal_width string
  6. Species string
  7. }

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

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

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

CreateModel function:

  1. func CreateModel(c echo.Context) error {
  2. fmt.Println("====> Entry CreateModel function");
  3. //var results []dataStruct
  4. var Success bool = false
  5. Db := db.MgoDb{}
  6. Db.Init()
  7. defer Db.Close()
  8. col := Db.C(db.TrainingDataCollection)
  9. var results dataStruct
  10. if err := col.Find(nil).All(results); err != nil {
  11. fmt.Println("ERROR WHILE GETTING THE TRAINING DATA")
  12. } else {
  13. //fmt.Println("Results All: ", results)
  14. Success = true
  15. }
  16. fmt.Println("=============",results)
  17. //Initialises a new KNN classifier
  18. cls := knn.NewKnnClassifier("euclidean", "linear", 2)
  19. //Do a training-test split
  20. trainData, testData := base.InstancesTrainTestSplit(results, 0.55)
  21. cls.Fit(trainData)
  22. //Calculates the Euclidean distance and returns the most popular label
  23. predictions, err := cls.Predict(testData)
  24. if err != nil {
  25. panic(err)
  26. }
  27. fmt.Println(predictions)
  28. // Prints precision/recall metrics
  29. confusionMat, err := evaluation.GetConfusionMatrix(testData, predictions)
  30. if err != nil {
  31. panic(fmt.Sprintf("Unable to get confusion matrix: %s", err.Error()))
  32. }
  33. fmt.Println(evaluation.GetSummary(confusionMat))
  34. return c.JSON(http.StatusOK, Success)
  35. }

Thank you in advance for your help !

答案1

得分: 1

这是我解决问题的方法:实际上有一个函数InstancesFromMat64(row int, col int, matrix),它可以从一个float64矩阵创建instances,这就是我使用的方法:

  1. func CreateModel(c echo.Context) error {
  2. fmt.Println("====> Entry CreateModel function")
  3. var Success bool = false
  4. Db := db.MgoDb{}
  5. Db.Init()
  6. defer Db.Close()
  7. col := Db.C(db.TrainingDataCollection)
  8. var results dataStruct
  9. if err := col.Find(nil).All(&results); err != nil {
  10. fmt.Println("ERROR WHILE GETTING THE TRAINING DATA")
  11. } else {
  12. Success = true
  13. }
  14. Data := make([]float64, len(results)*nbAttrs)
  15. /**** Filling the Data var with my dataset data *****/
  16. mat := mat64.NewDense(row, nbAttrs, Data)
  17. inst := base.InstancesFromMat64(row, nbAttrs, mat)
  18. //Selecting the class attribute for our instance
  19. attrs := inst.AllAttributes()
  20. inst.AddClassAttribute(attrs[4])
  21. //Initialise a new KNN classifier
  22. cls := knn.NewKnnClassifier("manhattan", "linear", 3)
  23. //Training-tessting split
  24. trainData, testData := base.InstancesTrainTestSplit(inst, 0.7)
  25. /******* Continue the Model creation ******/
  26. // 这里是你的代码,我只翻译了之前的部分
  27. I'll be glad if my answer helps someone.
  28. Thanks a lot @mkopriva for your help !

希望我的回答对某人有所帮助。

非常感谢 @mkopriva 的帮助!

英文:

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:

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

I'll be glad if my answer helps someone.

Thanks a lot @mkopriva for your help !

答案2

得分: 0

base.FixedDataGrid 是一个接口,所以你需要实现该接口,也就是在你想要用作 FixedDataGrid 的类型上实现它的所有方法。

由于你想要使用 []dataStruct,也就是 dataStruct 的切片,它是一个无名类型,作为 FixedDataGrid,你需要声明一个新的类型来添加方法,因为你只能给命名类型添加方法。例如像这样:

  1. type dataStructList []dataStruct

现在,如果你查看文档,你会发现 FixedDataGrid 接口声明了两个方法 RowStringSize,但它还嵌入了另一个接口,即 base.DataGrid 接口,这意味着你还需要实现 DataGrid 声明的方法。所以,给定你的新类型 dataStructList,你可以这样做:

  1. func (l dataStructList) RowString(int) string { /* ... */ }
  2. func (l dataStructList) Size() (int, int) { /* ... */ }
  3. func (l dataStructList) GetAttribute(base.Attribute) (base.AttributeSpec, error) { /* ... */ }
  4. func (l dataStructList) AllAttributes() []base.Attribute { /* ... */ }
  5. func (l dataStructList) AddClassAttribute(base.Attribute) error { /* ... */ }
  6. func (l dataStructList) RemoveClassAttribute(base.Attribute) error { /* ... */ }
  7. func (l dataStructList) AllClassAttributes() []base.Attribute { /* ... */ }
  8. func (l dataStructList) Get(base.AttributeSpec, int) []byte { /* ... */ }
  9. func (l dataStructList) MapOverRows([]base.AttributeSpec, func([][]byte, int) (bool, error)) error { /* ... */ }

在你实现了 /* ... */ 部分之后,你就可以开始使用 dataStructList 作为 FixedDataGrid,就像这样:

  1. var results []dataStruct
  2. err := col.Find(nil).All(&results)
  3. fdg := dataStructList(results) // 你可以将 fdg 用作 FixedDataGrid
  4. 或者
  5. var results dataStructList // 你可以将 results 用作 FixedDataGrid
  6. err := col.Find(nil).All(&results)

更新

在你在 dataStructList 上实现了所有这些方法之后,你只需要在函数内部的 results 变量上指定类型:

  1. func CreateModel(c echo.Context) error {
  2. fmt.Println("====> Entry CreateModel function")
  3. //var results []dataStruct
  4. var Success bool = false
  5. Db := db.MgoDb{}
  6. Db.Init()
  7. defer Db.Close()
  8. col := Db.C(db.TrainingDataCollection)
  9. var results dataStructList // <--- 使用实现了接口的类型
  10. if err := col.Find(nil).All(&results); err != nil { // <-- 传递 results 的指针
  11. fmt.Println("ERROR WHILE GETTING THE TRAINING DATA")
  12. } else {
  13. //fmt.Println("Results All: ", results)
  14. Success = true
  15. }
  16. fmt.Println("=============", results)
  17. //Initialises a new KNN classifier
  18. cls := knn.NewKnnClassifier("euclidean", "linear", 2)
  19. //Do a training-test split
  20. trainData, testData := base.InstancesTrainTestSplit(results, 0.55) // <-- 这将起作用,因为 results 的类型是 dataStructList,它实现了 base.FixedDataGrid 接口。
  21. cls.Fit(trainData)
  22. //Calculates the Euclidean distance and returns the most popular label
  23. predictions, err := cls.Predict(testData)
  24. if err != nil {
  25. panic(err)
  26. }
  27. fmt.Println(predictions)
  28. // Prints precision/recall metrics
  29. confusionMat, err := evaluation.GetConfusionMatrix(testData, predictions)
  30. if err != nil {
  31. panic(fmt.Sprintf("Unable to get confusion matrix: %s", err.Error()))
  32. }
  33. fmt.Println(evaluation.GetSummary(confusionMat))
  34. return c.JSON(http.StatusOK, Success)
  35. }
英文:

base.FixedDataGrid is an interface, so what you need to do is to implement that interface, that is, implement all of its methods, on the type you want to use as FixedDataGrid.

Since you want to use []dataStruct, a slice of dataStructs, which is an unnamed type, as FixedDataGrid you will have to declare a new type to be able to add methods to it because you can add methods only to named types. For example something like this:

  1. type dataStructList []dataStruct

Now, if you take a look at the documentation, you can see that the FixedDataGrid interface declares two methods RowString and Size but also embeds another interface, the base.DataGrid interface, which means you need to implement the methods declared by DataGrid as well. So, given your new dataStructList type, you can do something like this:

  1. func (l dataStructList) RowString(int) string { /* ... */ }
  2. func (l dataStructList) Size() (int, int) { /* ... */ }
  3. func (l dataStructList) GetAttribute(base.Attribute) (base.AttributeSpec, error) { /* ... */ }
  4. func (l dataStructList) AllAttributes() []base.Attribute { /* ... */ }
  5. func (l dataStructList) AddClassAttribute(base.Attribute) error { /* ... */ }
  6. func (l dataStructList) RemoveClassAttribute(base.Attribute) error { /* ... */ }
  7. func (l dataStructList) AllClassAttributes() []base.Attribute { /* ... */ }
  8. func (l dataStructList) Get(base.AttributeSpec, int) []byte { /* ... */ }
  9. func (l dataStructList) MapOverRows([]base.AttributeSpec, func([][]byte, int) (bool, error)) error { /* ... */ }

After you've implemented the /* ... */ parts you can then start using dataStructList as a FixedDataGrid, so something like this:

  1. var results []dataStruct
  2. err := col.Find(nil).All(&amp;results)
  3. fdg := dataStructList(results) // you can use fdg as FixedDataGrid

Or

  1. var results dataStructList // you can use results as FixedDataGrid
  2. err := col.Find(nil).All(&amp;results)

Update:

After you've implemented all of those methods on the dataStructList all you need is the type of the results variable inside your function:

  1. func CreateModel(c echo.Context) error {
  2. fmt.Println(&quot;====&gt; Entry CreateModel function&quot;)
  3. //var results []dataStruct
  4. var Success bool = false
  5. Db := db.MgoDb{}
  6. Db.Init()
  7. defer Db.Close()
  8. col := Db.C(db.TrainingDataCollection)
  9. var results dataStructList // &lt;--- use the type that implements the interface
  10. if err := col.Find(nil).All(&amp;results); err != nil { // &lt;-- pass a pointer to results
  11. fmt.Println(&quot;ERROR WHILE GETTING THE TRAINING DATA&quot;)
  12. } else {
  13. //fmt.Println(&quot;Results All: &quot;, results)
  14. Success = true
  15. }
  16. fmt.Println(&quot;=============&quot;, results)
  17. //Initialises a new KNN classifier
  18. cls := knn.NewKnnClassifier(&quot;euclidean&quot;, &quot;linear&quot;, 2)
  19. //Do a training-test split
  20. trainData, testData := base.InstancesTrainTestSplit(results, 0.55) // &lt;-- this will work because results if of type dataStructList, which implements the base.FixedDataGrid interface.
  21. cls.Fit(trainData)
  22. //Calculates the Euclidean distance and returns the most popular label
  23. predictions, err := cls.Predict(testData)
  24. if err != nil {
  25. panic(err)
  26. }
  27. fmt.Println(predictions)
  28. // Prints precision/recall metrics
  29. confusionMat, err := evaluation.GetConfusionMatrix(testData, predictions)
  30. if err != nil {
  31. panic(fmt.Sprintf(&quot;Unable to get confusion matrix: %s&quot;, err.Error()))
  32. }
  33. fmt.Println(evaluation.GetSummary(confusionMat))
  34. return c.JSON(http.StatusOK, Success)
  35. }

huangapple
  • 本文由 发表于 2017年5月2日 16:17:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/43733221.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定