-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathNormalizer.test.ts
More file actions
79 lines (74 loc) · 1.99 KB
/
Copy pathNormalizer.test.ts
File metadata and controls
79 lines (74 loc) · 1.99 KB
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
import { Normalizer, setBackend } from '../index'
import * as dfd from 'danfojs-node'
import { arrayEqual } from '../utils'
import * as tf from '@tensorflow/tfjs'
setBackend(tf)
describe('Normalizer', function () {
it('Standardize values in a DataFrame using a Normalizer (l1 case)', function () {
const data = [
[-1, 1],
[-6, 6],
[0, 10],
[10, 20]
]
const scaler = new Normalizer({ norm: 'l1' })
const expected = [
[-0.5, 0.5],
[-0.5, 0.5],
[0, 1],
[0.33, 0.66]
]
const transformedData = [[0.5, 0.5]]
scaler.fit(new dfd.DataFrame(data))
const resultDf = new dfd.DataFrame(
scaler.transform(new dfd.DataFrame(data))
)
expect(arrayEqual(resultDf.values, expected, 0.1)).toBe(true)
expect(scaler.transform([[2, 2]]).arraySync()).toEqual(transformedData)
})
it('fitTransform using a Normalizer (l2 case)', function () {
const data = [
[-1, 2],
[-3, 4],
[0, 10]
]
const scaler = new Normalizer()
const resultDf = scaler.fitTransform(data)
const expected = [
[-1 / Math.sqrt(5), 2 / Math.sqrt(5)],
[-0.6, 0.8],
[0, 1]
]
expect(arrayEqual(resultDf.arraySync(), expected, 0.1)).toBe(true)
})
it('fitTransform using a Normalizer (max case)', function () {
const data = [
[-1, 2],
[-3, 4],
[0, 10]
]
const scaler = new Normalizer({ norm: 'max' })
const resultDf = scaler.fitTransform(data)
const expected = [
[-0.5, 1],
[-0.75, 1],
[0, 1]
]
expect(arrayEqual(resultDf.arraySync(), expected, 0.1)).toBe(true)
})
it('fitTransform using a Normalizer (l1 case)', function () {
const data = [
[-1, 2],
[-3, 4],
[0, 0]
]
const scaler = new Normalizer({ norm: 'l1' })
const resultDf = scaler.fitTransform(data)
const expected = [
[-1 / 3, 2 / 3],
[-3 / 7, 4 / 7],
[0, 0]
]
expect(arrayEqual(resultDf.arraySync(), expected, 0.1)).toBe(true)
})
})