-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathseparating_dataset.go
72 lines (58 loc) · 1.46 KB
/
separating_dataset.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
package main
import (
"bufio"
"github.com/kniren/gota/dataframe"
"log"
"os"
)
func main() {
// Open the diabetes dataset file.
f, err := os.Open("diabetes.csv")
if err != nil {
log.Fatal(err)
}
defer f.Close()
// Create a dataframe from the CSV file.
// The types of the columns will be inferred.
diabetesDF := dataframe.ReadCSV(f)
// Calculate the number of elements in each set.
trainingNum := (4 * diabetesDF.Nrow()) / 5
testNum := diabetesDF.Nrow() / 5
if trainingNum+testNum < diabetesDF.Nrow() {
trainingNum++
}
// Create the subset indices.
trainingIdx := make([]int, trainingNum)
testIdx := make([]int, testNum)
// Enumerate the training indices.
for i := 0; i < trainingNum; i++ {
trainingIdx[i] = i
}
// Enumerate the test indices.
for i := 0; i < testNum; i++ {
testIdx[i] = trainingNum + i
}
// Create the subset dataframes.
trainingDF := diabetesDF.Subset(trainingIdx)
testDF := diabetesDF.Subset(testIdx)
// Create a map that will be used in writing the data
// to files.
setMap := map[int]dataframe.DataFrame{
0: trainingDF,
1: testDF,
}
// Create the respective files.
for idx, setName := range []string{"training.csv", "test.csv"} {
// Save the filtered dataset file.
f, err := os.Create(setName)
if err != nil {
log.Fatal(err)
}
// Create a buffered writer.
w := bufio.NewWriter(f)
// Write the dataframe out as a CSV.
if err := setMap[idx].WriteCSV(w); err != nil {
log.Fatal(err)
}
}
}