7
7
getMeanByVector ,
8
8
getStdByVector ,
9
9
setVector ,
10
+ pushVector ,
10
11
} from './util' ;
11
12
12
13
csvToMatrix ( './src/data.csv' , init ) ;
@@ -19,6 +20,7 @@ function init(matrix) {
19
20
let X = getSubset ( matrix , ':, 1:2' ) ;
20
21
let y = getSubset ( matrix , ':, 3' ) ;
21
22
let m = getDimension ( y , 1 ) ;
23
+ let n = getDimension ( y , 2 ) ;
22
24
23
25
// Part 1: Feature Normalization
24
26
console . log ( 'Part 1: Feature Normalization ...\n' ) ;
@@ -31,6 +33,20 @@ function init(matrix) {
31
33
console . log ( '\n' ) ;
32
34
console . log ( 'Std: ' , sigma ) ;
33
35
console . log ( '\n' ) ;
36
+
37
+ // Part 2: Gradient Descent
38
+ console . log ( 'Part 2: Gradient Descent ...\n' ) ;
39
+
40
+ // Add Intercept Term
41
+ XNorm = pushVector ( XNorm , 0 , math . ones ( [ m , 1 ] ) . valueOf ( ) ) ;
42
+
43
+ const ALPHA = 0.01 ;
44
+ const ITERATIONS = 400 ;
45
+
46
+ let theta = math . zeros ( 3 , 1 ) . valueOf ( ) ;
47
+ theta = gradientDescentMulti ( XNorm , y , theta , ALPHA , ITERATIONS ) ;
48
+
49
+ console . log ( theta ) ;
34
50
}
35
51
36
52
function featureNormalize ( X ) {
@@ -51,3 +67,35 @@ function featureNormalize(X) {
51
67
52
68
return { XNorm : X , mu, sigma } ;
53
69
}
70
+
71
+ function gradientDescentMulti ( X , y , theta , ALPHA , ITERATIONS ) {
72
+ const m = getDimension ( y , 1 ) ;
73
+
74
+ for ( let i = 0 ; i < ITERATIONS ; i ++ ) {
75
+ // Octave:
76
+ // theta = theta - ALPHA / m * ((X * theta - y)' * X)';
77
+
78
+ theta = math . subtract (
79
+ theta ,
80
+ math . multiply (
81
+ ( ALPHA / m ) ,
82
+ math . transpose (
83
+ math . multiply (
84
+ math . transpose (
85
+ math . subtract (
86
+ math . multiply (
87
+ X ,
88
+ theta
89
+ ) ,
90
+ y
91
+ )
92
+ ) ,
93
+ X
94
+ )
95
+ )
96
+ )
97
+ ) ;
98
+ }
99
+
100
+ return theta ;
101
+ }
0 commit comments