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 9a33e09 commit 3816cad
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 23 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.

30 changes: 24 additions & 6 deletions src/main/ml.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { setDecimalPlaces } from './format';
import { getArithmeticMean, getStandardDeviation } from './statistics';
import { IMlStandardizeResult } from '../types';

/**
* Returns a copy of array, where each value will be in the range [0, 1].
Expand All @@ -21,11 +22,28 @@ export const mlNormalize = (data: number[], decimalPlaces = Infinity): number[]
};

/**
* Returns a copy of array, where each value will be in the range [-1, 1]
* Changes value to be in the range [-1, 1].
*/
export const mlStandardize = (data: number[], decimalPlaces = Infinity): number[] => {
const copy = [...data];
const mean = getArithmeticMean(copy) ?? 0;
const stdDev = getStandardDeviation(copy, decimalPlaces);
return copy.map(val => (val - mean) / stdDev);
export const mlStandardizeValue = (value: number, mean: number, stdDev: number, decimalPlaces = Infinity) : number => {
if(stdDev === 0) return 0;
return setDecimalPlaces((value - mean) / stdDev, decimalPlaces);
};

/**
* Returns a copy of array, where each value will be in the range [-1, 1].
*/
export const mlStandardizeArray = (data: number[], mean: number, stdDev: number, decimalPlaces = Infinity) : number[] => {
return [...data].map(value => mlStandardizeValue(value, mean, stdDev, decimalPlaces));
};

export const mlStandardizeTestData = (data: number[], decimalPlaces = Infinity): IMlStandardizeResult => {
const mean = getArithmeticMean(data) ?? 0;
const stdDev = getStandardDeviation(data);
const _data = mlStandardizeArray(data, mean, stdDev, decimalPlaces);

return {
mean: setDecimalPlaces(mean, decimalPlaces),
stdDev: setDecimalPlaces(stdDev, decimalPlaces),
data: _data,
};
};
8 changes: 7 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ export interface ICircle {
r: number;
}

export type IPolygon = Vector2[];
export type IPolygon = Vector2[];

export interface IMlStandardizeResult {
mean: number;
stdDev: number;
data: number[];
}
43 changes: 42 additions & 1 deletion test/ml.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mlNormalize } from '../src/main/ml';
import { mlNormalize, mlStandardizeTestData } from '../src/main/ml';

describe('ML', () => {

Expand All @@ -24,4 +24,45 @@ describe('ML', () => {
expect(mlNormalize([1, 1, 2, 3, 4], 2)).toStrictEqual([0, 0, 0.33, 0.67, 1]);
});
});

describe('Standardize', () => {
it('returns an object with mean and standard deviation of 0 when data is empty', () => {
const result = mlStandardizeTestData([]);
expect(result.mean).toBe(0);
expect(result.stdDev).toBe(0);
expect(result.data).toStrictEqual([]);
});

it('returns an object with mean and standard deviation when data contains only one element', () => {
const result = mlStandardizeTestData([5]);
expect(result.mean).toBe(5);
expect(result.stdDev).toBe(0);
expect(result.data).toStrictEqual([0]);
});

it('returns the correct mean, standard deviation, and standardized data', () => {
const data = [2, 4, 4, 4, 5, 5, 7, 9];

// Expected mean and standard deviation (calculated manually)
const expectedMean = 5;
const expectedStdDev = 2;

// Expected standardized data (calculated manually)
const expectedData = [-1.5, -0.5, -0.5, -0.5, 0, 0, 1, 2];

// Call the function
const result = mlStandardizeTestData(data);

// Assert the mean, standard deviation, and standardized data
expect(result.mean).toBe(expectedMean);
expect(result.stdDev).toBe(expectedStdDev);
expect(result.data).toEqual(expectedData);
});

it('returns the mean and standard deviation with specified decimal places', () => {
const result = mlStandardizeTestData([1, 2, 3, 4, 5], 2);
expect(result.mean).toStrictEqual(3);
expect(result.stdDev).toStrictEqual(1.41);
});
});
});

0 comments on commit 3816cad

Please sign in to comment.