forked from NullHypothesis/mlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
161 lines (135 loc) · 2.71 KB
/
types.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
package mlgo
import (
"math"
)
type Vector []float64
// this definition creates problem in client program defining Matrix and Vector
// i.e. mlgo.Vector != Vector
//type Matrix []Vector
type Matrix [][]float64
const (
MaxValue = math.MaxFloat64
)
func Range(start, end int) (x []int) {
size := end - start
x = make([]int, size)
v := start
for i := 0; i < size; i++ {
x[i] = v
v++
}
return
}
func NewVector(m int, v float64) (x Vector) {
x = make(Vector, m)
for i := 0; i < m; i++ {
x[i] = v
}
return
}
func (x Vector) Reordered(index []int) (y Vector) {
y = make(Vector, len(index))
for i, v := range index {
y[i] = x[v]
}
return
}
func (x Vector) Mean() (m float64) {
for i := 0; i < len(x); i++ {
m += x[i]
}
m /= float64(len(x))
return
}
func (x Vector) Summarize() (mean, variance float64) {
var stats Summary
for _, v := range x {
// accumulate statistics
stats.Add(v)
}
mean, variance = stats.Mean, stats.VarP()
return
}
func (x Vector) Equal(y Vector) bool {
const epsilon = 1e-6
for i := range x {
if !EssentiallyEqual(x[i], y[i], epsilon) {
return false
}
}
return true
}
func (X Matrix) Summarize() (means, variances Vector) {
m := len(X)
if m < 2 { return }
n := len(X[0])
stats := make([]Summary, n)
means, variances = make(Vector, n), make(Vector, n)
for i := 0; i < m; i++ {
// accumulate statistics for each feature
for j, x := range X[i] {
stats[j].Add(x)
}
}
for j, _ := range stats {
means[j] = stats[j].Mean
variances[j] = stats[j].VarP()
}
return
}
func (X Matrix) Len() int {
return len(X)
}
// Less returns whether row i is lexicographically less than row j
func (X Matrix) Less(i, j int) bool {
for k := range X[i] {
if X[i][k] < X[j][k] {
return true
} else if X[i][k] > X[j][k] {
return false
}
// otherwise, X[i][k] == X[j][k] and continue to next position
}
// all positions are equal
return false
}
func (X Matrix) Swap(i, j int) {
X[i], X[j] = X[j], X[i]
}
// CopyMatrix returns a Matrix filled with content from Y
func CopyMatrix(Y [][]float64) (X Matrix) {
X = make(Matrix, len(Y))
for i := range Y {
X[i] = make(Vector, len(Y[i]))
copy(X[i], Y[i])
}
return
}
// Copied returns a copy of the Matrix.
func (X Matrix) Copied() (Y Matrix) {
Y = make(Matrix, len(X))
for i := range X {
Y[i] = make(Vector, len(X[i]))
copy(Y[i], X[i])
}
return
}
func (X Matrix) Slice(idx []int) (Y Matrix) {
Y = make(Matrix, len(idx))
for i, ii := range idx {
Y[i] = make(Vector, len(X[ii]))
copy(Y[i], X[ii])
}
return
}
func (X Matrix) Equal(Y Matrix) bool {
const epsilon = 1e-6
for i := range X {
for j := range X[i] {
if !EssentiallyEqual(X[i][j], Y[i][j], epsilon) {
return false
}
}
}
return true
}