-
Notifications
You must be signed in to change notification settings - Fork 3
/
forest.py
1435 lines (1245 loc) · 47.4 KB
/
forest.py
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
# Authors: Stephane Gaiffas <stephane.gaiffas@gmail.com>
# License: BSD 3 clause
import os
import numpy as np
from numba import jitclass, njit
from numba import types, _helperlib
from .types import float32, boolean, uint32, string, void, get_array_2d_type
from .checks import check_X_y, check_array
from .sample import SamplesCollection, add_samples
from .tree import TreeClassifier, TreeRegressor
from .tree_methods import (
tree_classifier_partial_fit,
tree_regressor_partial_fit,
tree_classifier_predict,
tree_regressor_predict,
tree_regressor_weighted_depth,
)
from .utils import get_type
spec_amf_learner = [
("n_features", uint32),
("n_estimators", uint32),
("step", float32),
("loss", string),
("use_aggregation", boolean),
("split_pure", boolean),
("n_jobs", uint32),
("n_samples_increment", uint32),
("verbose", boolean),
("samples", get_type(SamplesCollection)),
("iteration", uint32),
]
spec_amf_classifier = spec_amf_learner + [
("n_classes", uint32),
("dirichlet", float32),
("trees", types.List(get_type(TreeClassifier), reflected=True)),
]
# TODO: we can force pre-compilation when creating the nopython forest
@jitclass(spec_amf_classifier)
class AMFClassifierNoPython(object):
def __init__(
self,
n_classes,
n_features,
n_estimators,
step,
loss,
use_aggregation,
dirichlet,
split_pure,
n_jobs,
n_samples_increment,
verbose,
):
self.n_classes = n_classes
self.n_features = n_features
self.n_estimators = n_estimators
self.step = step
self.loss = loss
self.use_aggregation = use_aggregation
self.dirichlet = dirichlet
self.split_pure = split_pure
self.n_jobs = n_jobs
self.n_samples_increment = n_samples_increment
self.verbose = verbose
self.iteration = 0
samples = SamplesCollection(self.n_samples_increment, self.n_features)
self.samples = samples
# TODO: reflected lists will be replaced by typed list soon...
trees = [
TreeClassifier(
self.n_classes,
self.n_features,
self.step,
self.loss,
self.use_aggregation,
self.dirichlet,
self.split_pure,
self.samples,
)
for _ in range(n_estimators)
]
self.trees = trees
@njit(void(get_type(AMFClassifierNoPython), get_array_2d_type(float32), float32[::1],))
def forest_classifier_partial_fit(forest, X, y):
n_samples_batch, n_features = X.shape
# First, we save the new batch of data
n_samples_before = forest.samples.n_samples
# Add the samples in the forest
add_samples(forest.samples, X, y)
for i in range(n_samples_before, n_samples_before + n_samples_batch):
# Then we fit all the trees using all new samples
for tree in forest.trees:
tree_classifier_partial_fit(tree, i)
forest.iteration += 1
# TODO: code predict
# def predict(self, X, scores):
# scores.fill(0.0)
# n_samples_batch, _ = X.shape
# if self.iteration > 0:
# scores_tree = np.empty(self.n_classes, float32)
# for i in range(n_samples_batch):
# # print('i:', i)
# scores_i = scores[i]
# x_i = X[i]
# # print('x_i:', x_i)
# # The prediction is simply the average of the predictions
# for tree in self.trees:
# tree_predict(tree, x_i, scores_tree, self.use_aggregation)
# # print('scores_tree:', scores_tree)
# scores_i += scores_tree
# scores_i /= self.n_estimators
# # print('scores_i:', scores_i)
# else:
# raise RuntimeError("You must call ``partial_fit`` before ``predict``.")
@njit(
void(
get_type(AMFClassifierNoPython),
get_array_2d_type(float32),
get_array_2d_type(float32),
)
)
def forest_classifier_predict_proba(forest, X, scores):
# TODO: use predict_proba_tree from below ? Or put it in the tree ?
scores.fill(0.0)
n_samples_batch, _ = X.shape
scores_tree = np.empty(forest.n_classes, float32)
for i in range(n_samples_batch):
scores_i = scores[i]
x_i = X[i]
# The prediction is simply the average of the predictions
for tree in forest.trees:
tree_classifier_predict(tree, x_i, scores_tree, forest.use_aggregation)
scores_i += scores_tree
scores_i /= forest.n_estimators
@njit(
get_array_2d_type(float32)(
get_type(AMFClassifierNoPython), uint32, get_array_2d_type(float32)
)
)
def forest_classifier_predict_proba_tree(forest, idx_tree, X):
n_samples_batch, _ = X.shape
scores = np.empty((n_samples_batch, forest.n_classes), dtype=float32)
tree = forest.trees[idx_tree]
for i in range(n_samples_batch):
scores_i = scores[i]
x_i = X[i]
tree_classifier_predict(tree, x_i, scores_i, forest.use_aggregation)
return scores
spec_amf_regressor = spec_amf_learner + [
("trees", types.List(get_type(TreeRegressor), reflected=True)),
]
# TODO: we can force pre-compilation when creating the nopython forest
@jitclass(spec_amf_regressor)
class AMFRegressorNoPython(object):
def __init__(
self,
n_features,
n_estimators,
step,
loss,
use_aggregation,
split_pure,
n_jobs,
n_samples_increment,
verbose,
):
self.n_features = n_features
self.n_estimators = n_estimators
self.step = step
self.loss = loss
self.use_aggregation = use_aggregation
self.split_pure = split_pure
self.n_jobs = n_jobs
self.n_samples_increment = n_samples_increment
self.verbose = verbose
self.iteration = 0
samples = SamplesCollection(self.n_samples_increment, self.n_features)
self.samples = samples
# TODO: reflected lists will be replaced by typed list soon...
trees = [
TreeRegressor(
self.n_features,
self.step,
self.loss,
self.use_aggregation,
self.split_pure,
self.samples,
)
for _ in range(n_estimators)
]
self.trees = trees
@njit(void(get_type(AMFRegressorNoPython), get_array_2d_type(float32), float32[::1]))
def forest_regressor_partial_fit(forest, X, y):
n_samples_batch, n_features = X.shape
# First, we save the new batch of data
n_samples_before = forest.samples.n_samples
# Add the samples in the forest
add_samples(forest.samples, X, y)
for i in range(n_samples_before, n_samples_before + n_samples_batch):
# Then we fit all the trees using all new samples
for tree in forest.trees:
tree_regressor_partial_fit(tree, i)
forest.iteration += 1
# TODO: code predict
# def predict(self, X, scores):
# scores.fill(0.0)
# n_samples_batch, _ = X.shape
# if self.iteration > 0:
# scores_tree = np.empty(self.n_classes, float32)
# for i in range(n_samples_batch):
# # print('i:', i)
# scores_i = scores[i]
# x_i = X[i]
# # print('x_i:', x_i)
# # The prediction is simply the average of the predictions
# for tree in self.trees:
# tree_predict(tree, x_i, scores_tree, self.use_aggregation)
# # print('scores_tree:', scores_tree)
# scores_i += scores_tree
# scores_i /= self.n_estimators
# # print('scores_i:', scores_i)
# else:
# raise RuntimeError("You must call ``partial_fit`` before ``predict``.")
@njit(void(get_type(AMFRegressorNoPython), get_array_2d_type(float32), float32[::1]))
def forest_regressor_predict(forest, X, predictions):
# TODO: Useless ?
predictions.fill(0.0)
n_samples_batch, _ = X.shape
for i in range(n_samples_batch):
x_i = X[i]
prediction = 0
# The prediction is simply the average of the predictions
for tree in forest.trees:
prediction += tree_regressor_predict(tree, x_i, forest.use_aggregation)
predictions[i] = prediction / forest.n_estimators
@njit(
void(
get_type(AMFRegressorNoPython),
get_array_2d_type(float32),
get_array_2d_type(float32),
)
)
def forest_regressor_weighted_depths(forest, X, weighted_depths):
n_samples_batch, _ = X.shape
for i in range(n_samples_batch):
x_i = X[i]
n_tree = 0
for tree in forest.trees:
weighted_depth = tree_regressor_weighted_depth(
tree, x_i, forest.use_aggregation
)
weighted_depths[i, n_tree] = weighted_depth
n_tree += 1
@njit(
get_array_2d_type(float32)(
get_type(AMFClassifierNoPython), uint32, get_array_2d_type(float32)
)
)
def forest_classifier_predict_proba_tree(forest, idx_tree, X):
n_samples_batch, _ = X.shape
scores = np.empty((n_samples_batch, forest.n_classes), dtype=float32)
tree = forest.trees[idx_tree]
for i in range(n_samples_batch):
scores_i = scores[i]
x_i = X[i]
tree_classifier_predict(tree, x_i, scores_i, forest.use_aggregation)
return scores
# TODO: make amf.nopython.partial_fit work in a jitted function, test it and document it
class AMFLearner(object):
"""Base class for Aggregated Mondrian Forest classifier and regressors for online
learning.
Note
----
This class is not intended for end users but for development only.
"""
def __init__(
self,
n_estimators,
step,
loss,
use_aggregation,
split_pure,
n_jobs,
n_samples_increment,
random_state,
verbose,
):
"""Instantiates a `AMFLearner` instance.
Parameters
----------
n_estimators : :obj:`int`
The number of trees in the forest.
step : :obj:`float`
Step-size for the aggregation weights.
loss : :obj:`str`
The loss used for the computation of the aggregation weights.
use_aggregation : :obj:`bool`
Controls if aggregation is used in the trees. It is highly recommended to
leave it as `True`.
split_pure : :obj:`bool`
Controls if nodes that contains only sample of the same class should be
split ("pure" nodes). Default is `False`, namely pure nodes are not split,
but `True` can be sometimes better.
n_jobs : :obj:`int`
Sets the number of threads used to grow the tree in parallel. The default is
n_jobs=1, namely single-threaded. Fow now, this parameter has no effect and
only a single thread can be used.
n_samples_increment : :obj:`int`
Sets the minimum amount of memory which is pre-allocated each time extra
memory is required for new samples and new nodes. Decreasing it can slow
down training. If you know that each ``partial_fit`` will be called with
approximately `n` samples, you can set n_samples_increment = `n` if `n` is
larger than the default.
random_state : :obj:`int` or :obj:`None`
Controls the randomness involved in the trees.
verbose : :obj:`bool`, default = `False`
Controls the verbosity when fitting and predicting.
"""
# We will instantiate the numba class when data is passed to
# `partial_fit`, since we need to know about `n_features` among others things
self.no_python = None
self._n_features = None
self.n_estimators = n_estimators
self.step = step
self.loss = loss
self.use_aggregation = use_aggregation
self.split_pure = split_pure
self.n_jobs = n_jobs
self.n_samples_increment = n_samples_increment
self.random_state = random_state
self.verbose = verbose
if os.getenv("NUMBA_DISABLE_JIT", None) == "1":
self._using_numba = False
else:
self._using_numba = True
def partial_fit_helper(self, X, y):
"""Updates the classifier with the given batch of samples.
Parameters
----------
X : :obj:`np.ndarray`, shape=(n_samples, n_features)
Input features matrix.
y : :obj:`np.ndarray`
Input labels vector.
classes : :obj:`None`
Must not be used, only here for backwards compatibility
Returns
-------
output : :obj:`AMFClassifier`
Updated instance of :obj:`AMFClassifier`
"""
# First,ensure that X and y are C-contiguous and with float32 dtype
X, y = check_X_y(
X,
y,
accept_sparse=False,
accept_large_sparse=False,
dtype="float32",
order="C",
copy=False,
force_all_finite=True,
ensure_2d=True,
allow_nd=False,
multi_output=False,
ensure_min_samples=1,
ensure_min_features=1,
y_numeric=True,
estimator=self.__class__.__name__,
)
n_samples, n_features = X.shape
self._extra_y_test(y)
# This is the first call to `partial_fit`, so we need to instantiate
# the no python class
if self.no_python is None:
self._n_features = n_features
self._instantiate_nopython_class()
else:
_, n_features = X.shape
if n_features != self.n_features:
raise ValueError(
"`partial_fit` was first called with n_features=%d while "
"n_features=%d in this call" % (self.n_features, n_features)
)
self._set_random_state()
self._partial_fit(X, y)
self._put_back_random_state()
return self
# TODO: such methods should be private
def predict_helper(self, X):
"""Helper method for the predictions of the given features vectors. This is used
in the ``predict`` and ``predict_proba`` methods of ``AMFRegressor`` and
``AMFClassifier``.
Parameters
----------
X : :obj:`np.ndarray`, shape=(n_samples, n_features)
Input features matrix to predict for.
Returns
-------
output : :obj:`np.ndarray`
Returns the predictions for the input features
"""
X = check_array(
X,
accept_sparse=False,
accept_large_sparse=False,
dtype=["float32"],
order="C",
copy=False,
force_all_finite=True,
ensure_2d=True,
allow_nd=False,
ensure_min_samples=1,
ensure_min_features=1,
estimator=self.__class__.__name__,
)
n_samples, n_features = X.shape
if not self.no_python:
raise RuntimeError(
"You must call `partial_fit` before asking for predictions"
)
else:
if n_features != self.n_features:
raise ValueError(
"`partial_fit` was called with n_features=%d while predictions are "
"asked with n_features=%d" % (self.n_features, n_features)
)
# TODO: this is useless for predictions ?!?
self._set_random_state()
predictions = self._compute_predictions(X)
self._put_back_random_state()
return predictions
def weighted_depth_helper(self, X):
X = check_array(
X,
accept_sparse=False,
accept_large_sparse=False,
dtype=["float32"],
order="C",
copy=False,
force_all_finite=True,
ensure_2d=True,
allow_nd=False,
ensure_min_samples=1,
ensure_min_features=1,
estimator=self.__class__.__name__,
)
n_samples, n_features = X.shape
if not self.no_python:
raise RuntimeError(
"You must call `partial_fit` before asking for weighted depths"
)
else:
if n_features != self.n_features:
raise ValueError(
"`partial_fit` was called with n_features=%d while depths are "
"asked with n_features=%d" % (self.n_features, n_features)
)
weighted_depths = self._compute_weighted_depths(X)
return weighted_depths
def _compute_predictions(self, X):
pass
def _extra_y_test(self, y):
pass
def _instantiate_nopython_class(self):
pass
def _set_random_state(self):
# This uses a trick by Alexandre Gramfort,
# see https://github.com/numba/numba/issues/3249
if self._random_state >= 0:
if self._using_numba:
r = np.random.RandomState(self._random_state)
ptr = _helperlib.rnd_get_np_state_ptr()
ints, index = r.get_state()[1:3]
_helperlib.rnd_set_state(ptr, (index, [int(x) for x in ints]))
self._ptr = ptr
self._r = r
else:
np.random.seed(self._random_state)
def _put_back_random_state(self):
# This uses a trick by Alexandre Gramfort,
# see https://github.com/numba/numba/issues/3249
if self._random_state >= 0:
if self._using_numba:
ptr = self._ptr
r = self._r
index, ints = _helperlib.rnd_get_state(ptr)
r.set_state(("MT19937", ints, index, 0, 0.0))
def get_nodes_df(self, idx_tree):
import pandas as pd
tree = self.no_python.trees[idx_tree]
nodes = tree.nodes
n_nodes = nodes.n_nodes
index = nodes.index[:n_nodes]
parent = nodes.parent[:n_nodes]
left = nodes.left[:n_nodes]
right = nodes.right[:n_nodes]
feature = nodes.feature[:n_nodes]
threshold = nodes.threshold[:n_nodes]
time = nodes.time[:n_nodes]
depth = nodes.depth[:n_nodes]
memory_range_min = nodes.memory_range_min[:n_nodes]
memory_range_max = nodes.memory_range_max[:n_nodes]
n_samples = nodes.n_samples[:n_nodes]
weight = nodes.weight[:n_nodes]
log_weight_tree = nodes.log_weight_tree[:n_nodes]
is_leaf = nodes.is_leaf[:n_nodes]
# is_memorized = nodes.is_memorized[:n_nodes]
counts = nodes.counts[:n_nodes]
columns = [
"id",
"parent",
"left",
"right",
"depth",
"is_leaf",
"feature",
"threshold",
"time",
"n_samples",
"weight",
"log_weight_tree",
"memory_range_min",
"memory_range_max",
"counts",
]
data = {
"id": index,
"parent": parent,
"left": left,
"right": right,
"depth": depth,
"feature": feature,
"threshold": threshold,
"is_leaf": is_leaf,
"time": time,
"n_samples": n_samples,
"weight": weight,
"log_weight_tree": log_weight_tree,
"memory_range_min": [tuple(t) for t in memory_range_min],
"memory_range_max": [tuple(t) for t in memory_range_max],
"counts": [tuple(t) for t in counts],
}
df = pd.DataFrame(data, columns=columns)
return df
@property
def n_features(self):
""":obj:`int`: Number of features used during training."""
return self._n_features
@n_features.setter
def n_features(self, val):
raise ValueError("`n_features` is a readonly attribute")
@property
def n_estimators(self):
""":obj:`int`: Number of trees in the forest."""
return self._n_estimators
@n_estimators.setter
def n_estimators(self, val):
if self.no_python:
raise ValueError(
"You cannot modify `n_estimators` after calling `partial_fit`"
)
else:
if not isinstance(val, int):
raise ValueError("`n_estimators` must be of type `int`")
elif val < 1:
raise ValueError("`n_estimators` must be >= 1")
else:
self._n_estimators = val
@property
def n_jobs(self):
""":obj:`int`: Number of expected classes in the labels."""
return self._n_jobs
@n_jobs.setter
def n_jobs(self, val):
if self.no_python:
raise ValueError("You cannot modify `n_jobs` after calling `partial_fit`")
else:
if not isinstance(val, int):
raise ValueError("`n_jobs` must be of type `int`")
elif val < 1:
raise ValueError("`n_jobs` must be >= 1")
else:
self._n_jobs = val
@property
def n_samples_increment(self):
""":obj:`int`: Amount of memory pre-allocated each time extra memory is
required."""
return self._n_samples_increment
@n_samples_increment.setter
def n_samples_increment(self, val):
if self.no_python:
raise ValueError(
"You cannot modify `n_samples_increment` after calling `partial_fit`"
)
else:
if not isinstance(val, int):
raise ValueError("`n_samples_increment` must be of type `int`")
elif val < 1:
raise ValueError("`n_samples_increment` must be >= 1")
else:
self._n_samples_increment = val
@property
def step(self):
""":obj:`float`: Step-size for the aggregation weights."""
return self._step
@step.setter
def step(self, val):
if self.no_python:
raise ValueError("You cannot modify `step` after calling `partial_fit`")
else:
if not isinstance(val, float):
raise ValueError("`step` must be of type `float`")
elif val <= 0:
raise ValueError("`step` must be > 0")
else:
self._step = val
@property
def use_aggregation(self):
""":obj:`bool`: Controls if aggregation is used in the trees."""
return self._use_aggregation
@use_aggregation.setter
def use_aggregation(self, val):
if self.no_python:
raise ValueError(
"You cannot modify `use_aggregation` after calling `partial_fit`"
)
else:
if not isinstance(val, bool):
raise ValueError("`use_aggregation` must be of type `bool`")
else:
self._use_aggregation = val
@property
def split_pure(self):
""":obj:`bool`: Controls if nodes that contains only sample of the same class
should be split."""
return self._split_pure
@split_pure.setter
def split_pure(self, val):
if self.no_python:
raise ValueError(
"You cannot modify `split_pure` after calling `partial_fit`"
)
else:
if not isinstance(val, bool):
raise ValueError("`split_pure` must be of type `bool`")
else:
self._split_pure = val
@property
def verbose(self):
""":obj:`bool`: Controls the verbosity when fitting and predicting."""
return self._verbose
@verbose.setter
def verbose(self, val):
if self.no_python:
raise ValueError("You cannot modify `verbose` after calling `partial_fit`")
else:
if not isinstance(val, bool):
raise ValueError("`verbose` must be of type `bool`")
else:
self._verbose = val
@property
def loss(self):
""":obj:`str`: The loss used for the computation of the aggregation weights."""
return "log"
@loss.setter
def loss(self, val):
pass
@property
def random_state(self):
""":obj:`int` or :obj:`None`: Controls the randomness involved in the trees."""
if self._random_state == -1:
return None
else:
return self._random_state
@random_state.setter
def random_state(self, val):
if self.no_python:
raise ValueError(
"You cannot modify `random_state` after calling `partial_fit`"
)
else:
if val is None:
self._random_state = -1
elif not isinstance(val, int):
raise ValueError("`random_state` must be of type `int`")
elif val < 0:
raise ValueError("`random_state` must be >= 0")
else:
self._random_state = val
def __repr__(self):
r = self.__class__.__name__
r += "n_estimators={n_estimators}, ".format(n_estimators=self.n_estimators)
r += "step={step}, ".format(step=self.step)
r += "loss={loss}, ".format(loss=self.loss)
r += "use_aggregation={use_aggregation}, ".format(
use_aggregation=self.use_aggregation
)
r += "split_pure={split_pure}, ".format(split_pure=self.split_pure)
r += "n_jobs={n_jobs}, ".format(n_jobs=self.n_jobs)
r += "random_state={random_state}, ".format(random_state=self.random_state)
r += "verbose={verbose})".format(verbose=self.verbose)
return r
# TODO: add attributes in docstring
class AMFClassifier(AMFLearner):
"""Aggregated Mondrian Forest classifier for online learning. This algorithm
is truly online, in the sense that a single pass is performed, and that predictions
can be produced anytime.
Each node in a tree predicts according to the distribution of the labels
it contains. This distribution is regularized using a "Jeffreys" prior
with parameter ``dirichlet``. For each class with `count` labels in the
node and `n_samples` samples in it, the prediction of a node is given by
(count + dirichlet) / (n_samples + dirichlet * n_classes)
The prediction for a sample is computed as the aggregated predictions of all the
subtrees along the path leading to the leaf node containing the sample. The
aggregation weights are exponential weights with learning rate ``step`` and loss
``loss`` when ``use_aggregation`` is ``True``.
This computation is performed exactly thanks to a context tree weighting algorithm.
More details can be found in the paper cited in references below.
The final predictions are the average class probabilities predicted by each of the
``n_estimators`` trees in the forest.
Note
----
All the parameters of ``AMFClassifier`` become **read-only** after the first call
to ``partial_fit``
References
----------
J. Mourtada, S. Gaiffas and E. Scornet, *AMF: Aggregated Mondrian Forests for Online Learning*, arXiv:1906.10529, 2019
"""
def __init__(
self,
n_classes,
n_estimators=10,
step=1.0,
loss="log",
use_aggregation=True,
dirichlet=None,
split_pure=False,
n_jobs=1,
n_samples_increment=1024,
random_state=None,
verbose=False,
):
"""Instantiates a `AMFClassifier` instance.
Parameters
----------
n_classes : :obj:`int`
Number of expected classes in the labels. This is required since we
don't know the number of classes in advance in a online setting.
n_estimators : :obj:`int`, default = 10
The number of trees in the forest.
step : :obj:`float`, default = 1
Step-size for the aggregation weights. Default is 1 for classification with
the log-loss, which is usually the best choice.
loss : {"log"}, default = "log"
The loss used for the computation of the aggregation weights. Only "log"
is supported for now, namely the log-loss for multi-class
classification.
use_aggregation : :obj:`bool`, default = `True`
Controls if aggregation is used in the trees. It is highly recommended to
leave it as `True`.
dirichlet : :obj:`float` or :obj:`None`, default = `None`
Regularization level of the class frequencies used for predictions in each
node. Default is dirichlet=0.5 for n_classes=2 and dirichlet=0.01 otherwise.
split_pure : :obj:`bool`, default = `False`
Controls if nodes that contains only sample of the same class should be
split ("pure" nodes). Default is `False`, namely pure nodes are not split,
but `True` can be sometimes better.
n_jobs : :obj:`int`, default = 1
Sets the number of threads used to grow the tree in parallel. The default is
n_jobs=1, namely single-threaded. Fow now, this parameter has no effect and
only a single thread can be used.
n_samples_increment : :obj:`int`, default = 1024
Sets the minimum amount of memory which is pre-allocated each time extra
memory is required for new samples and new nodes. Decreasing it can slow
down training. If you know that each ``partial_fit`` will be called with
approximately `n` samples, you can set n_samples_increment = `n` if `n` is
larger than the default.
random_state : :obj:`int` or :obj:`None`, default = `None`
Controls the randomness involved in the trees.
verbose : :obj:`bool`, default = `False`
Controls the verbosity when fitting and predicting.
"""
AMFLearner.__init__(
self,
n_estimators=n_estimators,
step=step,
loss=loss,
use_aggregation=use_aggregation,
split_pure=split_pure,
n_jobs=n_jobs,
n_samples_increment=n_samples_increment,
random_state=random_state,
verbose=verbose,
)
self.n_classes = n_classes
if dirichlet is None:
if self.n_classes == 2:
self.dirichlet = 0.5
else:
self.dirichlet = 0.01
else:
self.dirichlet = dirichlet
self._classes = set(range(n_classes))
def _extra_y_test(self, y):
if y.min() < 0:
raise ValueError("All the values in `y` must be non-negative")
y_max = y.max()
if y_max not in self._classes:
raise ValueError("n_classes=%d while y.max()=%d" % (self.n_classes, y_max))
def _instantiate_nopython_class(self):
self.no_python = AMFClassifierNoPython(
self.n_classes,
self.n_features,
self.n_estimators,
self.step,
self.loss,
self.use_aggregation,
self.dirichlet,
self.split_pure,
self.n_jobs,
self.n_samples_increment,
self.verbose,
)
def _partial_fit(self, X, y):
forest_classifier_partial_fit(self.no_python, X, y)
def partial_fit(self, X, y, classes=None):
"""Updates the classifier with the given batch of samples.
Parameters
----------
X : :obj:`np.ndarray`, shape=(n_samples, n_features)
Input features matrix.
y : :obj:`np.ndarray`
Input labels vector.
classes : :obj:`None`
Must not be used, only here for backwards compatibility
Returns
-------
output : :obj:`AMFClassifier`
Updated instance of :obj:`AMFClassifier`
"""
return AMFLearner.partial_fit_helper(self, X, y)
def _compute_predictions(self, X):
n_samples, n_features = X.shape
scores = np.zeros((n_samples, self.n_classes), dtype="float32")
forest_classifier_predict_proba(self.no_python, X, scores)
return scores
def predict_proba(self, X):
"""Predicts the class probabilities for the given features vectors.
Parameters
----------
X : :obj:`np.ndarray`, shape=(n_samples, n_features)
Input features matrix to predict for.
Returns
-------
output : :obj:`np.ndarray`, shape=(n_samples, n_classes)
Returns the predicted class probabilities for the input features
"""
return AMFLearner.predict_helper(self, X)
# TODO: put in AMFLearner and reorganize
def predict_proba_tree(self, X, tree):
"""Predicts the class probabilities for the given features vectors using a
single tree at given index ``tree``. Should be used only for debugging or
visualisation purposes.
Parameters
----------
X : :obj:`np.ndarray`, shape=(n_samples, n_features)
Input features matrix to predict for.