Skip to content

Commit

Permalink
ml
Browse files Browse the repository at this point in the history
  • Loading branch information
mzusin committed Feb 20, 2024
1 parent 34a021c commit 3152eb5
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 15 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.

21 changes: 21 additions & 0 deletions src/main/ml.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { setDecimalPlaces } from './format';
import { getArithmeticMean } from './statistics';

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

return copy;
};

export const mlStandardize = (data: number[]): number[] => {

const mean = getArithmeticMean(data) ?? 0;

// Calculate the standard deviation ----------
// variance (spread or dispersion) is calculated by summing up the squared differences
// between each element and the mean,
// and then dividing by the number of elements.
const variance = data.reduce((acc, val) => acc + Math.pow(val - mean, 2), 0) / data.length;
// stdDev is the square root of the variance.
const stdDev = Math.sqrt(variance);

// Standardize each element in the data array
// Each element in the data array is standardized
// by subtracting the mean and dividing by the standard deviation.
const standardizedData = data.map(val => (val - mean) / stdDev);

return standardizedData;
};

0 comments on commit 3152eb5

Please sign in to comment.