-
Notifications
You must be signed in to change notification settings - Fork 0
/
scaler_test.go
102 lines (93 loc) · 3.64 KB
/
scaler_test.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
package preprocessing
import (
"testing"
"math"
"github.com/rom1mouret/ml-essentials/dataframe"
u "github.com/rom1mouret/ml-essentials/utils"
)
func TestMinMaxScaling(t *testing.T) {
builder := dataframe.DataBuilder{RawData: dataframe.NewRawData()}
df := builder.AddFloats("col", 1.0, 1.0, math.NaN(), 2.0, 2.0).ToDataFrame()
imputer := NewScaler(ScalerOptions{MinMax: true})
imputer.Fit(df)
df = df.IndexView([]int{0, 1, 3, 4})
err := imputer.TransformInplace(df)
if err != nil {
panic(err.Error())
}
vals := df.Floats("col")
u.AssertFloatEquals("vals[0]", vals.Get(0), 0.0, t)
u.AssertFloatEquals("vals[1]", vals.Get(1), 0.0, t)
u.AssertFloatEquals("vals[2]", vals.Get(2), 1.0, t)
u.AssertFloatEquals("vals[3]", vals.Get(3), 1.0, t)
imputer.InverseTransformInplace(df)
u.AssertFloatEquals("vals[0]", vals.Get(0), 1.0, t)
u.AssertFloatEquals("vals[1]", vals.Get(1), 1.0, t)
u.AssertFloatEquals("vals[2]", vals.Get(2), 2.0, t)
u.AssertFloatEquals("vals[3]", vals.Get(3), 2.0, t)
}
func TestCenteringAlone(t *testing.T) {
builder := dataframe.DataBuilder{RawData: dataframe.NewRawData()}
df := builder.AddFloats("col", math.NaN(), 1.0, 1.0, 2.0, 2.0).ToDataFrame()
imputer := NewScaler(ScalerOptions{Centering: true})
imputer.Fit(df)
df = df.IndexView([]int{1, 2, 3, 4})
err := imputer.TransformInplace(df)
if err != nil {
panic(err.Error())
}
vals := df.Floats("col")
u.AssertFloatEquals("vals[0]", vals.Get(0), -0.5, t)
u.AssertFloatEquals("vals[1]", vals.Get(1), -0.5, t)
u.AssertFloatEquals("vals[2]", vals.Get(2), 0.5, t)
u.AssertFloatEquals("vals[3]", vals.Get(3), 0.5, t)
imputer.InverseTransformInplace(df)
u.AssertFloatEquals("vals[0]", vals.Get(0), 1.0, t)
u.AssertFloatEquals("vals[1]", vals.Get(1), 1.0, t)
u.AssertFloatEquals("vals[2]", vals.Get(2), 2.0, t)
u.AssertFloatEquals("vals[3]", vals.Get(3), 2.0, t)
}
func TestScalingAlone(t *testing.T) {
builder := dataframe.DataBuilder{RawData: dataframe.NewRawData()}
df := builder.AddFloats("col", 1.0, 1.0, 2.0, 2.0, math.NaN()).ToDataFrame()
imputer := NewScaler(ScalerOptions{Scaling: true})
imputer.Fit(df)
df = df.IndexView([]int{0, 1, 2, 3})
err := imputer.TransformInplace(df)
if err != nil {
panic(err.Error())
}
vals := df.Floats("col")
u.AssertFloatEquals("vals[0]", vals.Get(0), 2.0, t)
u.AssertFloatEquals("vals[1]", vals.Get(1), 2.0, t)
u.AssertFloatEquals("vals[2]", vals.Get(2), 4.0, t)
u.AssertFloatEquals("vals[3]", vals.Get(3), 4.0, t)
// reverse
imputer.InverseTransformInplace(df)
u.AssertFloatEquals("vals[0]", vals.Get(0), 1.0, t)
u.AssertFloatEquals("vals[1]", vals.Get(1), 1.0, t)
u.AssertFloatEquals("vals[2]", vals.Get(2), 2.0, t)
u.AssertFloatEquals("vals[3]", vals.Get(3), 2.0, t)
}
func TestCenteringScaling(t *testing.T) {
builder := dataframe.DataBuilder{RawData: dataframe.NewRawData()}
df := builder.AddFloats("col", 1.0, 1.0, 2.0, 2.0, math.NaN()).ToDataFrame()
imputer := NewScaler(ScalerOptions{Centering: true, Scaling: true})
imputer.Fit(df)
df = df.IndexView([]int{0, 1, 2, 3})
err := imputer.TransformInplace(df)
if err != nil {
panic(err.Error())
}
vals := df.Floats("col")
u.AssertFloatEquals("vals[0]", vals.Get(0), -1.0, t)
u.AssertFloatEquals("vals[1]", vals.Get(1), -1.0, t)
u.AssertFloatEquals("vals[2]", vals.Get(2), 1.0, t)
u.AssertFloatEquals("vals[3]", vals.Get(3), 1.0, t)
// reverse
imputer.InverseTransformInplace(df)
u.AssertFloatEquals("vals[0]", vals.Get(0), 1.0, t)
u.AssertFloatEquals("vals[1]", vals.Get(1), 1.0, t)
u.AssertFloatEquals("vals[2]", vals.Get(2), 2.0, t)
u.AssertFloatEquals("vals[3]", vals.Get(3), 2.0, t)
}