|
1 | | -import kMeans from '../kmeans'; |
| 1 | +import KMeans from '../kMeans'; |
2 | 2 |
|
3 | 3 | describe('kMeans', () => { |
4 | 4 | it('should throw an error on invalid data', () => { |
5 | 5 | expect(() => { |
6 | | - kMeans(); |
7 | | - }).toThrowError('Either dataSet or labels or toClassify were not set'); |
| 6 | + KMeans(); |
| 7 | + }).toThrowError('The data is empty'); |
8 | 8 | }); |
9 | 9 |
|
10 | 10 | it('should throw an error on inconsistent data', () => { |
11 | 11 | expect(() => { |
12 | | - kMeans([[1, 2], [1]], 2); |
13 | | - }).toThrowError('Inconsistent vector lengths'); |
| 12 | + KMeans([[1, 2], [1]], 2); |
| 13 | + }).toThrowError('Matrices have different shapes'); |
14 | 14 | }); |
15 | 15 |
|
16 | 16 | it('should find the nearest neighbour', () => { |
17 | | - const dataSet = [[1, 1], [6, 2], [3, 3], [4, 5], [9, 2], [2, 4], [8, 7]]; |
| 17 | + const data = [[1, 1], [6, 2], [3, 3], [4, 5], [9, 2], [2, 4], [8, 7]]; |
18 | 18 | const k = 2; |
19 | | - const expectedCluster = [0, 1, 0, 1, 1, 0, 1]; |
20 | | - expect(kMeans(dataSet, k)).toEqual(expectedCluster); |
| 19 | + const expectedClusters = [0, 1, 0, 1, 1, 0, 1]; |
| 20 | + expect(KMeans(data, k)).toEqual(expectedClusters); |
| 21 | + |
| 22 | + expect(KMeans([[0, 0], [0, 1], [10, 10]], 2)).toEqual( |
| 23 | + [0, 0, 1], |
| 24 | + ); |
21 | 25 | }); |
22 | 26 |
|
23 | 27 | it('should find the clusters with equal distances', () => { |
24 | 28 | const dataSet = [[0, 0], [1, 1], [2, 2]]; |
25 | 29 | const k = 3; |
26 | 30 | const expectedCluster = [0, 1, 2]; |
27 | | - expect(kMeans(dataSet, k)).toEqual(expectedCluster); |
| 31 | + expect(KMeans(dataSet, k)).toEqual(expectedCluster); |
28 | 32 | }); |
29 | 33 |
|
30 | 34 | it('should find the nearest neighbour in 3D space', () => { |
31 | 35 | const dataSet = [[0, 0, 0], [0, 1, 0], [2, 0, 2]]; |
32 | 36 | const k = 2; |
33 | 37 | const expectedCluster = [1, 1, 0]; |
34 | | - expect(kMeans(dataSet, k)).toEqual(expectedCluster); |
| 38 | + expect(KMeans(dataSet, k)).toEqual(expectedCluster); |
35 | 39 | }); |
36 | 40 | }); |
0 commit comments