-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathOrdinalEncoder.test.ts
More file actions
80 lines (76 loc) · 2.15 KB
/
Copy pathOrdinalEncoder.test.ts
File metadata and controls
80 lines (76 loc) · 2.15 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
80
import { OrdinalEncoder, setBackend } from '../index'
import { arrayTo2DColumn } from '../utils'
import * as tf from '@tensorflow/tfjs'
setBackend(tf)
describe('OrdinalEncoder', function () {
it('OrdinalEncoder works on array', function () {
const data = ['dog', 'cat', 'man', 'dog', 'cat', 'man', 'man', 'cat']
const X = arrayTo2DColumn(data)
const encode = new OrdinalEncoder()
encode.fit(X)
const expected = [[0], [1], [2], [0], [1], [2], [2], [1]]
expect(encode.transform(X).arraySync()).toEqual(expected)
expect(
encode.transform(arrayTo2DColumn(['man', 'cat'])).arraySync()
).toEqual([[2], [1]])
})
it('OrdinalEncoder works on 2DArray', function () {
const X = [
['Male', 1],
['Female', 2],
['Male', 4]
]
const encode = new OrdinalEncoder()
const expected = [
[0, 0],
[1, 1],
[0, 2]
]
expect(encode.fitTransform(X as any).arraySync()).toEqual(expected)
})
it('OrdinalEncoder can be passed categories', function () {
const X = [
['Male', 1],
['Female', 2],
['Male', 4]
]
const encode = new OrdinalEncoder({
categories: [
['Male', 'Female'],
[4, 2, 1]
]
})
const expected = [
[0, 2],
[1, 1],
[0, 0]
]
expect(encode.fitTransform(X as any).arraySync()).toEqual(expected)
})
it('OrdinalEncoder errors on values not seen in training', function () {
const X = [
['Male', 1],
['Female', 2],
['Male', 4]
]
const encode = new OrdinalEncoder()
encode.fit(X as any)
// Should throw an error on unknown input
expect(() => encode.transform([['Hello', 1]] as any)).toThrow()
})
it('OrdinalEncoder does not error on unknown values if you pass in defaults', function () {
const X = [
['Male', 1],
['Female', 2],
['Male', 4]
]
const encode = new OrdinalEncoder({
handleUnknown: 'useEncodedValue',
unknownValue: -1
})
encode.fit(X as any)
// Should throw an error on unknown input
const expected = encode.transform([['Hello', 1]] as any)
expect(expected.arraySync()).toEqual([[-1, 0]])
})
})