2020-11-04 17:07:37 +05:30
package base
import (
"fmt"
"reflect"
"strconv"
2020-11-27 06:07:20 -07:00
"github.com/rocketlaunchr/dataframe-go"
2020-11-04 17:07:37 +05:30
)
// 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.i
2020-11-27 06:07:20 -07:00
func ConvertDataFrameToInstances ( df * dataframe . DataFrame , classAttrIndex int ) FixedDataGrid {
2020-11-04 17:07:37 +05:30
2020-11-27 06:07:20 -07:00
// Creating Attributes based on Dataframe
2020-11-04 17:07:37 +05:30
names := df . Names ( )
2020-11-27 06:07:20 -07:00
attrs := make ( [ ] Attribute , len ( names ) )
2020-11-04 17:07:37 +05:30
2020-11-27 06:07:20 -07:00
newInst := NewDenseInstances ( )
2020-11-04 17:07:37 +05:30
for i := range names {
col := df . Series [ i ]
if reflect . TypeOf ( col . Value ( 0 ) ) . Kind ( ) == reflect . String {
2020-11-27 06:07:20 -07:00
attrs [ i ] = new ( CategoricalAttribute )
2020-11-04 17:07:37 +05:30
attrs [ i ] . SetName ( names [ i ] )
} else {
2020-11-27 06:07:20 -07:00
attrs [ i ] = NewFloatAttribute ( names [ i ] )
2020-11-04 17:07:37 +05:30
}
}
// Add the attributes
2020-11-27 06:07:20 -07:00
newSpecs := make ( [ ] AttributeSpec , len ( attrs ) )
2020-11-04 17:07:37 +05:30
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
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 ) )
}
}
return newInst
}