Skip to content

Commit

Permalink
chore: adding a TODO to check datatype when validating matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonShin committed Nov 7, 2018
1 parent c7bc3a6 commit ad8e7b2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib/cluster/k_means.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export class KMeans {
* @returns {number[]}
*/
public predict(X: Type2DMatrix<number>): number[] {
validateMatrix2D(X);
return _.map(X, data => {
return this.getClosestCentroids(data, this.centroids, this.distance);
});
Expand Down
12 changes: 12 additions & 0 deletions test/clusters/k_means.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,16 @@ describe('clusters:k_means', () => {
const pred2 = kmean.predict(predVector2);
expect(_.isEqual(expectedResult, pred2)).toBe(true);
});

it('should not fit none 2D matrix', () => {
const kmean = new KMeans({ k: 2 });
expect(() => kmean.fit([1, 2])).toThrow(
'The matrix is not 2D shaped: [1,2] of [2]'
);
expect(() => kmean.fit(null)).toThrow(
'values passed to tensor(values) must be an array of numbers or booleans, or a TypedArray'
);
// TODO: implement datatype check to the validation method
// expect(() => kmean.fit([["aa", "bb"]])).toThrow('The matrix is not 2D shaped: [1,2] of [2]');
});
});

0 comments on commit ad8e7b2

Please sign in to comment.