Repository navigation
Expand file tree
/
Copy pathrandom-forest.dml
More file actions
1375 lines (1177 loc) · 59.2 KB
/
Copy pathrandom-forest.dml
File metadata and controls
1375 lines (1177 loc) · 59.2 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#-------------------------------------------------------------
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
#-------------------------------------------------------------
#
# THIS SCRIPT IMPLEMENTS CLASSIFICATION RANDOM FOREST WITH BOTH SCALE AND CATEGORICAL FEATURES
#
# INPUT PARAMETERS:
# ---------------------------------------------------------------------------------------------
# NAME TYPE DEFAULT MEANING
# ---------------------------------------------------------------------------------------------
# X String --- Location to read feature matrix X; note that X needs to be both recoded and dummy coded
# Y String --- Location to read label matrix Y; note that Y needs to be both recoded and dummy coded
# R String " " Location to read the matrix R which for each feature in X contains the following information
# - R[,1]: column ids
# - R[,2]: start indices
# - R[,3]: end indices
# If R is not provided by default all variables are assumed to be scale
# bins Int 20 Number of equiheight bins per scale feature to choose thresholds
# depth Int 25 Maximum depth of the learned tree
# num_leaf Int 10 Number of samples when splitting stops and a leaf node is added
# num_samples Int 3000 Number of samples at which point we switch to in-memory subtree building
# num_trees Int 10 Number of trees to be learned in the random forest model
# subsamp_rate Double 1.0 Parameter controlling the size of each tree in the forest; samples are selected from a
# Poisson distribution with parameter subsamp_rate (the default value is 1.0)
# feature_subset Double 0.5 Parameter that controls the number of feature used as candidates for splitting at each tree node
# as a power of number of features in the dataset;
# by default square root of features (i.e., feature_subset = 0.5) are used at each tree node
# impurity String "Gini" Impurity measure: entropy or Gini (the default)
# M String --- Location to write matrix M containing the learned tree
# C String " " Location to write matrix C containing the number of times samples are chosen in each tree of the random forest
# S_map String " " Location to write the mappings from scale feature ids to global feature ids
# C_map String " " Location to write the mappings from categorical feature ids to global feature ids
# fmt String "text" The output format of the model (matrix M), such as "text" or "csv"
# ---------------------------------------------------------------------------------------------
# OUTPUT:
# Matrix M where each column corresponds to a node in the learned tree and each row contains the following information:
# M[1,j]: id of node j (in a complete binary tree)
# M[2,j]: tree id to which node j belongs
# M[3,j]: Offset (no. of columns) to left child of j
# M[4,j]: Feature index of the feature that node j looks at if j is an internal node, otherwise 0
# M[5,j]: Type of the feature that node j looks at if j is an internal node: 1 for scale and 2 for categorical features,
# otherwise the label that leaf node j is supposed to predict
# M[6,j]: 1 if j is an internal node and the feature chosen for j is scale, otherwise the size of the subset of values
# stored in rows 7,8,... if j is categorical
# M[7:,j]: Only applicable for internal nodes. Threshold the example's feature value is compared to is stored at M[7,j] if the feature chosen for j is scale;
# If the feature chosen for j is categorical rows 7,8,... depict the value subset chosen for j
# -------------------------------------------------------------------------------------------
# HOW TO INVOKE THIS SCRIPT - EXAMPLE:
# hadoop jar SystemML.jar -f random-forest.dml -nvargs X=INPUT_DIR/X Y=INPUT_DIR/Y R=INPUT_DIR/R M=OUTPUT_DIR/model
# bins=20 depth=25 num_leaf=10 num_samples=3000 num_trees=10 impurity=Gini fmt=csv
# External function for binning
binning = externalFunction(Matrix[Double] A, Integer binsize, Integer numbins) return (Matrix[Double] B, Integer numbinsdef)
implemented in (classname="org.apache.sysml.udf.lib.BinningWrapper",exectype="mem")
# Default values of some parameters
fileR = ifdef ($R, " ");
fileC = ifdef ($C, " ");
fileS_map = ifdef ($S_map, " ");
fileC_map = ifdef ($C_map, " ");
fileM = $M;
num_bins = ifdef($bins, 20);
depth = ifdef($depth, 25);
num_leaf = ifdef($num_leaf, 10);
num_trees = ifdef($num_trees, 1);
threshold = ifdef ($num_samples, 3000);
imp = ifdef($impurity, "Gini");
rate = ifdef ($subsamp_rate, 1);
fpow = ifdef ($feature_subset, 0.5);
fmtO = ifdef($fmt, "text");
X = read($X);
Y_bin = read($Y);
num_records = nrow (X);
num_classes = ncol (Y_bin);
# check if there is only one class label
Y_bin_sum = sum (colSums (Y_bin) == num_records);
if (Y_bin_sum == 1) {
stop ("Y contains only one class label. No model will be learned!");
} else if (Y_bin_sum > 1) {
stop ("Y is not properly dummy coded. Multiple columns of Y contain only ones!")
}
# split data into X_scale and X_cat
if (fileR != " ") {
R = read (fileR);
R = order (target = R, by = 2); # sort by start indices
dummy_coded = (R[,2] != R[,3]);
R_scale = removeEmpty (target = R[,2:3] * (1 - dummy_coded), margin = "rows");
R_cat = removeEmpty (target = R[,2:3] * dummy_coded, margin = "rows");
if (fileS_map != " ") {
scale_feature_mapping = removeEmpty (target = (1 - dummy_coded) * seq (1, nrow (R)), margin = "rows");
write (scale_feature_mapping, fileS_map, format = fmtO);
}
if (fileC_map != " ") {
cat_feature_mapping = removeEmpty (target = dummy_coded * seq (1, nrow (R)), margin = "rows");
write (cat_feature_mapping, fileC_map, format = fmtO);
}
sum_dummy = sum (dummy_coded);
if (sum_dummy == nrow (R)) { # all features categorical
print ("All features categorical");
num_cat_features = nrow (R_cat);
num_scale_features = 0;
X_cat = X;
distinct_values = t (R_cat[,2] - R_cat[,1] + 1);
distinct_values_max = max (distinct_values);
distinct_values_offset = cumsum (t (distinct_values));
distinct_values_overall = sum (distinct_values);
} else if (sum_dummy == 0) { # all features scale
print ("All features scale");
num_scale_features = ncol (X);
num_cat_features = 0;
X_scale = X;
distinct_values_max = 1;
} else { # some features scale some features categorical
num_cat_features = nrow (R_cat);
num_scale_features = nrow (R_scale);
distinct_values = t (R_cat[,2] - R_cat[,1] + 1);
distinct_values_max = max (distinct_values);
distinct_values_offset = cumsum (t (distinct_values));
distinct_values_overall = sum (distinct_values);
W = matrix (1, rows = num_cat_features, cols = 1) %*% matrix ("1 -1", rows = 1, cols = 2);
W = matrix (W, rows = 2 * num_cat_features, cols = 1);
if (as.scalar (R_cat[num_cat_features, 2]) == ncol (X)) {
W[2 * num_cat_features,] = 0;
}
last = (R_cat[,2] != ncol (X));
R_cat1 = (R_cat[,2] + 1) * last;
R_cat[,2] = (R_cat[,2] * (1 - last)) + R_cat1;
R_cat_vec = matrix (R_cat, rows = 2 * num_cat_features, cols = 1);
col_tab = table (R_cat_vec, 1, W, ncol (X), 1);
col_ind = cumsum (col_tab);
col_ind_cat = removeEmpty (target = col_ind * seq (1, ncol (X)), margin = "rows");
col_ind_scale = removeEmpty (target = (1 - col_ind) * seq (1, ncol (X)), margin = "rows");
X_cat = X %*% table (col_ind_cat, seq (1, nrow (col_ind_cat)), ncol (X), nrow (col_ind_cat));
X_scale = X %*% table (col_ind_scale, seq (1, nrow (col_ind_scale)), ncol (X), nrow (col_ind_scale));
}
} else { # only scale features exist
print ("All features scale");
num_scale_features = ncol (X);
num_cat_features = 0;
X_scale = X;
distinct_values_max = 1;
}
if (num_scale_features > 0) {
print ("COMPUTING BINNING...");
bin_size = max (as.integer (num_records / num_bins), 1);
count_thresholds = matrix (0, rows = 1, cols = num_scale_features)
thresholds = matrix (0, rows = num_bins + 1, cols = num_scale_features)
parfor(i1 in 1:num_scale_features) {
col = order (target = X_scale[,i1], by = 1, decreasing = FALSE);
[col_bins, num_bins_defined] = binning (col, bin_size, num_bins);
count_thresholds[,i1] = num_bins_defined;
thresholds[,i1] = col_bins;
}
print ("PREPROCESSING SCALE FEATURE MATRIX...");
min_num_bins = min (count_thresholds);
max_num_bins = max (count_thresholds);
total_num_bins = sum (count_thresholds);
cum_count_thresholds = t (cumsum (t (count_thresholds)));
X_scale_ext = matrix (0, rows = num_records, cols = total_num_bins);
parfor (i2 in 1:num_scale_features, check = 0) {
Xi2 = X_scale[,i2];
count_threshold = as.scalar (count_thresholds[,i2]);
offset_feature = 1;
if (i2 > 1) {
offset_feature = offset_feature + as.integer (as.scalar (cum_count_thresholds[, (i2 - 1)]));
}
ti2 = t(thresholds[1:count_threshold, i2]);
X_scale_ext[,offset_feature:(offset_feature + count_threshold - 1)] = outer (Xi2, ti2, "<");
}
}
num_features_total = num_scale_features + num_cat_features;
num_feature_samples = as.integer (floor (num_features_total ^ fpow));
##### INITIALIZATION
L = matrix (1, rows = num_records, cols = num_trees); # last visited node id for each training sample
# create matrix of counts (generated by Poisson distribution) storing how many times each sample appears in each tree
print ("CONPUTING COUNTS...");
C = rand (rows = num_records, cols = num_trees, pdf = "poisson", lambda = rate);
Ix_nonzero = (C != 0);
L = L * Ix_nonzero;
total_counts = sum (C);
# model
# LARGE leaf nodes
# NC_large[,1]: node id
# NC_large[,2]: tree id
# NC_large[,3]: class label
# NC_large[,4]: no. of misclassified samples
# NC_large[,5]: 1 if special leaf (impure and 3 samples at that leaf > threshold) or 0 otherwise
NC_large = matrix (0, rows = 5, cols = 1);
# SMALL leaf nodes
# same schema as for LARGE leaf nodes (to be initialized)
NC_small = matrix (0, rows = 5, cols = 1);
# LARGE internal nodes
# Q_large[,1]: node id
# Q_large[,2]: tree id
Q_large = matrix (0, rows = 2, cols = num_trees);
Q_large[1,] = matrix (1, rows = 1, cols = num_trees);
Q_large[2,] = t (seq (1, num_trees));
# SMALL internal nodes
# same schema as for LARGE internal nodes (to be initialized)
Q_small = matrix (0, rows = 2, cols = 1);
# F_large[,1]: feature
# F_large[,2]: type
# F_large[,3]: offset
F_large = matrix (0, rows = 3, cols = 1);
# same schema as for LARGE nodes
F_small = matrix (0, rows = 3, cols = 1);
# split points for LARGE internal nodes
S_large = matrix (0, rows = 1, cols = 1);
# split points for SMALL internal nodes
S_small = matrix (0, rows = 1, cols = 1);
# initialize queue
cur_nodes_large = matrix (1, rows = 2, cols = num_trees);
cur_nodes_large[2,] = t (seq (1, num_trees));
num_cur_nodes_large = num_trees;
num_cur_nodes_small = 0;
level = 0;
while ((num_cur_nodes_large + num_cur_nodes_small) > 0 & level < depth) {
level = level + 1;
print (" --- start level " + level + " --- ");
##### PREPARE MODEL
if (num_cur_nodes_large > 0) { # LARGE nodes to process
cur_Q_large = matrix (0, rows = 2, cols = 2 * num_cur_nodes_large);
cur_NC_large = matrix (0, rows = 5, cols = 2 * num_cur_nodes_large);
cur_F_large = matrix (0, rows = 3, cols = num_cur_nodes_large);
cur_S_large = matrix (0, rows = 1, cols = num_cur_nodes_large * distinct_values_max);
cur_nodes_small = matrix (0, rows = 3, cols = 2 * num_cur_nodes_large);
}
##### LOOP OVER LARGE NODES...
parfor (i6 in 1:num_cur_nodes_large, check = 0) {
cur_node = as.scalar (cur_nodes_large[1,i6]);
cur_tree = as.scalar (cur_nodes_large[2,i6]);
# select sample features WOR
feature_samples = sample (num_features_total, num_feature_samples);
feature_samples = order (target = feature_samples, by = 1);
num_scale_feature_samples = sum (feature_samples <= num_scale_features);
num_cat_feature_samples = num_feature_samples - num_scale_feature_samples;
# --- find best split ---
# samples that reach cur_node
Ix = (L[,cur_tree] == cur_node);
cur_Y_bin = Y_bin * (Ix * C[,cur_tree]);
label_counts_overall = colSums (cur_Y_bin);
label_sum_overall = sum (label_counts_overall);
label_dist_overall = label_counts_overall / label_sum_overall;
if (imp == "entropy") {
label_dist_zero = (label_dist_overall == 0);
cur_impurity = - sum (label_dist_overall * log (label_dist_overall + label_dist_zero)); # / log (2); # impurity before
} else { # imp == "Gini"
cur_impurity = sum (label_dist_overall * (1 - label_dist_overall)); # impurity before
}
best_scale_gain = 0;
best_cat_gain = 0;
if (num_scale_features > 0 & num_scale_feature_samples > 0) {
scale_feature_samples = feature_samples[1:num_scale_feature_samples,];
# main operation
label_counts_left_scale = t (t (cur_Y_bin) %*% X_scale_ext);
# compute left and right label distribution
label_sum_left = rowSums (label_counts_left_scale);
label_dist_left = label_counts_left_scale / label_sum_left;
if (imp == "entropy") {
label_dist_left = replace (target = label_dist_left, pattern = 0, replacement = 1);
log_label_dist_left = log (label_dist_left); # / log (2)
impurity_left_scale = - rowSums (label_dist_left * log_label_dist_left);
} else { # imp == "Gini"
impurity_left_scale = rowSums (label_dist_left * (1 - label_dist_left));
}
#
label_counts_right_scale = - label_counts_left_scale + label_counts_overall;
label_sum_right = rowSums (label_counts_right_scale);
label_dist_right = label_counts_right_scale / label_sum_right;
if (imp == "entropy") {
label_dist_right = replace (target = label_dist_right, pattern = 0, replacement = 1);
log_label_dist_right = log (label_dist_right); # / log (2)
impurity_right_scale = - rowSums (label_dist_right * log_label_dist_right);
} else { # imp == "Gini"
impurity_right_scale = rowSums (label_dist_right * (1 - label_dist_right));
}
I_gain_scale = cur_impurity - ( ( label_sum_left / label_sum_overall ) * impurity_left_scale + ( label_sum_right / label_sum_overall ) * impurity_right_scale);
I_gain_scale = replace (target = I_gain_scale, pattern = "NaN", replacement = 0);
# determine best feature to split on and the split value
feature_start_ind = matrix (0, rows = 1, cols = num_scale_features);
feature_start_ind[1,1] = 1;
if (num_scale_features > 1) {
feature_start_ind[1,2:num_scale_features] = cum_count_thresholds[1,1:(num_scale_features - 1)] + 1;
}
max_I_gain_found = 0;
max_I_gain_found_ind = 0;
best_i = 0;
for (i in 1:num_scale_feature_samples) { # assuming feature_samples is 5x1
cur_feature_samples_bin = as.scalar (scale_feature_samples[i,]);
cur_start_ind = as.scalar (feature_start_ind[,cur_feature_samples_bin]);
cur_end_ind = as.scalar (cum_count_thresholds[,cur_feature_samples_bin]);
I_gain_portion = I_gain_scale[cur_start_ind:cur_end_ind,];
cur_max_I_gain = max (I_gain_portion);
cur_max_I_gain_ind = as.scalar (rowIndexMax (t (I_gain_portion)));
if (cur_max_I_gain > max_I_gain_found) {
max_I_gain_found = cur_max_I_gain;
max_I_gain_found_ind = cur_max_I_gain_ind;
best_i = i;
}
}
best_scale_gain = max_I_gain_found;
max_I_gain_ind_scale = max_I_gain_found_ind;
best_scale_feature = 0;
if (best_i > 0) {
best_scale_feature = as.scalar (scale_feature_samples[best_i,]);
}
best_scale_split = max_I_gain_ind_scale;
if (best_scale_feature > 1) {
best_scale_split = best_scale_split + as.scalar(cum_count_thresholds[,(best_scale_feature - 1)]);
}
}
if (num_cat_features > 0 & num_cat_feature_samples > 0){
cat_feature_samples = feature_samples[(num_scale_feature_samples + 1):(num_scale_feature_samples + num_cat_feature_samples),] - num_scale_features;
# initialization
split_values_bin = matrix (0, rows = 1, cols = distinct_values_overall);
split_values = split_values_bin;
split_values_offset = matrix (0, rows = 1, cols = num_cat_features);
I_gains = split_values_offset;
impurities_left = split_values_offset;
impurities_right = split_values_offset;
best_label_counts_left = matrix (0, rows = num_cat_features, cols = num_classes);
best_label_counts_right = matrix (0, rows = num_cat_features, cols = num_classes);
# main operation
label_counts = t (t (cur_Y_bin) %*% X_cat);
parfor (i9 in 1:num_cat_feature_samples, check = 0){
cur_cat_feature = as.scalar (cat_feature_samples[i9,1]);
start_ind = 1;
if (cur_cat_feature > 1) {
start_ind = start_ind + as.scalar (distinct_values_offset[(cur_cat_feature - 1),]);
}
offset = as.scalar (distinct_values[1,cur_cat_feature]);
cur_label_counts = label_counts[start_ind:(start_ind + offset - 1),];
label_sum = rowSums (cur_label_counts);
label_dist = cur_label_counts / label_sum;
if (imp == "entropy") {
label_dist = replace (target = label_dist, pattern = 0, replacement = 1);
log_label_dist = log (label_dist); # / log(2)
impurity = - rowSums (label_dist * log_label_dist);
impurity = replace (target = impurity, pattern = "NaN", replacement = 1/0);
} else { # imp == "Gini"
impurity = rowSums (label_dist * (1 - label_dist));
}
# sort cur feature by impurity
cur_distinct_values = seq (1, nrow (cur_label_counts));
cur_distinct_values_impurity = cbind (cur_distinct_values, impurity);
cur_feature_sorted = order (target = cur_distinct_values_impurity, by = 2, decreasing = FALSE);
P = table (cur_distinct_values, cur_feature_sorted); # permutation matrix
label_counts_sorted = P %*% cur_label_counts;
# compute left and right label distribution
label_counts_left = cumsum (label_counts_sorted);
label_sum_left = rowSums (label_counts_left);
label_dist_left = label_counts_left / label_sum_left;
label_dist_left = replace (target = label_dist_left, pattern = "NaN", replacement = 1);
if (imp == "entropy") {
label_dist_left = replace (target = label_dist_left, pattern = 0, replacement = 1);
log_label_dist_left = log (label_dist_left); # / log(2)
impurity_left = - rowSums (label_dist_left * log_label_dist_left);
} else { # imp == "Gini"
impurity_left = rowSums (label_dist_left * (1 - label_dist_left));
}
#
label_counts_right = - label_counts_left + label_counts_overall;
label_sum_right = rowSums (label_counts_right);
label_dist_right = label_counts_right / label_sum_right;
label_dist_right = replace (target = label_dist_right, pattern = "NaN", replacement = 1);
if (imp == "entropy") {
label_dist_right = replace (target = label_dist_right, pattern = 0, replacement = 1);
log_label_dist_right = log (label_dist_right); # / log (2)
impurity_right = - rowSums (label_dist_right * log_label_dist_right);
} else { # imp == "Gini"
impurity_right = rowSums (label_dist_right * (1 - label_dist_right));
}
I_gain = cur_impurity - ( ( label_sum_left / label_sum_overall ) * impurity_left + ( label_sum_right / label_sum_overall ) * impurity_right);
Ix_label_sum_left_zero = (label_sum_left == 0);
Ix_label_sum_right_zero = (label_sum_right == 0);
Ix_label_sum_zero = Ix_label_sum_left_zero * Ix_label_sum_right_zero;
I_gain = I_gain * (1 - Ix_label_sum_zero);
I_gain[nrow (I_gain),] = 0; # last entry invalid
max_I_gain_ind = as.scalar (rowIndexMax (t (I_gain)));
split_values[1, start_ind:(start_ind + max_I_gain_ind - 1)] = t (cur_feature_sorted[1:max_I_gain_ind,1]);
for (i10 in 1:max_I_gain_ind) {
ind = as.scalar (cur_feature_sorted[i10,1]);
if (ind == 1) {
split_values_bin[1,start_ind] = 1.0;
} else {
split_values_bin[1,(start_ind + ind - 1)] = 1.0;
}
}
split_values_offset[1,cur_cat_feature] = max_I_gain_ind;
I_gains[1,cur_cat_feature] = max (I_gain);
impurities_left[1,cur_cat_feature] = as.scalar (impurity_left[max_I_gain_ind,]);
impurities_right[1,cur_cat_feature] = as.scalar (impurity_right[max_I_gain_ind,]);
best_label_counts_left[cur_cat_feature,] = label_counts_left[max_I_gain_ind,];
best_label_counts_right[cur_cat_feature,] = label_counts_right[max_I_gain_ind,];
}
# determine best feature to split on and the split values
best_cat_feature = as.scalar (rowIndexMax (I_gains));
best_cat_gain = max (I_gains);
start_ind = 1;
if (best_cat_feature > 1) {
start_ind = start_ind + as.scalar (distinct_values_offset[(best_cat_feature - 1),]);
}
offset = as.scalar (distinct_values[1,best_cat_feature]);
best_split_values_bin = split_values_bin[1, start_ind:(start_ind + offset - 1)];
}
# compare best scale feature to best cat. feature and pick the best one
if (num_scale_features > 0 & num_scale_feature_samples > 0 & best_scale_gain >= best_cat_gain & best_scale_gain > 0) {
# --- update model ---
cur_F_large[1,i6] = best_scale_feature;
cur_F_large[2,i6] = 1;
cur_F_large[3,i6] = 1;
cur_S_large[1,(i6 - 1) * distinct_values_max + 1] = thresholds[max_I_gain_ind_scale, best_scale_feature];
left_child = 2 * (cur_node - 1) + 1 + 1;
right_child = 2 * (cur_node - 1) + 2 + 1;
# samples going to the left subtree
Ix_left = X_scale_ext[,best_scale_split];
Ix_left = Ix * Ix_left;
Ix_right = Ix * (1 - Ix_left);
L[,cur_tree] = L[,cur_tree] * (1 - Ix_left) + (Ix_left * left_child);
L[,cur_tree] = L[,cur_tree] * (1 - Ix_right) + (Ix_right * right_child);
left_child_size = sum (Ix_left * C[,cur_tree]);
right_child_size = sum (Ix_right * C[,cur_tree]);
# check if left or right child is a leaf
left_pure = FALSE;
right_pure = FALSE;
cur_impurity_left = as.scalar(impurity_left_scale[best_scale_split,]); # max_I_gain_ind_scale
cur_impurity_right = as.scalar(impurity_right_scale[best_scale_split,]); # max_I_gain_ind_scale
if ( (left_child_size <= num_leaf | cur_impurity_left == 0 | (level == depth)) &
(right_child_size <= num_leaf | cur_impurity_right == 0 | (level == depth)) |
(left_child_size <= threshold & right_child_size <= threshold & (level == depth)) ) { # both left and right nodes are leaf
cur_label_counts_left = label_counts_left_scale[best_scale_split,]; # max_I_gain_ind_scale
cur_NC_large[1,(2 * (i6 - 1) + 1)] = left_child;
cur_NC_large[2,(2 * (i6 - 1) + 1)] = cur_tree;
cur_NC_large[3,(2 * (i6 - 1) + 1)] = as.scalar( rowIndexMax (cur_label_counts_left)); # leaf class label
left_pure = TRUE;
# compute number of misclassified points
cur_NC_large[4,(2 * (i6 - 1) + 1)] = left_child_size - max (cur_label_counts_left);
cur_label_counts_right = label_counts_overall - cur_label_counts_left;
cur_NC_large[1,(2 * i6)] = right_child;
cur_NC_large[2,(2 * i6)] = cur_tree;
cur_NC_large[3,(2 * i6)] = as.scalar( rowIndexMax (cur_label_counts_right)); # leaf class label
right_pure = TRUE;
# compute number of misclassified pints
cur_NC_large[4,(2 * i6)] = right_child_size - max (cur_label_counts_right);
} else if (left_child_size <= num_leaf | cur_impurity_left == 0 | (level == depth) |
(left_child_size <= threshold & (level == depth))) {
cur_label_counts_left = label_counts_left_scale[best_scale_split,]; # max_I_gain_ind_scale
cur_NC_large[1,(2 * (i6 - 1) + 1)] = left_child;
cur_NC_large[2,(2 * (i6 - 1) + 1)] = cur_tree;
cur_NC_large[3,(2 * (i6 - 1) + 1)] = as.scalar( rowIndexMax (cur_label_counts_left)); # leaf class label
left_pure = TRUE;
# compute number of misclassified points
cur_NC_large[4,(2 * (i6 - 1) + 1)] = left_child_size - max (cur_label_counts_left);
} else if (right_child_size <= num_leaf | cur_impurity_right == 0 | (level == depth) |
(right_child_size <= threshold & (level == depth))) {
cur_label_counts_right = label_counts_right_scale[best_scale_split,]; # max_I_gain_ind_scale
cur_NC_large[1,(2 * i6)] = right_child;
cur_NC_large[2,(2 * i6)] = cur_tree;
cur_NC_large[3,(2 * i6)] = as.scalar( rowIndexMax (cur_label_counts_right)); # leaf class label
right_pure = TRUE;
# compute number of misclassified pints
cur_NC_large[4,(2 * i6)] = right_child_size - max (cur_label_counts_right);
}
} else if (num_cat_features > 0 & num_cat_feature_samples > 0 & best_cat_gain > 0) {
# --- update model ---
cur_F_large[1,i6] = best_cat_feature;
cur_F_large[2,i6] = 2;
offset_nonzero = as.scalar (split_values_offset[1,best_cat_feature]);
S_start_ind = (i6 - 1) * distinct_values_max + 1;
cur_F_large[3,i6] = offset_nonzero;
cur_S_large[1,S_start_ind:(S_start_ind + offset_nonzero - 1)] = split_values[1,start_ind:(start_ind + offset_nonzero - 1)];
left_child = 2 * (cur_node - 1) + 1 + 1;
right_child = 2 * (cur_node - 1) + 2 + 1;
# samples going to the left subtree
Ix_left = rowSums (X_cat[,start_ind:(start_ind + offset - 1)] * best_split_values_bin);
Ix_left = (Ix_left >= 1);
Ix_left = Ix * Ix_left;
Ix_right = Ix * (1 - Ix_left);
L[,cur_tree] = L[,cur_tree] * (1 - Ix_left) + (Ix_left * left_child);
L[,cur_tree] = L[,cur_tree] * (1 - Ix_right) + (Ix_right * right_child);
left_child_size = sum (Ix_left * C[,cur_tree]);
right_child_size = sum (Ix_right * C[,cur_tree]);
# check if left or right child is a leaf
left_pure = FALSE;
right_pure = FALSE;
cur_impurity_left = as.scalar(impurities_left[,best_cat_feature]);
cur_impurity_right = as.scalar(impurities_right[,best_cat_feature]);
if ( (left_child_size <= num_leaf | cur_impurity_left == 0 | (level == depth)) &
(right_child_size <= num_leaf | cur_impurity_right == 0 | (level == depth)) |
(left_child_size <= threshold & right_child_size <= threshold & (level == depth)) ) { # both left and right nodes are leaf
cur_label_counts_left = best_label_counts_left[best_cat_feature,];
cur_NC_large[1,(2 * (i6 - 1) + 1)] = left_child;
cur_NC_large[2,(2 * (i6 - 1) + 1)] = cur_tree;
cur_NC_large[3,(2 * (i6 - 1) + 1)] = as.scalar( rowIndexMax (cur_label_counts_left)); # leaf class label
left_pure = TRUE;
# compute number of misclassified points
cur_NC_large[4,(2 * (i6 - 1) + 1)] = left_child_size - max (cur_label_counts_left);
cur_label_counts_right = label_counts_overall - cur_label_counts_left;
cur_NC_large[1,(2 * i6)] = right_child;
cur_NC_large[2,(2 * i6)] = cur_tree;
cur_NC_large[3,(2 * i6)] = as.scalar( rowIndexMax (cur_label_counts_right)); # leaf class label
right_pure = TRUE;
# compute number of misclassified pints
cur_NC_large[4,(2 * i6)] = right_child_size - max (cur_label_counts_right);
} else if (left_child_size <= num_leaf | cur_impurity_left == 0 | (level == depth) |
(left_child_size <= threshold & (level == depth))) {
cur_label_counts_left = best_label_counts_left[best_cat_feature,];
cur_NC_large[1,(2 * (i6 - 1) + 1)] = left_child;
cur_NC_large[2,(2 * (i6 - 1) + 1)] = cur_tree;
cur_NC_large[3,(2 * (i6 - 1) + 1)] = as.scalar( rowIndexMax (cur_label_counts_left)); # leaf class label
left_pure = TRUE;
# compute number of misclassified points
cur_NC_large[4,(2 * (i6 - 1) + 1)] = left_child_size - max (cur_label_counts_left);
} else if (right_child_size <= num_leaf | cur_impurity_right == 0 | (level == depth) |
(right_child_size <= threshold & (level == depth))) {
cur_label_counts_right = best_label_counts_right[best_cat_feature,];
cur_NC_large[1,(2 * i6)] = right_child;
cur_NC_large[2,(2 * i6)] = cur_tree;
cur_NC_large[3,(2 * i6)] = as.scalar( rowIndexMax (cur_label_counts_right)); # leaf class label
right_pure = TRUE;
# compute number of misclassified pints
cur_NC_large[4,(2 * i6)] = right_child_size - max (cur_label_counts_right);
}
} else {
print ("NUMBER OF SAMPLES AT NODE " + cur_node + " in tree " + cur_tree + " CANNOT BE REDUCED TO MATCH " + num_leaf + ". THIS NODE IS DECLARED AS LEAF!");
right_pure = TRUE;
left_pure = TRUE;
cur_NC_large[1,(2 * (i6 - 1) + 1)] = cur_node;
cur_NC_large[2,(2 * (i6 - 1) + 1)] = cur_tree;
class_label = as.scalar (rowIndexMax (label_counts_overall));
cur_NC_large[3,(2 * (i6 - 1) + 1)] = class_label;
cur_NC_large[4,(2 * (i6 - 1) + 1)] = label_sum_overall - max (label_counts_overall);
cur_NC_large[5,(2 * (i6 - 1) + 1)] = 1; # special leaf
}
# add nodes to Q
if (!left_pure) {
if (left_child_size > threshold) {
cur_Q_large[1,(2 * (i6 - 1)+ 1)] = left_child;
cur_Q_large[2,(2 * (i6 - 1)+ 1)] = cur_tree;
} else {
cur_nodes_small[1,(2 * (i6 - 1)+ 1)] = left_child;
cur_nodes_small[2,(2 * (i6 - 1)+ 1)] = left_child_size;
cur_nodes_small[3,(2 * (i6 - 1)+ 1)] = cur_tree;
}
}
if (!right_pure) {
if (right_child_size > threshold) {
cur_Q_large[1,(2 * i6)] = right_child;
cur_Q_large[2,(2 * i6)] = cur_tree;
} else{
cur_nodes_small[1,(2 * i6)] = right_child;
cur_nodes_small[2,(2 * i6)] = right_child_size;
cur_nodes_small[3,(2 * i6)] = cur_tree;
}
}
}
##### PREPARE MODEL FOR LARGE NODES
if (num_cur_nodes_large > 0) {
cur_Q_large = removeEmpty (target = cur_Q_large, margin = "cols");
if (as.scalar (cur_Q_large[1,1]) != 0) Q_large = cbind (Q_large, cur_Q_large);
cur_NC_large = removeEmpty (target = cur_NC_large, margin = "cols");
if (as.scalar (cur_NC_large[1,1]) != 0) NC_large = cbind (NC_large, cur_NC_large);
cur_F_large = removeEmpty (target = cur_F_large, margin = "cols");
if (as.scalar (cur_F_large[1,1]) != 0) F_large = cbind (F_large, cur_F_large);
cur_S_large = removeEmpty (target = cur_S_large, margin = "cols");
if (as.scalar (cur_S_large[1,1]) != 0) S_large = cbind (S_large, cur_S_large);
num_cur_nodes_large_pre = 2 * num_cur_nodes_large;
if (as.scalar (cur_Q_large[1,1]) == 0) {
num_cur_nodes_large = 0;
} else {
cur_nodes_large = cur_Q_large;
num_cur_nodes_large = ncol (cur_Q_large);
}
}
##### PREPARE MODEL FOR SMALL NODES
cur_nodes_small_nonzero = removeEmpty (target = cur_nodes_small, margin = "cols");
if (as.scalar (cur_nodes_small_nonzero[1,1]) != 0) { # if SMALL nodes exist
num_cur_nodes_small = ncol (cur_nodes_small_nonzero);
}
if (num_cur_nodes_small > 0) { # SMALL nodes to process
reserve_len = sum (2 ^ (ceil (log (cur_nodes_small_nonzero[2,]) / log (2)))) + num_cur_nodes_small;
cur_Q_small = matrix (0, rows = 2, cols = reserve_len);
cur_F_small = matrix (0, rows = 3, cols = reserve_len);
cur_NC_small = matrix (0, rows = 5, cols = reserve_len);
cur_S_small = matrix (0, rows = 1, cols = reserve_len * distinct_values_max); # split values of the best feature
}
##### LOOP OVER SMALL NODES...
parfor (i7 in 1:num_cur_nodes_small, check = 0) {
cur_node_small = as.scalar (cur_nodes_small_nonzero[1,i7]);
cur_tree_small = as.scalar (cur_nodes_small_nonzero[3,i7]);
# build dataset for SMALL node
Ix = (L[,cur_tree_small] == cur_node_small);
if (num_scale_features > 0) {
X_scale_ext_small = removeEmpty (target = X_scale_ext, margin = "rows", select = Ix);
}
if (num_cat_features > 0) {
X_cat_small = removeEmpty (target = X_cat, margin = "rows", select = Ix);
}
L_small = removeEmpty (target = L * Ix, margin = "rows");
C_small = removeEmpty (target = C * Ix, margin = "rows");
Y_bin_small = removeEmpty (target = Y_bin * Ix, margin = "rows");
# compute offset
offsets = cumsum (t (2 ^ ceil (log (cur_nodes_small_nonzero[2,]) / log (2))));
start_ind_global = 1;
if (i7 > 1) {
start_ind_global = start_ind_global + as.scalar (offsets[(i7 - 1),]);
}
start_ind_S_global = 1;
if (i7 > 1) {
start_ind_S_global = start_ind_S_global + (as.scalar (offsets[(i7 - 1),]) * distinct_values_max);
}
Q = matrix (0, rows = 2, cols = 1);
Q[1,1] = cur_node_small;
Q[2,1] = cur_tree_small;
F = matrix (0, rows = 3, cols = 1);
NC = matrix (0, rows = 5, cols = 1);
S = matrix (0, rows = 1, cols = 1);
cur_nodes_ = matrix (cur_node_small, rows = 2, cols = 1);
cur_nodes_[1,1] = cur_node_small;
cur_nodes_[2,1] = cur_tree_small;
num_cur_nodes = 1;
level_ = level;
while (num_cur_nodes > 0 & level_ < depth) {
level_ = level_ + 1;
cur_Q = matrix (0, rows = 2, cols = 2 * num_cur_nodes);
cur_F = matrix (0, rows = 3, cols = num_cur_nodes);
cur_NC = matrix (0, rows = 5, cols = 2 * num_cur_nodes);
cur_S = matrix (0, rows = 1, cols = num_cur_nodes * distinct_values_max);
parfor (i8 in 1:num_cur_nodes, check = 0) {
cur_node = as.scalar (cur_nodes_[1,i8]);
cur_tree = as.scalar (cur_nodes_[2,i8]);
# select sample features WOR
feature_samples = sample (num_features_total, num_feature_samples);
feature_samples = order (target = feature_samples, by = 1);
num_scale_feature_samples = sum (feature_samples <= num_scale_features);
num_cat_feature_samples = num_feature_samples - num_scale_feature_samples;
# --- find best split ---
# samples that reach cur_node
Ix = (L_small[,cur_tree] == cur_node);
cur_Y_bin = Y_bin_small * (Ix * C_small[,cur_tree]);
label_counts_overall = colSums (cur_Y_bin);
label_sum_overall = sum (label_counts_overall);
label_dist_overall = label_counts_overall / label_sum_overall;
if (imp == "entropy") {
label_dist_zero = (label_dist_overall == 0);
cur_impurity = - sum (label_dist_overall * log (label_dist_overall + label_dist_zero)); # / log (2);
} else { # imp == "Gini"
cur_impurity = sum (label_dist_overall * (1 - label_dist_overall));
}
best_scale_gain = 0;
best_cat_gain = 0;
if (num_scale_features > 0 & num_scale_feature_samples > 0) {
scale_feature_samples = feature_samples[1:num_scale_feature_samples,];
# main operation
label_counts_left_scale = t (t (cur_Y_bin) %*% X_scale_ext_small);
# compute left and right label distribution
label_sum_left = rowSums (label_counts_left_scale);
label_dist_left = label_counts_left_scale / label_sum_left;
if (imp == "entropy") {
label_dist_left = replace (target = label_dist_left, pattern = 0, replacement = 1);
log_label_dist_left = log (label_dist_left); # / log (2)
impurity_left_scale = - rowSums (label_dist_left * log_label_dist_left);
} else { # imp == "Gini"
impurity_left_scale = rowSums (label_dist_left * (1 - label_dist_left));
}
#
label_counts_right_scale = - label_counts_left_scale + label_counts_overall;
label_sum_right = rowSums (label_counts_right_scale);
label_dist_right = label_counts_right_scale / label_sum_right;
if (imp == "entropy") {
label_dist_right = replace (target = label_dist_right, pattern = 0, replacement = 1);
log_label_dist_right = log (label_dist_right); # log (2)
impurity_right_scale = - rowSums (label_dist_right * log_label_dist_right);
} else { # imp == "Gini"
impurity_right_scale = rowSums (label_dist_right * (1 - label_dist_right));
}
I_gain_scale = cur_impurity - ( ( label_sum_left / label_sum_overall ) * impurity_left_scale + ( label_sum_right / label_sum_overall ) * impurity_right_scale);
I_gain_scale = replace (target = I_gain_scale, pattern = "NaN", replacement = 0);
# determine best feature to split on and the split value
feature_start_ind = matrix (0, rows = 1, cols = num_scale_features);
feature_start_ind[1,1] = 1;
if (num_scale_features > 1) {
feature_start_ind[1,2:num_scale_features] = cum_count_thresholds[1,1:(num_scale_features - 1)] + 1;
}
max_I_gain_found = 0;
max_I_gain_found_ind = 0;
best_i = 0;
for (i in 1:num_scale_feature_samples) { # assuming feature_samples is 5x1
cur_feature_samples_bin = as.scalar (scale_feature_samples[i,]);
cur_start_ind = as.scalar (feature_start_ind[,cur_feature_samples_bin]);
cur_end_ind = as.scalar (cum_count_thresholds[,cur_feature_samples_bin]);
I_gain_portion = I_gain_scale[cur_start_ind:cur_end_ind,];
cur_max_I_gain = max (I_gain_portion);
cur_max_I_gain_ind = as.scalar (rowIndexMax (t (I_gain_portion)));
if (cur_max_I_gain > max_I_gain_found) {
max_I_gain_found = cur_max_I_gain;
max_I_gain_found_ind = cur_max_I_gain_ind;
best_i = i;
}
}
best_scale_gain = max_I_gain_found;
max_I_gain_ind_scale = max_I_gain_found_ind;
best_scale_feature = 0;
if (best_i > 0) {
best_scale_feature = as.scalar (scale_feature_samples[best_i,]);
}
best_scale_split = max_I_gain_ind_scale;
if (best_scale_feature > 1) {
best_scale_split = best_scale_split + as.scalar(cum_count_thresholds[,(best_scale_feature - 1)]);
}
}
if (num_cat_features > 0 & num_cat_feature_samples > 0){
cat_feature_samples = feature_samples[(num_scale_feature_samples + 1):(num_scale_feature_samples + num_cat_feature_samples),] - num_scale_features;
# initialization
split_values_bin = matrix (0, rows = 1, cols = distinct_values_overall);
split_values = split_values_bin;
split_values_offset = matrix (0, rows = 1, cols = num_cat_features);
I_gains = split_values_offset;
impurities_left = split_values_offset;
impurities_right = split_values_offset;
best_label_counts_left = matrix (0, rows = num_cat_features, cols = num_classes);
best_label_counts_right = matrix (0, rows = num_cat_features, cols = num_classes);
# main operation
label_counts = t (t (cur_Y_bin) %*% X_cat_small);
parfor (i9 in 1:num_cat_feature_samples, check = 0){
cur_cat_feature = as.scalar (cat_feature_samples[i9,1]);
start_ind = 1;
if (cur_cat_feature > 1) {
start_ind = start_ind + as.scalar (distinct_values_offset[(cur_cat_feature - 1),]);
}
offset = as.scalar (distinct_values[1,cur_cat_feature]);
cur_label_counts = label_counts[start_ind:(start_ind + offset - 1),];
label_sum = rowSums (cur_label_counts);
label_dist = cur_label_counts / label_sum;
if (imp == "entropy") {
label_dist = replace (target = label_dist, pattern = 0, replacement = 1);
log_label_dist = log (label_dist); # / log(2)
impurity = - rowSums (label_dist * log_label_dist);
impurity = replace (target = impurity, pattern = "NaN", replacement = 1/0);
} else { # imp == "Gini"
impurity = rowSums (label_dist * (1 - label_dist));
}
# sort cur feature by impurity
cur_distinct_values = seq (1, nrow (cur_label_counts));
cur_distinct_values_impurity = cbind (cur_distinct_values, impurity);
cur_feature_sorted = order (target = cur_distinct_values_impurity, by = 2, decreasing = FALSE);
P = table (cur_distinct_values, cur_feature_sorted); # permutation matrix
label_counts_sorted = P %*% cur_label_counts;
# compute left and right label distribution
label_counts_left = cumsum (label_counts_sorted);
label_sum_left = rowSums (label_counts_left);
label_dist_left = label_counts_left / label_sum_left;
label_dist_left = replace (target = label_dist_left, pattern = "NaN", replacement = 1);
if (imp == "entropy") {
label_dist_left = replace (target = label_dist_left, pattern = 0, replacement = 1);
log_label_dist_left = log (label_dist_left); # / log(2)
impurity_left = - rowSums (label_dist_left * log_label_dist_left);
} else { # imp == "Gini"
impurity_left = rowSums (label_dist_left * (1 - label_dist_left));
}
#
label_counts_right = - label_counts_left + label_counts_overall;
label_sum_right = rowSums (label_counts_right);
label_dist_right = label_counts_right / label_sum_right;
label_dist_right = replace (target = label_dist_right, pattern = "NaN", replacement = 1);
if (imp == "entropy") {
label_dist_right = replace (target = label_dist_right, pattern = 0, replacement = 1);
log_label_dist_right = log (label_dist_right); # / log (2)
impurity_right = - rowSums (label_dist_right * log_label_dist_right);
} else { # imp == "Gini"
impurity_right = rowSums (label_dist_right * (1 - label_dist_right));
}
I_gain = cur_impurity - ( ( label_sum_left / label_sum_overall ) * impurity_left + ( label_sum_right / label_sum_overall ) * impurity_right);
Ix_label_sum_left_zero = (label_sum_left == 0);
Ix_label_sum_right_zero = (label_sum_right == 0);
Ix_label_sum_zero = Ix_label_sum_left_zero * Ix_label_sum_right_zero;
I_gain = I_gain * (1 - Ix_label_sum_zero);
I_gain[nrow (I_gain),] = 0; # last entry invalid
max_I_gain_ind = as.scalar (rowIndexMax (t (I_gain)));
split_values[1, start_ind:(start_ind + max_I_gain_ind - 1)] = t (cur_feature_sorted[1:max_I_gain_ind,1]);
for (i10 in 1:max_I_gain_ind) {
ind = as.scalar (cur_feature_sorted[i10,1]);
if (ind == 1) {
split_values_bin[1,start_ind] = 1.0;
} else {
split_values_bin[1,(start_ind + ind - 1)] = 1.0;
}
}
split_values_offset[1,cur_cat_feature] = max_I_gain_ind;
I_gains[1,cur_cat_feature] = max (I_gain);
impurities_left[1,cur_cat_feature] = as.scalar (impurity_left[max_I_gain_ind,]);
impurities_right[1,cur_cat_feature] = as.scalar (impurity_right[max_I_gain_ind,]);
best_label_counts_left[cur_cat_feature,] = label_counts_left[max_I_gain_ind,];
best_label_counts_right[cur_cat_feature,] = label_counts_right[max_I_gain_ind,];
}
# determine best feature to split on and the split values
best_cat_feature = as.scalar (rowIndexMax (I_gains));
best_cat_gain = max (I_gains);
start_ind = 1;
if (best_cat_feature > 1) {
start_ind = start_ind + as.scalar (distinct_values_offset[(best_cat_feature - 1),]);
}
offset = as.scalar (distinct_values[1,best_cat_feature]);
best_split_values_bin = split_values_bin[1, start_ind:(start_ind + offset - 1)];
}
# compare best scale feature to best cat. feature and pick the best one
if (num_scale_features > 0 & num_scale_feature_samples > 0 & best_scale_gain >= best_cat_gain & best_scale_gain > 0) {
# --- update model ---
cur_F[1,i8] = best_scale_feature;
cur_F[2,i8] = 1;
cur_F[3,i8] = 1;
cur_S[1,(i8 - 1) * distinct_values_max + 1] = thresholds[max_I_gain_ind_scale, best_scale_feature];
left_child = 2 * (cur_node - 1) + 1 + 1;
right_child = 2 * (cur_node - 1) + 2 + 1;
# samples going to the left subtree
Ix_left = X_scale_ext_small[, best_scale_split];
Ix_left = Ix * Ix_left;
Ix_right = Ix * (1 - Ix_left);
L_small[,cur_tree] = L_small[,cur_tree] * (1 - Ix_left) + (Ix_left * left_child);
L_small[,cur_tree] = L_small[,cur_tree] * (1 - Ix_right) + (Ix_right * right_child);
left_child_size = sum (Ix_left * C_small[,cur_tree]);
right_child_size = sum (Ix_right * C_small[,cur_tree]);
# check if left or right child is a leaf
left_pure = FALSE;
right_pure = FALSE;
cur_impurity_left = as.scalar(impurity_left_scale[best_scale_split,]);
cur_impurity_right = as.scalar(impurity_right_scale[best_scale_split,]);
if ( (left_child_size <= num_leaf | cur_impurity_left == 0 | level_ == depth) &
(right_child_size <= num_leaf | cur_impurity_right == 0 | level_ == depth) ) { # both left and right nodes are leaf
cur_label_counts_left = label_counts_left_scale[best_scale_split,];
cur_NC[1,(2 * (i8 - 1) + 1)] = left_child;
cur_NC[2,(2 * (i8 - 1) + 1)] = cur_tree;
cur_NC[3,(2 * (i8 - 1) + 1)] = as.scalar( rowIndexMax (cur_label_counts_left)); # leaf class label