@@ -21,7 +21,7 @@ module.exports = class Process {
21
21
dimensions : p [ 'Dimensions' ] ,
22
22
column : p [ 'Target variable' ] ,
23
23
transform : p [ 'Transform' ] ,
24
- method : p [ 'Method ' ] ,
24
+ method : p [ 'Projection method ' ] ,
25
25
steps : p [ 'Steps' ] ,
26
26
importance : p [ 'Feature importance' ]
27
27
}
@@ -50,6 +50,9 @@ module.exports = class Process {
50
50
const Xraw = new Matrix ( this . records . map ( row => features . map ( f => row [ f ] ) ) )
51
51
const featuresFiltered = [ ]
52
52
53
+ let imp
54
+ let impMatrix
55
+
53
56
// Remove columns with many NaNs
54
57
console . log ( '[Vis] Transforming data' )
55
58
const cols = [ ]
@@ -142,10 +145,11 @@ module.exports = class Process {
142
145
} )
143
146
Y = ae . encode ( X )
144
147
145
- var impMatrix = [ ]
148
+ impMatrix = [ ]
149
+
146
150
console . log ( '[Vis] Generate importance matrix with Autoencoder' )
147
151
featuresFiltered . forEach ( ( f , fi ) => {
148
- const imp = [ ]
152
+ const impTemp = [ ]
149
153
const Xr = [ ]
150
154
X . forEach ( x => Xr . push ( x . slice ( 0 ) ) )
151
155
for ( let i = Xr . length - 1 ; i > 0 ; i -- ) {
@@ -157,9 +161,9 @@ module.exports = class Process {
157
161
const Xp = ae . predict ( Xr )
158
162
featuresFiltered . forEach ( ( ff , ffi ) => {
159
163
const mse = Xp . reduce ( ( a , x , xi ) => Math . pow ( x [ ffi ] - X [ xi ] [ ffi ] , 2 ) + a , 0 ) / Xp . length
160
- imp . push ( mse )
164
+ impTemp . push ( mse )
161
165
} )
162
- impMatrix . push ( imp )
166
+ impMatrix . push ( impTemp )
163
167
} )
164
168
console . log ( '[Vis] Autoencoder importance matrix:' , impMatrix )
165
169
impMatrix = new Matrix ( impMatrix ) . scaleColumns ( ) . to2DArray ( )
@@ -177,7 +181,6 @@ module.exports = class Process {
177
181
let target
178
182
let colorscale
179
183
let g
180
- let imp
181
184
182
185
if ( params . column && params . column . length && ( params . column !== 'None' ) ) {
183
186
console . log ( '[Vis] Target variable is present' )
@@ -212,9 +215,29 @@ module.exports = class Process {
212
215
[ 0 , '#8A8DA1' ] ,
213
216
[ 1 , '#8A8DA1' ]
214
217
]
218
+
219
+ if ( params . importance === 'Random Forest' ) {
220
+ console . log ( '[Vis] Fitting random forest on all variables' )
221
+ console . log ( X , featuresFiltered , featuresFiltered . length )
222
+ impMatrix = featuresFiltered . map ( ( f , i ) => {
223
+ console . log ( `Calculating ${ i } of ${ featuresFiltered . length } : ${ f } ` )
224
+ const Xtemp = X . map ( row => row . filter ( ( _ , j ) => j !== i ) )
225
+ const gtemp = X . map ( row => row . filter ( ( _ , j ) => j === i ) )
226
+ const rf = new RandomForest ( {
227
+ nEstimators : 50 ,
228
+ maxDepth : 5 ,
229
+ maxFeatures : 'auto'
230
+ } )
231
+ rf . train ( Xtemp , gtemp )
232
+ const impTemp = rf . getFeatureImportances ( Xtemp , gtemp , { n : 3 , means : true , verbose : false } )
233
+ // Add importance of the target feature on itself
234
+ impTemp . splice ( i , 0 , 0 )
235
+ return impTemp
236
+ } )
237
+ }
215
238
}
216
239
217
- return { Y, X, params, nDims, featuresFiltered, recordsFiltered, target, g, colorscale, impMatrix, corr, imp}
240
+ return { Y, X, params, nDims, featuresFiltered, recordsFiltered, target, g, colorscale, impMatrix, corr, imp }
218
241
}
219
242
}
220
243
}
0 commit comments