-
Notifications
You must be signed in to change notification settings - Fork 3
/
scaling.go
67 lines (58 loc) · 1.96 KB
/
scaling.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
package math
import (
"github.com/bitflow-stream/go-bitflow/bitflow"
"github.com/bitflow-stream/go-bitflow/script/reg"
"github.com/bitflow-stream/go-bitflow/steps"
)
type MinMaxScaling struct {
Min float64
Max float64
}
func RegisterMinMaxScaling(b reg.ProcessorRegistry) {
b.RegisterBatchStep("scale_min_max",
func(params map[string]interface{}) (res bitflow.BatchProcessingStep, err error) {
res = &MinMaxScaling{
Min: params["min"].(float64),
Max: params["max"].(float64),
}
return
},
"Normalize a batch of samples using a min-max scale. The output value range is 0..1 by default, but can be customized.").
Optional("min", reg.Float(), 0.0).
Optional("max", reg.Float(), 1.0)
}
func RegisterStandardizationScaling(b reg.ProcessorRegistry) {
b.RegisterBatchStep("standardize",
func(_ map[string]interface{}) (res bitflow.BatchProcessingStep, err error) {
return new(StandardizationScaling), nil
},
"Normalize a batch of samples based on the mean and std-deviation")
}
func (s *MinMaxScaling) ProcessBatch(header *bitflow.Header, samples []*bitflow.Sample) (*bitflow.Header, []*bitflow.Sample, error) {
min, max := steps.GetMinMax(header, samples)
for _, sample := range samples {
for i, val := range sample.Values {
res := steps.ScaleMinMax(float64(val), min[i], max[i], s.Min, s.Max)
sample.Values[i] = bitflow.Value(res)
}
}
return header, samples, nil
}
func (s *MinMaxScaling) String() string {
return "Min-Max scaling"
}
type StandardizationScaling struct {
}
func (s *StandardizationScaling) ProcessBatch(header *bitflow.Header, samples []*bitflow.Sample) (*bitflow.Header, []*bitflow.Sample, error) {
stats := steps.GetStats(header, samples)
for _, sample := range samples {
for i, val := range sample.Values {
res := stats[i].ScaleStddev(float64(val))
sample.Values[i] = bitflow.Value(res)
}
}
return header, samples, nil
}
func (s *StandardizationScaling) String() string {
return "Standardization scaling"
}