From 76577c449be2e390e4a6fb95f907059c6f7034bd Mon Sep 17 00:00:00 2001 From: Ayush Date: Tue, 27 Oct 2020 13:50:42 +0530 Subject: [PATCH 1/2] Create dataframe_go.go Added Function to Convert from DataFrame to Golearn Fixed DataGrid --- base/dataframe_go.go | 74 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 base/dataframe_go.go diff --git a/base/dataframe_go.go b/base/dataframe_go.go new file mode 100644 index 0000000..29ae912 --- /dev/null +++ b/base/dataframe_go.go @@ -0,0 +1,74 @@ +package base + +import ( + "fmt" + "reflect" + "strconv" + + dataframe "github.com/rocketlaunchr/dataframe-go" + + "github.com/sjwhitworth/golearn/base" +) + +// ConvertDataFrameToInstances converts a DataFrame-go dataframe object to Golearn Fixed Data Grid. Allows for compabitibility between dataframe and golearn's ML models. +// df is the dataframe Object. classAttrIndex is the index of the class Attribute in the data. +func ConvertDataFrameToInstances(df *dataframe.DataFrame, classAttrIndex int) base.FixedDataGrid { + + // Creating Attributes based on Datafraem + names := df.Names() + attrs := make([]base.Attribute, len(names)) + + newInst := base.NewDenseInstances() + + for i := range names { + col := df.Series[i] + if reflect.TypeOf(col.Value(0)).Kind() == reflect.String { + attrs[i] = new(base.CategoricalAttribute) + attrs[i].SetName(names[i]) + } else { + attrs[i] = base.NewFloatAttribute(names[i]) + } + } + + // Add the attributes + newSpecs := make([]base.AttributeSpec, len(attrs)) + for i, a := range attrs { + + newSpecs[i] = newInst.AddAttribute(a) + } + // Adding the class attribute + newInst.AddClassAttribute(attrs[classAttrIndex]) + + // Allocate space + nRows := df.NRows() + newInst.Extend(df.NRows()) + + // Write the data based on DataType + for i := 0; i < nRows; i++ { + for j := range names { + col := df.Series[j] + + var val string + str, ok := col.Value(i).(string) + val = str + if ok != true { + int_64, ok := col.Value(i).(int64) + val = strconv.FormatInt(int_64, 10) + if ok != true { + float_64, ok := col.Value(i).(float64) + val = fmt.Sprintf("%f", float_64) + if ok != true { + float_32, ok := col.Value(i).(float32) + if ok == true { + val = fmt.Sprintf("%f", float_32) + } + } + } + } + newInst.Set(newSpecs[j], i, newSpecs[j].GetAttribute().GetSysValFromString(val)) + } + + } + + return newInst +} From 47b57c4835bab8a62b6d4d1edb25bd195dc51f34 Mon Sep 17 00:00:00 2001 From: Ayush Date: Wed, 4 Nov 2020 16:21:29 +0530 Subject: [PATCH 2/2] Replacing Nest with Switch Case --- base/dataframe_go.go | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/base/dataframe_go.go b/base/dataframe_go.go index 29ae912..e20118f 100644 --- a/base/dataframe_go.go +++ b/base/dataframe_go.go @@ -11,7 +11,7 @@ import ( ) // ConvertDataFrameToInstances converts a DataFrame-go dataframe object to Golearn Fixed Data Grid. Allows for compabitibility between dataframe and golearn's ML models. -// df is the dataframe Object. classAttrIndex is the index of the class Attribute in the data. +// df is the dataframe Object. classAttrIndex is the index of the class Attribute in the data.i func ConvertDataFrameToInstances(df *dataframe.DataFrame, classAttrIndex int) base.FixedDataGrid { // Creating Attributes based on Datafraem @@ -49,22 +49,17 @@ func ConvertDataFrameToInstances(df *dataframe.DataFrame, classAttrIndex int) ba col := df.Series[j] var val string - str, ok := col.Value(i).(string) - val = str - if ok != true { - int_64, ok := col.Value(i).(int64) - val = strconv.FormatInt(int_64, 10) - if ok != true { - float_64, ok := col.Value(i).(float64) - val = fmt.Sprintf("%f", float_64) - if ok != true { - float_32, ok := col.Value(i).(float32) - if ok == true { - val = fmt.Sprintf("%f", float_32) - } - } - } + switch v := col.Value(i).(type) { + case string: + val = v + case int64: + val = strconv.FormatInt(v, 10) + case float64: + val = fmt.Sprintf("%f", v) + case float32: + val = fmt.Sprintf("%f", v) } + newInst.Set(newSpecs[j], i, newSpecs[j].GetAttribute().GetSysValFromString(val)) }