Created Attribute Specifications (markdown)
parent
7d43a20804
commit
11bf0959c7
|
@ -0,0 +1,71 @@
|
|||
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**
|
||||
```go
|
||||
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 [`ResolveSomeAttributes`](https://godoc.org/github.com/sjwhitworth/golearn/base#ResolveAttributes), which retrieves a slice of `AttributeSpecs` for the attributes specified.
|
||||
|
||||
**Code excerpt: resolving the specifications of all non-class Attributes**
|
||||
```go
|
||||
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
|
||||
})
|
||||
|
||||
}
|
||||
```
|
Loading…
Reference in New Issue