-
Notifications
You must be signed in to change notification settings - Fork 0
/
scaler.go
187 lines (176 loc) · 4.75 KB
/
scaler.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
package preprocessing
import (
"math"
"github.com/rom1mouret/ml-essentials/utils"
"github.com/rom1mouret/ml-essentials/dataframe"
)
type ScalerOptions struct {
// if true, Centering and Scaling are ignored
MinMax bool
// with mean
Centering bool
// with std
Scaling bool
}
// Scaler is a json-serializable structure that lets you center and scale
// float features to avoid your models to be biased towards certain features
// or face initialization issues.
type Scaler struct {
Shift map[string]float64
Scale map[string]float64
options ScalerOptions
}
// NewScaler allocates a new Scaler
func NewScaler(opt ScalerOptions) *Scaler {
scaler := new(Scaler)
scaler.options = opt
return scaler
}
type fittingResult struct {
shift *float64
scale *float64
}
func workerFits(df *dataframe.DataFrame, opt ScalerOptions, q utils.StringQ) {
for col := q.Next(); len(col) > 0; col = q.Next() {
access := df.Floats(col)
result := fittingResult{}
defer q.Notify(utils.ProcessedJob{Key: col, Result: &result})
// min max scaling
if opt.MinMax {
min := math.Inf(1)
max := math.Inf(-1)
for i := 0; i < access.Size(); i++ {
val := access.Get(i)
if val > max {
max = val
}
if val < min {
min = val
}
}
shift := -min
scale := max - min
result.shift = &shift
result.scale = &scale
} else if opt.Scaling || opt.Centering {
// TODO: bessell correction?
mean := 0.0
n := 0
for i := 0; i < access.Size(); i++ {
val := access.Get(i)
if !math.IsNaN(val) {
mean += val
n++
}
}
mean /= float64(n)
if opt.Centering {
shift := -mean
result.shift = &shift
}
if opt.Scaling {
squaresum := 0.0
for i := 0; i < access.Size(); i++ {
val := access.Get(i)
if !math.IsNaN(val) {
squaresum += (val - mean) * (val - mean)
}
}
if squaresum == 0 {
squaresum += 1
n++
}
scale := math.Sqrt(float64(n) / squaresum)
result.scale = &scale
}
}
}
}
// Fit implements PreprocTraining and InplaceTransform interfaces.
func (scaler *Scaler) Fit(df *dataframe.DataFrame) error {
columns := df.FloatHeader().NameList()
q := df.CreateColumnQueue(columns)
for i := 0; i < q.Workers; i++ {
go workerFits(df, scaler.options, q)
}
scaler.Shift = make(map[string]float64)
scaler.Scale = make(map[string]float64)
for _, job := range q.Results() {
result := job.Result.(*fittingResult)
if result.scale != nil {
scaler.Scale[job.Key] = *result.scale
}
if result.shift != nil {
scaler.Shift[job.Key] = *result.shift
}
}
return nil
}
func (scaler *Scaler) workerTransforms(df *dataframe.DataFrame, q utils.StringQ) {
for col := q.Next(); len(col) > 0; col = q.Next() {
defer q.Notify(utils.ProcessedJob{Key: col})
var ok bool
var scale float64
shift := scaler.Shift[col] // 0 by default
if scale, ok = scaler.Scale[col]; !ok {
scale = 1 // default scale
}
access := df.Floats(col)
for i := 0; i < access.Size(); i++ {
access.Set(i, (access.Get(i) + shift) * scale)
}
}
}
// TransformInplace implements InplaceTransform interface.
func (scaler *Scaler) TransformInplace(df *dataframe.DataFrame) error {
// divide into column groups
var m map[string]float64
if len(scaler.Scale) > len(scaler.Shift) {
m = scaler.Scale
} else {
m = scaler.Shift
}
if len(m) == 0 {
return nil
}
q := df.CreateColumnQueue(scaler.TransformedColumns())
defer q.Wait()
for i := 0; i < q.Workers; i++ {
go scaler.workerTransforms(df, q)
}
return nil
}
// InverseTransformInplace implements InverseInplaceTransform interface.
func (scaler *Scaler) InverseTransformInplace(df *dataframe.DataFrame) error {
var reverse Scaler
reverse.Shift = make(map[string]float64)
reverse.Scale = make(map[string]float64)
for col, shift := range scaler.Shift {
if scale, ok := scaler.Scale[col]; ok {
reverse.Shift[col] = -shift * scale // it's going to be divided by scale
} else {
reverse.Shift[col] = -shift
}
}
for col, scale := range scaler.Scale {
reverse.Scale[col] = 1 / scale
}
return reverse.TransformInplace(df)
}
// TransformedColumns implements PreprocTraining interface and InplaceTransform.
func (scaler *Scaler) TransformedColumns() []string {
if len(scaler.Shift) > 0 {
return mapKeys(scaler.Shift)
} else {
return mapKeys(scaler.Scale)
}
}
func mapKeys(set map[string]float64) []string {
result := make([]string, len(set))
i := 0
for key := range set {
result[i] = key
i++
}
return result
}