|
1 | 1 | import math from 'mathjs';
|
2 | 2 | import csvToMatrix from 'csv-to-array-matrix';
|
3 | 3 |
|
4 |
| -csvToMatrix('./src/data.csv', init, ';'); |
| 4 | +import { |
| 5 | + getDimension, |
| 6 | + getSubset, |
| 7 | + getMeanByVector, |
| 8 | + getStdByVector, |
| 9 | + setVector, |
| 10 | +} from './util'; |
5 | 11 |
|
6 |
| -const getSubset = (matrix, selector) => |
7 |
| - math.eval(`matrix[${selector}]`, { matrix }); |
8 |
| - |
9 |
| -const setVector = (matrix, index, vector) => |
10 |
| - matrix.map((row, rowKey) => row.map((column, columnKey) => index === columnKey ? vector[rowKey][0] : column)); |
11 |
| - |
12 |
| -const getMeanByVector = (matrix) => { |
13 |
| - const n = getDimension(matrix, 2); |
14 |
| - const vectors = Array(n).fill().map((_, i) => getSubset(matrix, `:, ${i + 1}`)); |
15 |
| - return vectors.reduce((result, vector) => result.concat(math.mean(vector)), []); |
16 |
| -}; |
17 |
| - |
18 |
| -const getStdByVector = (matrix) => { |
19 |
| - const n = getDimension(matrix, 2); |
20 |
| - const vectors = Array(n).fill().map((_, i) => getSubset(matrix, `:, ${i + 1}`)); |
21 |
| - return vectors.reduce((result, vector) => result.concat(math.std(vector)), []); |
22 |
| -}; |
23 |
| - |
24 |
| -const getDimension = (matrix, dimension) => |
25 |
| - dimension === 1 |
26 |
| - ? matrix.length |
27 |
| - : matrix[0].length |
| 12 | +csvToMatrix('./src/data.csv', init); |
28 | 13 |
|
29 | 14 | function init(matrix) {
|
| 15 | + |
30 | 16 | // Part 0: Preparation
|
| 17 | + console.log('Part 0: Preparation ...\n'); |
31 | 18 |
|
32 | 19 | let X = getSubset(matrix, ':, 1:2');
|
33 | 20 | let y = getSubset(matrix, ':, 3');
|
34 | 21 | let m = getDimension(y, 1);
|
35 | 22 |
|
36 | 23 | // Part 1: Feature Normalization
|
| 24 | + console.log('Part 1: Feature Normalization ...\n'); |
37 | 25 |
|
38 | 26 | let { XNorm, mu, sigma } = featureNormalize(X);
|
| 27 | + |
| 28 | + console.log('XNorm: ', XNorm); |
| 29 | + console.log('\n'); |
| 30 | + console.log('Mean: ', mu); |
| 31 | + console.log('\n'); |
| 32 | + console.log('Std: ', sigma); |
| 33 | + console.log('\n'); |
39 | 34 | }
|
40 | 35 |
|
41 | 36 | function featureNormalize(X) {
|
|
0 commit comments