-
Notifications
You must be signed in to change notification settings - Fork 12
/
matrixNumericalEncoding.ts
44 lines (41 loc) · 1.29 KB
/
matrixNumericalEncoding.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { matrixClone } from './matrixClone';
/**
* Numerically encodes the strings in the matrix and returns an encoding dictionary which can be used to encode other matrices
* @param matrixInitial - original matrix before encoding
* @returns - dictionary from string to number
*/
export function matrixNumericalEncoding(
matrixInitial: Array<Array<string | number>>,
): {
matrix: number[][];
dictCategoricalToNumerical: Record<string, number>;
} {
const matrix = matrixClone(matrixInitial);
const nRows = matrix.length;
const nColumns = matrix[0].length;
let k = 0;
for (let i = 0; i < nRows; i++) {
for (let j = 0; j < nColumns; j++) {
const value = matrix[i][j];
if (typeof value === 'number' && value > k) {
k = value;
}
}
}
const dictCategoricalToNumerical: Record<string, number> = {};
for (let i = 0; i < nRows; i++) {
for (let j = 0; j < nColumns; j++) {
const value = matrix[i][j];
if (typeof value === 'string') {
if (value in dictCategoricalToNumerical) {
matrix[i][j] = dictCategoricalToNumerical[value];
} else {
k++;
dictCategoricalToNumerical[value] = k;
matrix[i][j] = k;
}
}
}
}
return { matrix: matrix as number[][], dictCategoricalToNumerical };
}