2
Attribute Specifications
Sentimentron edited this page 2014-10-29 06:24:48 -07:00
Attribute specifications (AttributeSpec
) describe the storage location of the data typed by a given Attribute
, and allow attributes to be re-ordered and filtered without generating unexpected behaviour.
Code excerpt: resolving the specification of an individual attribute
package main
import (
"fmt"
"github.com/sjwhitworth/golearn/base"
)
func main() {
// Read the CSV file
data, err := base.ParseCSVToInstances("iris_headers.csv", true)
// Error check
if err != nil {
panic(fmt.Sprintf("Couldn't load CSV file (error %s)", err))
}
// Resolve an Attribute
as, err := data.GetAttribute(base.NewFloatAttribute("Sepal length"))
if err != nil {
panic(fmt.Sprintf("Couldn't resolve AttributeSpec (error %s)", err))
}
// Print first column
asArr := []base.AttributeSpec{as}
data.MapOverRows(asArr, func(row [][]byte, i int) (bool, error) {
fmt.Println(base.UnpackBytesToFloat(row[0]))
return true, nil
})
}
For convenience, base
also provides ResolveAttributes
, which retrieves a slice of AttributeSpecs
for the attributes specified.
Code excerpt: resolving the specifications of all non-class Attributes
package main
import (
"fmt"
"github.com/sjwhitworth/golearn/base"
)
func main() {
// Read the CSV file
data, err := base.ParseCSVToInstances("iris_headers.csv", true)
// Error check
if err != nil {
panic(fmt.Sprintf("Couldn't load CSV file (error %s)", err))
}
// Resolve all non-class Attributes
asArr := base.ResolveAttributes(data, base.NonClassAttributes(data))
// (ResolveAllAttributes gets AttributeSpecs for every attribute)
// Print non-class data
data.MapOverRows(asArr, func(row [][]byte, i int) (bool, error) {
for _, b := range row {
fmt.Print(base.UnpackBytesToFloat(b), " ")
}
fmt.Println()
return true, nil
})
}