forked from sjwhitworth/golearn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instances.go
90 lines (72 loc) · 2.65 KB
/
instances.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
// This example program demonstrates Instances
import (
"fmt"
"github.com/sjwhitworth/golearn/base"
)
func main() {
// Instances can be read using ParseCsvToInstances
rawData, err := base.ParseCSVToInstances("../datasets/iris_headers.csv", true)
if err != nil {
panic(err)
}
// Instances can be printed, and you'll see a human-readable summary
// if you do so. The first section is a line like
// Instances with 150 row(s) and 5 attribute(s)
//
// It next prints all the attributes
// FloatAttribute(Sepal length)
// FloatAttribute(Sepal width)
// FloatAttribute(Petal length)
// FloatAttribute(Petal width)
// CategoricalAttribute([Iris-setosa Iris-versicolor Iris-viriginica])
// The final attribute has an asterisk (*) printed before it,
// meaning that it is the class variable. It then prints out up to
// 30 rows which correspond to those attributes.
// 5.10 3.50 1.40 0.20 Iris-setosa
// 4.90 3.00 1.40 0.20 Iris-setosa
fmt.Println(rawData)
// If two decimal places isn't enough, you can update the
// Precision field on any FloatAttribute
if attr, ok := rawData.AllAttributes()[0].(*base.FloatAttribute); !ok {
panic("Invalid cast")
} else {
attr.Precision = 4
}
// Now the first column has more precision
fmt.Println(rawData)
// We can update the set of Instances, although the API
// for doing so is not very sophisticated.
// First, have to resolve Attribute Specifications
as := base.ResolveAttributes(rawData, rawData.AllAttributes())
// Attribute Specifications describe where a given column lives
rawData.Set(as[0], 0, as[0].GetAttribute().GetSysValFromString("1.00"))
// A SetClass method exists as a shortcut
base.SetClass(rawData, 0, "Iris-unusual")
fmt.Println(rawData)
// There is a way of creating new Instances from scratch.
// Inside an Instance, everything's stored as float64
newData := make([]float64, 2)
newData[0] = 1.0
newData[1] = 0.0
// Let's create some attributes
attrs := make([]base.Attribute, 2)
attrs[0] = base.NewFloatAttribute("Arbitrary Float Quantity")
attrs[1] = new(base.CategoricalAttribute)
attrs[1].SetName("Class")
// Insert a standard class
attrs[1].GetSysValFromString("A")
// Now let's create the final instances set
newInst := base.NewDenseInstances()
// Add the attributes
newSpecs := make([]base.AttributeSpec, len(attrs))
for i, a := range attrs {
newSpecs[i] = newInst.AddAttribute(a)
}
// Allocate space
newInst.Extend(1)
// Write the data
newInst.Set(newSpecs[0], 0, newSpecs[0].GetAttribute().GetSysValFromString("1.0"))
newInst.Set(newSpecs[1], 0, newSpecs[1].GetAttribute().GetSysValFromString("A"))
fmt.Println(newInst)
}