forked from pa-m/sklearn
/
base.go
165 lines (144 loc) · 4.67 KB
/
base.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package datasets
import (
"bufio"
"encoding/csv"
"encoding/json"
"go/build"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"gonum.org/v1/gonum/mat"
)
// MLDataset structure returned by LoadIris,LoadBreastCancer,LoadDiabetes,LoadBoston
type MLDataset struct {
Data [][]float64 `json:"data,omitempty"`
Target []float64 `json:"target,omitempty"`
TargetNames []string `json:"target_names,omitempty"`
DESCR string `json:"DESCR,omitempty"`
FeatureNames []string `json:"feature_names,omitempty"`
X, Y *mat.Dense
}
// fix data path for travis
func realPath(filepath string) string {
if _, err := os.Stat(filepath); err != nil {
p := "/home/travis/gopath/src/github.com/"
lp := len(p)
if len(filepath) > lp && filepath[0:lp] == p {
filepath = "/home/travis/build/" + filepath[lp:]
}
}
return filepath
}
// LoadIris load the iris dataset
func loadJSON(filepath string) (ds *MLDataset) {
dat, err := ioutil.ReadFile(realPath(filepath))
check(err)
ds = &MLDataset{}
err = json.Unmarshal(dat, &ds)
check(err)
ds.X, ds.Y = ds.GetXY()
return
}
// LoadIris load the iris dataset
func LoadIris() (ds *MLDataset) {
return loadJSON(localPath("/src/github.com/RobinRCM/sklearn/datasets/data/iris.json"))
}
// LoadBreastCancer load the breat cancer dataset
func LoadBreastCancer() (ds *MLDataset) {
return loadJSON(localPath("/src/github.com/RobinRCM/sklearn/datasets/data/cancer.json"))
}
// LoadDiabetes load the diabetes dataset
func LoadDiabetes() (ds *MLDataset) {
return loadJSON(localPath("/src/github.com/RobinRCM/sklearn/datasets/data/diabetes.json"))
}
// LoadBoston load the boston housing dataset
func LoadBoston() (ds *MLDataset) {
return loadJSON(localPath("/src/github.com/RobinRCM/sklearn/datasets/data/boston.json"))
}
// LoadWine load the boston housing dataset
func LoadWine() (ds *MLDataset) {
return loadJSON(localPath("/src/github.com/RobinRCM/sklearn/datasets/data/wine.json"))
}
// GetXY returns X,Y matrices for dataset
func (ds *MLDataset) GetXY() (X, Y *mat.Dense) {
nSamples, nFeatures, nOutputs := len(ds.Data), len(ds.FeatureNames), 1
X = mat.NewDense(nSamples, nFeatures, nil)
X.Apply(func(i, j int, _ float64) float64 {
return ds.Data[i][j]
}, X)
Y = mat.NewDense(nSamples, nOutputs, nil)
Y.Apply(func(i, _ int, _ float64) float64 {
return ds.Target[i]
}, Y)
return
}
func localPath(s string) string {
p := strings.Split(build.Default.GOPATH, string(filepath.ListSeparator))[0]
return filepath.Join(p, s)
}
// LoadExamScore loads data from ex2data1 from Andrew Ng machine learning course
func LoadExamScore() (X, Y *mat.Dense) {
return loadCsv(localPath("/src/github.com/RobinRCM/sklearn/datasets/data/ex2data1.txt"), nil, 1)
}
// LoadMicroChipTest loads data from ex2data2 from Andrew Ng machine learning course
func LoadMicroChipTest() (X, Y *mat.Dense) {
return loadCsv(localPath("/src/github.com/RobinRCM/sklearn/datasets/data/ex2data2.txt"), nil, 1)
}
// LoadMnist loads mnist data 5000x400,5000x1
func LoadMnist() (X, Y *mat.Dense) {
mats := LoadOctaveBin(localPath("/src/github.com/RobinRCM/sklearn/datasets/data/ex4data1.dat.gz"))
return mats["X"], mats["y"]
}
// LoadMnistWeights loads mnist weights
func LoadMnistWeights() (Theta1, Theta2 *mat.Dense) {
mats := LoadOctaveBin(localPath("/src/github.com/RobinRCM/sklearn/datasets/data/ex4weights.dat.gz"))
return mats["Theta1"], mats["Theta2"]
}
func loadCsv(filepath string, setupReader func(*csv.Reader), nOutputs int) (X, Y *mat.Dense) {
f, err := os.Open(realPath(filepath))
check(err)
defer f.Close()
r := csv.NewReader(f)
if setupReader != nil {
setupReader(r)
}
cells, err := r.ReadAll()
check(err)
nSamples, nFeatures := len(cells), len(cells[0])-nOutputs
X = mat.NewDense(nSamples, nFeatures, nil)
X.Apply(func(i, j int, _ float64) float64 { x, err := strconv.ParseFloat(cells[i][j], 64); check(err); return x }, X)
Y = mat.NewDense(nSamples, nOutputs, nil)
Y.Apply(func(i, o int, _ float64) float64 {
y, err := strconv.ParseFloat(cells[i][nFeatures], 64)
check(err)
return y
}, Y)
return
}
func check(err error) {
if err != nil {
panic(err)
}
}
// LoadInternationalAirlinesPassengers ...
func LoadInternationalAirlinesPassengers() (Y *mat.Dense) {
f, err := os.Open(realPath(os.Getenv("GOPATH") + "/src/github.com/RobinRCM/sklearn/datasets/data/international-airline-passengers.csv"))
check(err)
defer f.Close()
fb := bufio.NewReader(f)
fb.ReadLine()
r := csv.NewReader(fb)
r.Comma = ','
cells, err := r.ReadAll()
check(err)
nSamples, nOutputs := len(cells), 1
Y = mat.NewDense(nSamples, nOutputs, nil)
Y.Apply(func(i, o int, _ float64) float64 {
y, err := strconv.ParseFloat(cells[i][1], 64)
check(err)
return y
}, Y)
return
}