-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
73 additions
and
22 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
}); | ||
}); | ||
}); |