=
make_classification
(
n_samples
=
1000
,
n_informative
=
5
,
n_classes
=
num_classes
)
dtrain
=
xgb
.
DMatrix
(
data
=
X
,
label
=
y
)
num_parallel_tree
=
4
num_boost_round
=
16
booster
=
xgb
.
train
({
'num_parallel_tree'
:
4
,
'subsample'
:
0.5
,
'num_class'
:
3
},
num_boost_round
=
num_boost_round
,
dtrain
=
dtrain
)
sliced
:
xgb
.
Booster
=
booster
[
3
:
7
]
trees
=
[
_
for
_
in
booster
]
assert
len
(
trees
)
==
num_boost_round
The sliced model is a copy of selected trees, that means the model itself is immutable
during slicing. This feature is the basis of save_best option in early stopping
callback. See Demo for prediction using individual trees and model slices for a worked example on
how to combine prediction with sliced trees.