Skip to content

Commit

Permalink
refactor: normalize refactor -> new validation API
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonShin committed Nov 8, 2018
1 parent c78f213 commit c6bbb07
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 31 deletions.
29 changes: 12 additions & 17 deletions src/lib/preprocessing/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ interface NumberOneHot {
*
* @example
* import { add_dummy_feature } from 'kalimdor/preprocessing';
* const dummy = add_dummy_feature({ X: [[0, 1, 2], [1, 0, 3]] });
* const dummy = add_dummy_feature([[0, 1, 2], [1, 0, 3]]);
* console.log(dummy); // returns: [ [ 1, 0, 1, 2 ], [ 1, 1, 0, 3 ] ]
*
* @param X - A matrix of data
Expand Down Expand Up @@ -633,15 +633,13 @@ export class PolynomialFeatures {
* to the square of the β values, while the L1 norm is proportional the absolute value of the values in β .
*
* @example
* import { normalize } from 'kalimdor/preprocess';
* import { normalize } from 'kalimdor/preprocessing';
*
* const result = normalize({
* X: [
* [1, -1, 2],
* [2, 0, 0],
* [0, 1, -1],
* ],
* });
* const result = normalize([
* [1, -1, 2],
* [2, 0, 0],
* [0, 1, -1],
* ], { norm: 'l2' });
* console.log(result);
* // [ [ 0.4082482904638631, -0.4082482904638631, 0.8164965809277261 ],
* // [ 1, 0, 0 ],
Expand All @@ -652,22 +650,19 @@ export class PolynomialFeatures {
* @return number[][]
*/
export function normalize(
X: Type2DMatrix<number> = null,
{
X = null,
norm = 'l2'
}: {
X: number[][];
norm?: string;
norm: string;
} = {
X: null,
norm: 'l2'
}
): number[][] {
// Validation
if (!math.contrib.isMatrixOf(X, 'number')) {
throw new Error('The data input must be a matrix of numbers');
if (Array.isArray(X) && X.length === 0) {
throw new TypeError('X cannot be empty');
}

validateMatrix2D(X);
const normalizedMatrix = [];
for (let i = 0; i < X.length; i++) {
const row = X[i];
Expand Down
20 changes: 6 additions & 14 deletions test/preprocessing/data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,29 +262,21 @@ describe('data:normalize', () => {
[1, 0, 0],
[0, 0.7071067811865475, -0.7071067811865475]
];
const result = normalize({
X: X1,
norm: 'l2'
});
const result = normalize(X1, { norm: 'l2' });
expect(result).toEqual(expected);
});
it('should normalize X1 with l1 norm', () => {
const expected = [[0.25, -0.25, 0.5], [1, 0, 0], [0, 0.5, -0.5]];
const result = normalize({
X: X1,
norm: 'l1'
});
const result = normalize(X1, { norm: 'l1' });
expect(result).toEqual(expected);
});
it('should throw an error if unrecognised norm is passed in', () => {
const expected = 'test is not a recognised normalization method';
expect(() => normalize({ X: X1, norm: 'test' })).toThrow(expected);
expect(() => normalize(X1, { norm: 'test' })).toThrow(expected);
});
it('should throw an error if the input is invalid', () => {
const nullException =
'Cannot perform isMatrixOf number unless the data is matrix';
expect(() => normalize({ X: null, norm: 'l1' })).toThrow(nullException);
expect(() => normalize({ X: [], norm: 'l1' })).toThrow(nullException);
expect(() => normalize({ X: 'aisjd', norm: 'l1' })).toThrow(nullException);
expect(() => normalize(null, { norm: 'l1' })).toThrow(tensorErr);
expect(() => normalize([], { norm: 'l1' })).toThrow('X cannot be empty');
expect(() => normalize('aisjd', { norm: 'l1' })).toThrow(tensorErr);
});
});

0 comments on commit c6bbb07

Please sign in to comment.