@@ -2,11 +2,9 @@ import math from 'mathjs';
2
2
import csvToMatrix from 'csv-to-array-matrix' ;
3
3
4
4
import {
5
- getDimension ,
6
- getSubset ,
7
- getMeanByVector ,
8
- getStdByVector ,
9
- setVector ,
5
+ getDimensionSize ,
6
+ getMeanAsRowVector ,
7
+ getStdAsRowVector ,
10
8
pushVector ,
11
9
} from './util' ;
12
10
@@ -17,10 +15,15 @@ function init(matrix) {
17
15
// Part 0: Preparation
18
16
console . log ( 'Part 0: Preparation ...\n' ) ;
19
17
20
- let X = getSubset ( matrix , ':, 1:2' ) ;
21
- let y = getSubset ( matrix , ':, 3' ) ;
22
- let m = getDimension ( y , 1 ) ;
23
- let n = getDimension ( y , 2 ) ;
18
+ let X = math . eval ( 'matrix[:, 1:2]' , {
19
+ matrix,
20
+ } ) ;
21
+ let y = math . eval ( 'matrix[:, 3]' , {
22
+ matrix,
23
+ } ) ;
24
+
25
+ let m = getDimensionSize ( y , 1 ) ;
26
+ let n = getDimensionSize ( y , 2 ) ;
24
27
25
28
// Part 1: Feature Normalization
26
29
console . log ( 'Part 1: Feature Normalization ...\n' ) ;
@@ -43,60 +46,56 @@ function init(matrix) {
43
46
const ALPHA = 0.01 ;
44
47
const ITERATIONS = 400 ;
45
48
46
- let theta = math . zeros ( 3 , 1 ) . valueOf ( ) ;
49
+ let theta = [ [ 0 ] , [ 0 ] , [ 0 ] ] ;
47
50
theta = gradientDescentMulti ( XNorm , y , theta , ALPHA , ITERATIONS ) ;
48
51
49
52
console . log ( 'theta: ' , theta ) ;
50
53
console . log ( '\n' ) ;
51
54
}
52
55
53
56
function featureNormalize ( X ) {
54
- const mu = getMeanByVector ( X ) ;
55
- const sigma = getStdByVector ( X ) ; // alternative: range
57
+ const mu = getMeanAsRowVector ( X ) ;
58
+ const sigma = getStdAsRowVector ( X ) ; // alternative: range
56
59
57
60
// n = features
58
- const n = getDimension ( X , 2 ) ;
61
+ const n = getDimensionSize ( X , 2 ) ;
59
62
for ( let i = 0 ; i < n ; i ++ ) {
60
63
61
- // take feature column by index i and subtract with mean with index i from mean vector
62
- let xMinusMean = math . subtract ( getSubset ( X , `:, ${ i + 1 } ` ) , mu [ i ] ) ;
63
- // put normalized feature column by dividing the meaned vector with the standard deviation
64
- let normalizedVector = math . divide ( xMinusMean , sigma [ i ] ) ;
64
+ let featureVector = math . eval ( `X[:, ${ i + 1 } ]` , {
65
+ X,
66
+ } ) ;
67
+
68
+ let featureMeanVector = math . eval ( 'featureVector - mu' , {
69
+ featureVector,
70
+ mu : mu [ i ]
71
+ } ) ;
72
+
73
+ let normalizedVector = math . eval ( 'featureMeanVector / sigma' , {
74
+ featureMeanVector,
75
+ sigma : sigma [ i ] ,
76
+ } ) ;
65
77
66
- X = setVector ( X , i , normalizedVector ) ;
78
+ math . eval ( `X[:, ${ i + 1 } ] = normalizedVector` , {
79
+ X,
80
+ normalizedVector,
81
+ } ) ;
67
82
}
68
83
69
84
return { XNorm : X , mu, sigma } ;
70
85
}
71
86
72
87
function gradientDescentMulti ( X , y , theta , ALPHA , ITERATIONS ) {
73
- const m = getDimension ( y , 1 ) ;
88
+ const m = getDimensionSize ( y , 1 ) ;
74
89
75
90
for ( let i = 0 ; i < ITERATIONS ; i ++ ) {
76
- // Octave:
77
- // theta = theta - ALPHA / m * ((X * theta - y)' * X)';
78
-
79
- theta = math . subtract (
91
+ theta = math . eval ( `theta - ALPHA / m * ((X * theta - y)' * X)'` , {
80
92
theta,
81
- math . multiply (
82
- ( ALPHA / m ) ,
83
- math . transpose (
84
- math . multiply (
85
- math . transpose (
86
- math . subtract (
87
- math . multiply (
88
- X ,
89
- theta
90
- ) ,
91
- y
92
- )
93
- ) ,
94
- X
95
- )
96
- )
97
- )
98
- ) ;
93
+ ALPHA ,
94
+ m,
95
+ X,
96
+ y,
97
+ } ) ;
99
98
}
100
99
101
100
return theta ;
102
- }
101
+ }
0 commit comments