Skip to content

Commit

Permalink
ml
Browse files Browse the repository at this point in the history
  • Loading branch information
mzusin committed Feb 21, 2024
1 parent 711f993 commit f36a8bd
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 39 deletions.
4 changes: 2 additions & 2 deletions dist/mz-math.esm.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 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.

6 changes: 3 additions & 3 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.

6 changes: 3 additions & 3 deletions dist/mz-math.node.cjs.map

Large diffs are not rendered by default.

42 changes: 33 additions & 9 deletions src/main/ml.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,50 @@
import { setDecimalPlaces } from './format';
import { getArithmeticMean, getStandardDeviation } from './statistics';
import { IMlStandardizeResult } from '../types';
import { IMlNormalizeResult, IMlStandardizeResult } from '../types';

// --------------------- NORMALIZE --------------------------------

/**
* Changes value to be in the range [0, 1].
*/
export const mlNormalizeValue = (value: number, min: number, max: number, decimalPlaces = Infinity) : number => {
const diff = max - min;
if(diff === 0) return 0;
return setDecimalPlaces((value - min) / diff, decimalPlaces);
};

/**
* Returns a copy of array, where each value will be in the range [0, 1].
*/
export const mlNormalize = (data: number[], decimalPlaces = Infinity): number[] => {
export const mlNormalizeArray = (data: number[], min: number, max: 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);
copy[i] = mlNormalizeValue(copy[i], min, max, decimalPlaces);
}

return copy;
};

export const mlNormalizeTestData = (data: number[], decimalPlaces = Infinity): IMlNormalizeResult => {
const min = Math.min(...data);
const max = Math.max(...data);
const _data = mlNormalizeArray(data, min, max, decimalPlaces);

return {
min: setDecimalPlaces(min, decimalPlaces),
max: setDecimalPlaces(max, decimalPlaces),
data: _data,
};
};

/**
* Alias.
*/
export const mlNormalizeUnseenData = (data: number[], min: number, max: number, decimalPlaces = Infinity): number[] => {
return mlNormalizeArray(data, min, max, decimalPlaces);
};

// --------------------- STANDARDIZE --------------------------------

/**
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,10 @@ export interface IMlStandardizeResult {
mean: number;
stdDev: number;
data: number[];
}

export interface IMlNormalizeResult {
min: number;
max: number;
data: number[];
}
26 changes: 11 additions & 15 deletions test/ml.test.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
import { mlNormalize, mlStandardizeValue } from '../src/main/ml';
import { mlNormalizeValue, mlStandardizeValue } from '../src/main/ml';

describe('ML', () => {

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

test('[]', () => {
expect(mlNormalize([])).toStrictEqual([]);
it('10, 5, 5', () => {
expect(mlNormalizeValue(10, 5, 5)).toBe(0);
});

test('[2]', () => {
expect(mlNormalize([2])).toStrictEqual([0]);
it('10, 5, 15', () => {
expect(mlNormalizeValue(10, 5, 15)).toBe(0.5);
});

test('[1, 2, 3]', () => {
expect(mlNormalize([1, 2, 3])).toStrictEqual([0, 0.5, 1]);
it('10, 5, 15, 2', () => {
expect(mlNormalizeValue(10, 5, 15, 2)).toBe(0.5);
});

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]);
it('10, -5, 15', () => {
expect(mlNormalizeValue(10, -5, 15)).toBe(0.75);
});
});

Expand All @@ -38,7 +34,7 @@ describe('ML', () => {
expect(mlStandardizeValue(10, 5, 2, 2)).toStrictEqual(2.5);
});

it('10, -5, -2n', () => {
it('10, -5, -2', () => {
expect(mlStandardizeValue(10, -5, -2)).toStrictEqual(-7.5);
});
});
Expand Down

0 comments on commit f36a8bd

Please sign in to comment.