Skip to content

Commit 28398a3

Browse files
committed
improve some test
1 parent 53b32e4 commit 28398a3

File tree

4 files changed

+174
-34
lines changed

4 files changed

+174
-34
lines changed

tests/lib/model/c2p.test.js

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,45 @@ import C2P from '../../../lib/model/c2p.js'
33

44
import { randIndex } from '../../../lib/evaluate/clustering.js'
55

6-
test('clustering', () => {
7-
const model = new C2P(10, 50)
8-
const n = 20
9-
const x = Matrix.concat(
10-
Matrix.concat(Matrix.randn(n, 2, 0, 0.1), Matrix.randn(n, 2, 5, 0.1)),
11-
Matrix.randn(n, 2, [0, 5], 0.1)
12-
).toArray()
6+
describe('clustering', () => {
7+
test('default', () => {
8+
const model = new C2P(10, 50)
9+
const n = 20
10+
const x = Matrix.concat(
11+
Matrix.concat(Matrix.randn(n, 2, 0, 0.1), Matrix.randn(n, 2, 5, 0.1)),
12+
Matrix.randn(n, 2, [0, 5], 0.1)
13+
).toArray()
1314

14-
model.fit(x)
15-
const y = model.predict(3)
16-
expect(y).toHaveLength(x.length)
15+
model.fit(x)
16+
const y = model.predict(3)
17+
expect(y).toHaveLength(x.length)
1718

18-
const t = []
19-
for (let i = 0; i < x.length; i++) {
20-
t[i] = Math.floor(i / n)
21-
}
22-
const ri = randIndex(y, t)
23-
expect(ri).toBeGreaterThan(0.9)
19+
const t = []
20+
for (let i = 0; i < x.length; i++) {
21+
t[i] = Math.floor(i / n)
22+
}
23+
const ri = randIndex(y, t)
24+
expect(ri).toBeGreaterThan(0.9)
25+
})
26+
27+
test('no cutoff', () => {
28+
const model = new C2P(10, 50)
29+
model._cutoff_scale = 0
30+
const n = 20
31+
const x = Matrix.concat(
32+
Matrix.concat(Matrix.randn(n, 2, 0, 0.1), Matrix.randn(n, 2, 5, 0.1)),
33+
Matrix.randn(n, 2, [0, 5], 0.1)
34+
).toArray()
35+
36+
model.fit(x)
37+
const y = model.predict(3)
38+
expect(y).toHaveLength(x.length)
39+
40+
const t = []
41+
for (let i = 0; i < x.length; i++) {
42+
t[i] = Math.floor(i / n)
43+
}
44+
const ri = randIndex(y, t)
45+
expect(ri).toBeGreaterThan(0.9)
46+
})
2447
})

tests/lib/model/gtm.test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,52 @@ describe('clustering', () => {
6868
expect(y[0]).toBeGreaterThan(y[2])
6969
expect(y[1]).toBeGreaterThan(y[2])
7070
})
71+
72+
test('init random', () => {
73+
const model = new GTM(2, 1)
74+
model._init_method = 'random'
75+
const n = 50
76+
const x = Matrix.concat(
77+
Matrix.concat(Matrix.randn(n, 2, 0, 0.1), Matrix.randn(n, 2, 5, 0.1)),
78+
Matrix.randn(n, 2, [0, 5], 0.1)
79+
).toArray()
80+
81+
for (let i = 0; i < 100; i++) {
82+
model.fit(x)
83+
}
84+
const y = model.predictIndex(x)
85+
expect(y).toHaveLength(x.length)
86+
87+
const t = []
88+
for (let i = 0; i < x.length; i++) {
89+
t[i] = Math.floor(i / n)
90+
}
91+
const ri = randIndex(y, t)
92+
expect(ri).toBeGreaterThan(0.8)
93+
})
94+
95+
test('mode fit', () => {
96+
const model = new GTM(2, 1)
97+
model._fit_method = 'mode'
98+
const n = 50
99+
const x = Matrix.concat(
100+
Matrix.concat(Matrix.randn(n, 2, 0, 0.1), Matrix.randn(n, 2, 5, 0.1)),
101+
Matrix.randn(n, 2, [0, 5], 0.1)
102+
).toArray()
103+
104+
for (let i = 0; i < 100; i++) {
105+
model.fit(x)
106+
}
107+
const y = model.predict(x)
108+
expect(y).toHaveLength(x.length)
109+
110+
const t = []
111+
for (let i = 0; i < x.length; i++) {
112+
t[i] = Math.floor(i / n)
113+
}
114+
const ri = randIndex(y, t)
115+
expect(ri).toBeGreaterThan(0.8)
116+
})
71117
})
72118

73119
test('dimensionality reduction', () => {

tests/lib/model/radius_neighbor.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,23 @@ describe('classifier', () => {
4444
const acc = accuracy(y, t)
4545
expect(acc).toBeGreaterThan(0.95)
4646
})
47+
48+
test('same count', () => {
49+
const model = new RadiusNeighbor()
50+
model.fit(
51+
[
52+
[1, 0],
53+
[0, 1],
54+
],
55+
['a', 'b']
56+
)
57+
const y = model.predict([
58+
[0.1, 0.2],
59+
[0.2, 0.1],
60+
])
61+
expect(y[0]).toBe('b')
62+
expect(y[1]).toBe('a')
63+
})
4764
})
4865

4966
describe('regression', () => {
@@ -79,6 +96,18 @@ describe('regression', () => {
7996
const err = rmse(y, t)
8097
expect(err).toBeLessThan(0.5)
8198
})
99+
100+
test('outlier', () => {
101+
const model = new RadiusNeighborRegression()
102+
const x = Matrix.randn(50, 2, 0, 5).toArray()
103+
const t = []
104+
for (let i = 0; i < x.length; i++) {
105+
t[i] = x[i][0] + x[i][1] + (Math.random() - 0.5) / 10
106+
}
107+
model.fit(x, t)
108+
const y = model.predict([[100, 100]])
109+
expect(y[0]).toBeNull()
110+
})
82111
})
83112

84113
describe('semi-classifier', () => {

tests/lib/model/som.test.js

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,68 @@ import SOM from '../../../lib/model/som.js'
77
import { randIndex } from '../../../lib/evaluate/clustering.js'
88
import { coRankingMatrix } from '../../../lib/evaluate/dimensionality_reduction.js'
99

10-
test('clustering', () => {
11-
const model = new SOM(2, 1, 3)
12-
const n = 50
13-
const x = Matrix.concat(
14-
Matrix.concat(Matrix.randn(n, 2, 0, 0.1), Matrix.randn(n, 2, 5, 0.1)),
15-
Matrix.randn(n, 2, [0, 5], 0.1)
16-
).toArray()
10+
describe('clustering', () => {
11+
test('default', () => {
12+
const model = new SOM(2, 1)
13+
const n = 50
14+
const x = Matrix.concat(
15+
Matrix.concat(Matrix.randn(n, 2, 0, 0.1), Matrix.randn(n, 2, 5, 0.1)),
16+
Matrix.randn(n, 2, [0, 5], 0.1)
17+
).toArray()
1718

18-
for (let i = 0; i < 100; i++) {
19-
model.fit(x)
20-
}
21-
const y = model.predict(x)
22-
expect(y).toHaveLength(x.length)
19+
for (let i = 0; i < 100; i++) {
20+
model.fit(x)
21+
}
22+
const y = model.predict(x)
23+
expect(y).toHaveLength(x.length)
2324

24-
const t = []
25-
for (let i = 0; i < x.length; i++) {
26-
t[i] = Math.floor(i / n)
27-
}
28-
const ri = randIndex(y, t)
29-
expect(ri).toBeGreaterThan(0.9)
25+
expect([...new Set(y)].length).toBeLessThanOrEqual(20)
26+
})
27+
28+
test('init PCA', () => {
29+
const model = new SOM(2, 1, 3)
30+
const n = 50
31+
const x = Matrix.concat(
32+
Matrix.concat(Matrix.randn(n, 2, 0, 0.1), Matrix.randn(n, 2, 5, 0.1)),
33+
Matrix.randn(n, 2, [0, 5], 0.1)
34+
).toArray()
35+
36+
for (let i = 0; i < 100; i++) {
37+
model.fit(x)
38+
}
39+
const y = model.predict(x)
40+
expect(y).toHaveLength(x.length)
41+
42+
const t = []
43+
for (let i = 0; i < x.length; i++) {
44+
t[i] = Math.floor(i / n)
45+
}
46+
const ri = randIndex(y, t)
47+
expect(ri).toBeGreaterThan(0.9)
48+
})
49+
50+
test('init random', () => {
51+
const model = new SOM(2, 1, 3)
52+
model._init_method = 'random'
53+
const n = 50
54+
const x = Matrix.concat(
55+
Matrix.concat(Matrix.randn(n, 2, 0, 0.1), Matrix.randn(n, 2, 5, 0.1)),
56+
Matrix.randn(n, 2, [0, 5], 0.1)
57+
).toArray()
58+
59+
for (let i = 0; i < 100; i++) {
60+
model.fit(x)
61+
}
62+
const y = model.predict(x)
63+
expect(y).toHaveLength(x.length)
64+
65+
const t = []
66+
for (let i = 0; i < x.length; i++) {
67+
t[i] = Math.floor(i / n)
68+
}
69+
const ri = randIndex(y, t)
70+
expect(ri).toBeGreaterThan(0.9)
71+
})
3072
})
3173

3274
test('dimension reduction', () => {

0 commit comments

Comments
 (0)