-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuilddf.go
283 lines (237 loc) · 5.3 KB
/
builddf.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package glasso
import (
"errors"
"log"
"sync"
"github.com/gonum/matrix/mat64"
)
var (
DimensionError = errors.New("wrong dimension")
LabelError = errors.New("missing labels for columns")
)
type DataFrame struct {
X *mat64.Dense
n, c int // memoize # rows, columns
labels []string // optional column names
}
func Mat64ToDF(mat *mat64.Dense) *DataFrame {
rows, cols := mat.Dims()
return &DataFrame{
X: mat,
n: rows,
c: cols,
}
}
func NewDataFrame(data [][]float64, labels ...[]string) *DataFrame {
rows := len(data)
cols := len(data[0])
x := make([]float64, 0, cols*rows)
for _, d := range data {
x = append(x, d...)
}
df := &DataFrame{
X: mat64.NewDense(rows, cols, x),
c: cols,
n: rows,
}
if len(labels) > 0 {
df.labels = labels[0]
}
return df
}
func (d *DataFrame) GetRow(i int) []float64 {
if i > d.n {
return nil
}
return mat64.Row(nil, i, d.X)
}
func (d *DataFrame) GetCol(j int) []float64 {
if j > d.c {
return nil
}
return mat64.Col(nil, j, d.X)
}
func (d *DataFrame) Rows() int { return d.n }
func (d *DataFrame) Cols() int { return d.c }
func (d *DataFrame) Data() *mat64.Dense {
return mat64.DenseCopyOf(d.X)
}
func (d *DataFrame) Copy() *DataFrame {
return &DataFrame{
X: d.Data(),
n: d.Rows(),
c: d.Cols(),
}
}
// Transform applies a function to the columns of the DataFrame.
// Cols indicates which columns to apply the function for.
// If nil, every column is evaluated.
func (d *DataFrame) Transform(f Evaluator, cols ...int) {
d.X.Apply(func(_, c int, v float64) float64 {
if containsInt(c, cols) && cols != nil {
return f(v)
}
return v
}, d.X)
}
// AppendCol appends a column to the end of the DataFrame.
func (d *DataFrame) AppendCol(col []float64) error {
if len(col) != d.n {
return DimensionError
}
d.X = mat64.DenseCopyOf(d.X.Grow(0, 1))
d.n, d.c = d.X.Dims()
d.X.SetCol(d.c-1, col)
return nil
}
// AppendCol appends a row to the end of the DataFrame.
func (d *DataFrame) AppendRow(row []float64) error {
if len(row) != d.c {
return DimensionError
}
d.X = mat64.DenseCopyOf(d.X.Grow(1, 0))
d.n, d.c = d.X.Dims()
d.X.SetRow(d.n-1, row)
return nil
}
// PushCol appends a column to the front of the DataFrame.
func (d *DataFrame) PushCol(col []float64) error {
if len(col) != d.n {
return DimensionError
}
d.c++
x := mat64.NewDense(d.n, d.c, nil)
x.SetCol(0, col)
for c := 1; c < d.c; c++ {
x.SetCol(c, d.GetCol(c-1))
}
d.X = x
return nil
}
// PushRow appends a row to the front of the DataFrame.
func (d *DataFrame) PushRow(row []float64) error {
if len(row) != d.c {
return DimensionError
}
d.n++
x := mat64.NewDense(d.n, d.c, nil)
x.SetRow(0, row)
for r := 1; r < d.n; r++ {
x.SetRow(r, d.GetRow(r-1))
}
d.X = x
return nil
}
// RemoveCol removes a specified column from the Dataframe.
func (d *DataFrame) RemoveCol(col int) error {
if col > d.c {
return DimensionError
}
tmp := mat64.NewDense(d.n, d.c-1, nil)
j := 0
for i := 0; i < d.c; i++ {
if i != col {
tmp.SetCol(j, d.GetCol(i))
j++
}
}
d.c--
d.X = tmp
return nil
}
// RemoveRow removes a specified row from the Dataframe
func (d *DataFrame) RemoveRow(row int) error {
if row > d.n {
return DimensionError
}
tmp := mat64.NewDense(d.n-1, d.c, nil)
j := 0
for i := 0; i < d.n; i++ {
if i != row {
tmp.SetRow(j, d.GetRow(i))
j++
}
}
d.n--
d.X = tmp
return nil
}
type Evaluator func(float64) float64
type Aggregator func([]float64) float64
// Similar to the R equivalent: apply a function across all rows / columns of a DataFrame.
// If margin == true, evaluate column wise, else evaluator rowwise.
// Each aggregation is run in a goroutine.
func (d *DataFrame) Apply(f Aggregator, margin bool, idxs ...int) []float64 {
if margin {
return d.ApplyCols(f, idxs)
}
return d.ApplyRows(f, idxs)
}
// ApplyCols aggregates values over the columns of the Dataframe.
func (d *DataFrame) ApplyCols(agg Aggregator, cols []int) []float64 {
if len(cols) > d.c {
log.Println("cannot apply function to more columns than present")
return nil
}
if len(cols) == 0 {
cols = seq(0, d.c-1, 1)
}
output := make([]float64, len(cols))
var wg sync.WaitGroup
for i, col := range cols {
if col > d.c {
log.Printf("Column Out of Range: %v > # columns(%v)", col, d.c)
return nil
}
wg.Add(1)
go func(i, c int) {
output[i] = agg(d.GetCol(c))
wg.Done()
}(i, col)
}
wg.Wait()
return output
}
// ApplyRows aggregates values over the rows of the Dataframe.
func (d *DataFrame) ApplyRows(agg Aggregator, rows []int) []float64 {
if len(rows) > d.n {
log.Println("cannot apply function to more rows than present")
return nil
}
if len(rows) == 0 {
rows = seq(0, d.n-1, 1)
}
output := make([]float64, len(rows))
var wg sync.WaitGroup
for i, row := range rows {
if row > d.n {
log.Printf("Row Out of Range: %v > # rows(%v)", row, d.n)
return nil
}
wg.Add(1)
go func(i, n int) {
output[i] = agg(d.GetRow(n))
wg.Done()
}(i, row)
}
wg.Wait()
return output
}
func (df *DataFrame) Standardize() {
d := df.X
n, p := d.Dims()
col := make([]float64, n)
for i := 0; i < p; i++ {
col = df.GetCol(i)
d.SetCol(i, standardize(col))
}
}
func (df *DataFrame) Normalize() {
d := df.X
n, p := d.Dims()
col := make([]float64, n)
for i := 0; i < p; i++ {
col = df.GetCol(i)
d.SetCol(i, normalize(col))
}
}