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

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

Convert type from struct table to base.FixedDataGrid in GO

问题

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

我的结构如下:

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

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

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

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

CreateModel函数:

func CreateModel(c echo.Context) error {
    fmt.Println("====> 进入CreateModel函数");
    //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("获取训练数据时出错")
    } else {
       //fmt.Println("Results All: ", results) 
       Success = true
    }

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

//初始化一个新的KNN分类器
cls := knn.NewKnnClassifier("euclidean", "linear", 2)

//进行训练集和测试集的拆分
trainData, testData := base.InstancesTrainTestSplit(results, 0.55)
cls.Fit(trainData)

//计算欧氏距离并返回最常见的标签
predictions, err := cls.Predict(testData)

if err != nil {
    panic(err)
}

fmt.Println(predictions)

//打印精确度/召回率指标
confusionMat, err := evaluation.GetConfusionMatrix(testData, predictions)

if err != nil {
    panic(fmt.Sprintf("无法获取混淆矩阵:%s", err.Error()))
}

fmt.Println(evaluation.GetSummary(confusionMat))

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:

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 !

答案1

得分: 1

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

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 !

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

非常感谢 @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:

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 !

答案2

得分: 0

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

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

type dataStructList []dataStruct

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

func (l dataStructList) RowString(int) string { /* ... */ }
func (l dataStructList) Size() (int, int) { /* ... */ }
func (l dataStructList) GetAttribute(base.Attribute) (base.AttributeSpec, error)  { /* ... */ }
func (l dataStructList) AllAttributes() []base.Attribute  { /* ... */ }
func (l dataStructList) AddClassAttribute(base.Attribute) error  { /* ... */ }
func (l dataStructList) RemoveClassAttribute(base.Attribute) error  { /* ... */ }
func (l dataStructList) AllClassAttributes() []base.Attribute  { /* ... */ }
func (l dataStructList) Get(base.AttributeSpec, int) []byte  { /* ... */ }
func (l dataStructList) MapOverRows([]base.AttributeSpec, func([][]byte, int) (bool, error)) error  { /* ... */ }

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

var results []dataStruct
err := col.Find(nil).All(&results)
fdg := dataStructList(results) // 你可以将 fdg 用作 FixedDataGrid

或者

var results dataStructList // 你可以将 results 用作 FixedDataGrid
err := col.Find(nil).All(&results)

更新

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

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 dataStructList // <--- 使用实现了接口的类型
    if err := col.Find(nil).All(&results); err != nil { // <-- 传递 results 的指针
        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) // <-- 这将起作用,因为 results 的类型是 dataStructList,它实现了 base.FixedDataGrid 接口。
    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)

}
英文:

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:

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:

func (l dataStructList) RowString(int) string { /* ... */ }
func (l dataStructList) Size() (int, int) { /* ... */ }
func (l dataStructList) GetAttribute(base.Attribute) (base.AttributeSpec, error)  { /* ... */ }
func (l dataStructList) AllAttributes() []base.Attribute  { /* ... */ }
func (l dataStructList) AddClassAttribute(base.Attribute) error  { /* ... */ }
func (l dataStructList) RemoveClassAttribute(base.Attribute) error  { /* ... */ }
func (l dataStructList) AllClassAttributes() []base.Attribute  { /* ... */ }
func (l dataStructList) Get(base.AttributeSpec, int) []byte  { /* ... */ }
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:

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

Or

var results dataStructList // you can use results as FixedDataGrid
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:

func CreateModel(c echo.Context) error {
fmt.Println(&quot;====&gt; Entry CreateModel function&quot;)
//var results []dataStruct
var Success bool = false
Db := db.MgoDb{}
Db.Init()
defer Db.Close()
col := Db.C(db.TrainingDataCollection)
var results dataStructList // &lt;--- use the type that implements the interface
if err := col.Find(nil).All(&amp;results); err != nil { // &lt;-- pass a pointer to results
fmt.Println(&quot;ERROR WHILE GETTING THE TRAINING DATA&quot;)
} else {
//fmt.Println(&quot;Results All: &quot;, results)
Success = true
}
fmt.Println(&quot;=============&quot;, results)
//Initialises a new KNN classifier
cls := knn.NewKnnClassifier(&quot;euclidean&quot;, &quot;linear&quot;, 2)
//Do a training-test split
trainData, testData := base.InstancesTrainTestSplit(results, 0.55) // &lt;-- this will work because results if of type dataStructList, which implements the base.FixedDataGrid interface.
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(&quot;Unable to get confusion matrix: %s&quot;, err.Error()))
}
fmt.Println(evaluation.GetSummary(confusionMat))
return c.JSON(http.StatusOK, Success)
}

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:

确定