|
| 1 | +import * as _ from 'lodash' |
| 2 | +import KMeans from './kmeans' |
| 3 | +import { assert } from 'chai' |
| 4 | +import { tensor2d } from '@tensorflow/tfjs-core' |
| 5 | + |
| 6 | +// TODO: Improve on kmeans cluster testing |
| 7 | +describe('clusters:k_means', () => { |
| 8 | + const X = tensor2d([ |
| 9 | + [1, 2], |
| 10 | + [1, 4], |
| 11 | + [1, 0], |
| 12 | + [4, 2], |
| 13 | + [4, 4], |
| 14 | + [4, 0] |
| 15 | + ]) |
| 16 | + |
| 17 | + const predVector1 = [ |
| 18 | + [0, 0], |
| 19 | + [4, 4] |
| 20 | + ] |
| 21 | + const predVector2 = [ |
| 22 | + [1298392183, 0], |
| 23 | + [0, 1] |
| 24 | + ] |
| 25 | + |
| 26 | + it('should fit vector1 + k=2 should return centroids of size 2 and clusters of size 2', () => { |
| 27 | + const expecterdCluster = { |
| 28 | + centroids: [ |
| 29 | + [2.5, 1], |
| 30 | + [2.5, 4] |
| 31 | + ], |
| 32 | + clusters: [ |
| 33 | + [ |
| 34 | + [1, 2], |
| 35 | + [1, 0], |
| 36 | + [4, 2], |
| 37 | + [4, 0] |
| 38 | + ], |
| 39 | + [ |
| 40 | + [1, 4], |
| 41 | + [4, 4] |
| 42 | + ] |
| 43 | + ], |
| 44 | + k: 2 |
| 45 | + } |
| 46 | + const kmean = new KMeans({ nClusters: 2, randomState: 0 }) |
| 47 | + kmean.fit(X) |
| 48 | + assert.deepEqual( |
| 49 | + expecterdCluster.centroids, |
| 50 | + kmean.clusterCenters.arraySync() |
| 51 | + ) |
| 52 | + }) |
| 53 | + |
| 54 | + // it('should fit vector1 + k=3 size 3 and clusters of size 2', () => { |
| 55 | + // const expectedCluster = { |
| 56 | + // centroids: [ |
| 57 | + // [2.5, 2], |
| 58 | + // [2.5, 4], |
| 59 | + // [2.5, 0] |
| 60 | + // ], |
| 61 | + // clusters: [ |
| 62 | + // [ |
| 63 | + // [1, 2], |
| 64 | + // [4, 2] |
| 65 | + // ], |
| 66 | + // [ |
| 67 | + // [1, 4], |
| 68 | + // [4, 4] |
| 69 | + // ], |
| 70 | + // [ |
| 71 | + // [1, 0], |
| 72 | + // [4, 0] |
| 73 | + // ] |
| 74 | + // ], |
| 75 | + // k: 3 |
| 76 | + // } |
| 77 | + // const kmean = new KMeans({ k: 3 }) |
| 78 | + // kmean.fit(vector1) |
| 79 | + // expect(_.isEqual(expectedCluster, kmean.toJSON())).toBe(true) |
| 80 | + // }) |
| 81 | + |
| 82 | + // it('should predict [2, 1] from predVector1 prediction', () => { |
| 83 | + // const expectedResult = [2, 1] |
| 84 | + // const kmean = new KMeans({ k: 3 }) |
| 85 | + // kmean.fit(vector1) |
| 86 | + // const pred = kmean.predict(predVector1) |
| 87 | + // expect(_.isEqual(pred, expectedResult)).toBe(true) |
| 88 | + // }) |
| 89 | + |
| 90 | + // it('should predict [ 0, 0 ] from predVector2 prediction', () => { |
| 91 | + // const expectedResult = [0, 0] |
| 92 | + // const kmean = new KMeans({ k: 3 }) |
| 93 | + // kmean.fit(vector1) |
| 94 | + // const pred = kmean.predict(predVector2) |
| 95 | + // expect(_.isEqual(pred, expectedResult)).toBe(true) |
| 96 | + // }) |
| 97 | + |
| 98 | + // it('should predict [ 0, 0 ] from predVector2 with X: vector1', () => { |
| 99 | + // const expectedResult = [0, 0] |
| 100 | + // const kmean = new KMeans({ k: 2 }) |
| 101 | + // kmean.fit(vector1) |
| 102 | + // const pred = kmean.predict(predVector2) |
| 103 | + // expect(_.isEqual(expectedResult, pred)).toBe(true) |
| 104 | + // }) |
| 105 | + |
| 106 | + // it('should predict the same after reloading the model', () => { |
| 107 | + // const expectedResult = [0, 0] |
| 108 | + // const kmean = new KMeans({ k: 2 }) |
| 109 | + // kmean.fit(vector1) |
| 110 | + // const pred1 = kmean.predict(predVector2) |
| 111 | + // expect(_.isEqual(expectedResult, pred1)).toBe(true) |
| 112 | + |
| 113 | + // const checkpoint = kmean.toJSON() |
| 114 | + |
| 115 | + // const kmean2 = new KMeans() |
| 116 | + // kmean2.fromJSON(checkpoint) |
| 117 | + // const pred2 = kmean.predict(predVector2) |
| 118 | + // expect(_.isEqual(expectedResult, pred2)).toBe(true) |
| 119 | + // }) |
| 120 | + |
| 121 | + // it('should not fit none 2D matrix', () => { |
| 122 | + // const kmean = new KMeans({ k: 2 }) |
| 123 | + |
| 124 | + // try { |
| 125 | + // kmean.fit([1, 2] as any) |
| 126 | + // } catch (err) { |
| 127 | + // expect(err).toBeInstanceOf(Validation2DMatrixError) |
| 128 | + // } |
| 129 | + // try { |
| 130 | + // kmean.fit(null as any) |
| 131 | + // } catch (err) { |
| 132 | + // expect(err).toBeInstanceOf(ValidationError) |
| 133 | + // } |
| 134 | + // // TODO: implement datatype check to the validation method |
| 135 | + // // expect(() => kmean.fit([["aa", "bb"]])).toThrow('The matrix is not 2D shaped: [1,2] of [2]'); |
| 136 | + // }) |
| 137 | +}) |
0 commit comments