Skip to content

Commit

Permalink
mlNormalize
Browse files Browse the repository at this point in the history
  • Loading branch information
mzusin committed Feb 20, 2024
1 parent 84afba5 commit 34a021c
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 22 deletions.
4 changes: 2 additions & 2 deletions dist/mz-math.esm.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions dist/mz-math.esm.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/mz-math.min.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions dist/mz-math.min.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/mz-math.node.cjs

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions dist/mz-math.node.cjs.map

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/index-esm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export * from './main/collision-detection';
export * from './main/animation';
export * from './main/circle-ellipse';
export * from './main/sequence';
export * from './main/statistics';
export * from './main/statistics';
export * from './main/ml';
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import * as animation from './main/animation';
import * as circleEllipse from './main/circle-ellipse';
import * as sequence from './main/sequence';
import * as statistics from './main/statistics';
import * as ml from './main/ml';

const api = {
...vector,
Expand All @@ -40,6 +41,7 @@ const api = {
...circleEllipse,
...sequence,
...statistics,
...ml,
};

declare global {
Expand Down Expand Up @@ -69,4 +71,5 @@ export * from './main/collision-detection';
export * from './main/animation';
export * from './main/circle-ellipse';
export * from './main/sequence';
export * from './main/statistics';
export * from './main/statistics';
export * from './main/ml';
21 changes: 21 additions & 0 deletions src/main/ml.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { setDecimalPlaces } from './format';

/**
* Returns a copy of array, where each value will be in the range [0, 1].
* const newValue = (value - array_min) / (array_max - array_min)
*/
export const mlNormalize = (data: number[], decimalPlaces = Infinity): number[] => {
const copy = [...data];

const min = Math.min(...copy);
const max = Math.max(...copy);

const diff = max - min;
if(diff === 0) return [0];

for(let i=0; i<copy.length; i++) {
copy[i] = setDecimalPlaces((copy[i] - min) / diff, decimalPlaces);
}

return copy;
};
3 changes: 1 addition & 2 deletions src/main/statistics.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { setDecimalPlaces } from './format';

// -------------------- CENTRAL TENDENCY ----------------------------

import { setDecimalPlaces } from './format';

/**
* Central tendency: Calculate the Average (mean)
* Sum of all numbers divided by the array length.
Expand Down
27 changes: 27 additions & 0 deletions test/ml.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { mlNormalize } from '../src/main/ml';

describe('ML', () => {

describe('mlNormalize()', () => {

test('[]', () => {
expect(mlNormalize([])).toStrictEqual([]);
});

test('[2]', () => {
expect(mlNormalize([2])).toStrictEqual([0]);
});

test('[1, 2, 3]', () => {
expect(mlNormalize([1, 2, 3])).toStrictEqual([0, 0.5, 1]);
});

test('[-1, -2]', () => {
expect(mlNormalize([-1, -2])).toStrictEqual([1, 0]);
});

test('[1, 1, 2, 3, 4]', () => {
expect(mlNormalize([1, 1, 2, 3, 4], 2)).toStrictEqual([0, 0, 0.33, 0.67, 1]);
});
});
});

0 comments on commit 34a021c

Please sign in to comment.