Nested Trees Classifier¶
Abstract of NestedTreesClassifier
Extracted from Ovchinnik, Otero & Freitas (2022), "Nested trees for longitudinal classification".
Longitudinal datasets contain repeated measurements of the same variables at different points in time. Longitudinal data mining algorithms aim to utilize such datasets to extract interesting knowledge and produce useful models. Many existing longitudinal classification methods either dismiss the longitudinal aspect of the data during model construction or produce complex models that are scarcely interpretable. We propose a new longitudinal classification algorithm based on decision trees, named Nested Trees. It utilizes a unique longitudinal model construction method that is fully aware of the longitudinal aspect of the predictive attributes (variables) and constructs tree nodes that make decisions based on a longitudinal attribute as a whole, considering measurements of that attribute across multiple time points. The algorithm was evaluated using 10 classification tasks based on the English Longitudinal Study of Ageing (ELSA) data.
What are features_group and non_longitudinal_features?
Two key attributes, features_group and non_longitudinal_features, enable algorithms to interpret the
temporal structure of longitudinal data.
- features_group: A list of lists where each sublist contains indices of a longitudinal attribute's waves, ordered from oldest to most recent. This captures temporal dependencies.
- non_longitudinal_features: A list of indices for static, non-temporal features excluded from the temporal matrix.
Proper setup of these attributes is critical for leveraging temporal patterns effectively.
NestedTreesClassifier ¶
Bases: CustomClassifierMixinEstimator
Nested Trees Classifier for longitudinal data classification.
The Nested Trees Classifier enhances traditional decision tree methods with a two-level, longitudinal-aware
construction: the outer tree picks splits on a whole longitudinal attribute (the group of time-specific
features that represent repeated measurements of the same variable across waves) instead of on a single
feature, and each outer node hosts an inner DecisionTreeClassifier from scikit-learn that partitions the
data using only the measurements of that selected attribute across time. This preserves the longitudinal
structure during model construction, keeps decisions interpretable (each outer node is labelled by one
attribute), and naturally captures temporal patterns and dependencies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features_group
|
List[List[int]]
|
Temporal matrix of feature indices for longitudinal attributes. Required for longitudinal functionality. |
None
|
non_longitudinal_features
|
List[Union[int, str]]
|
Indices of static, non-temporal features. Defaults to None. |
None
|
max_outer_depth
|
int
|
Maximum depth of the outer decision tree. Defaults to 3. |
3
|
max_inner_depth
|
int
|
Maximum depth of inner decision trees. Defaults to 2. |
2
|
min_outer_samples
|
int
|
Minimum samples required to split an outer node. Defaults to 5. |
5
|
inner_estimator_hyperparameters
|
Optional[Dict[str, Any]]
|
Hyperparameters for inner decision trees. Defaults to None. |
None
|
class_weight
|
Optional[Union[dict, List[dict], str]]
|
Class weights applied to every inner decision tree unless explicitly provided through
|
None
|
save_nested_trees
|
bool
|
If True, saves visualizations of the nested structure. Defaults to False. |
False
|
parallel
|
bool
|
Enables parallel processing for fitting inner trees. Defaults to False. |
False
|
num_cpus
|
int
|
Number of CPUs for parallel processing (-1 uses all available). Defaults to -1. |
-1
|
Attributes:
| Name | Type | Description |
|---|---|---|
root |
Node
|
Root node of the outer tree, initialized as None and set during fitting. |
classes_ |
ndarray
|
Unique class labels, set during fitting. |
Examples:
Basic Usage
from sklearn.metrics import accuracy_score
from scikit_longitudinal.estimators.ensemble import NestedTreesClassifier
import numpy as np
from scikit_longitudinal.data_preparation import LongitudinalDataset
# Load dataset
dataset = LongitudinalDataset('./stroke_longitudinal.csv')
dataset.load_data()
dataset.load_target(target_column="stroke_w2")
dataset.setup_features_group("elsa")
dataset.load_train_test_split(test_size=0.2, random_state=42)
clf = NestedTreesClassifier(features_group=dataset.feature_groups())
clf.fit(dataset.X_train, dataset.y_train)
y_pred = clf.predict(dataset.X_test)
print(f"Accuracy: {accuracy_score(dataset.y_test, y_pred)}")
Advanced: customising inner tree hyperparameters
# ... Similar setup as above ...
inner_params = {'criterion': 'gini', 'max_depth': 3}
clf = NestedTreesClassifier(
features_group=features_group,
non_longitudinal_features=non_longitudinal_features,
inner_estimator_hyperparameters=inner_params
)
clf.fit(X, y)
# ... Similar prediction and evaluation as above ...
Source code in scikit_longitudinal/estimators/ensemble/nested_trees/nested_trees.py
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 | |
fit(X, y=None, sample_weight=None)
¶
Fit the classifier to the training data.
Validates X (and y when provided) with scikit-learn's
check_X_y / check_array and then delegates to the subclass
implementation in _fit. sample_weight is forwarded only when
the subclass's _fit declares it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Training input samples of shape |
required |
y
|
ndarray
|
Target class labels of shape |
None
|
sample_weight
|
ndarray
|
Per-sample weights of shape |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
CustomClassifierMixinEstimator |
CustomClassifierMixinEstimator
|
The fitted estimator ( |
Source code in scikit_longitudinal/templates/custom_classifier_mixin_estimator.py
predict(X)
¶
Predict class labels for the input samples.
Validates X with scikit-learn's check_array and delegates to
the subclass implementation in _predict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Input samples of shape |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Predicted class labels of shape |
Source code in scikit_longitudinal/templates/custom_classifier_mixin_estimator.py
predict_proba(X)
¶
Predict class probabilities for the input samples.
Validates X with scikit-learn's check_array and delegates to
the subclass implementation in _predict_proba.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Input samples of shape |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Class probabilities of shape |
ndarray
|
with columns ordered as in |
Source code in scikit_longitudinal/templates/custom_classifier_mixin_estimator.py
print_nested_tree(node=None, depth=0, prefix='', parent_name='')
¶
Print the nested tree structure for interpretation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
Optional[Node]
|
Starting node (defaults to root if None). |
None
|
depth
|
int
|
Current depth (defaults to 0). |
0
|
prefix
|
str
|
String to prepend to node names (defaults to ""). |
''
|
parent_name
|
str
|
Parent node name (defaults to ""). |
''
|
Debugging Aid
Use this to visualize the tree hierarchy and verify model construction. Careful, it could be very verbose for large trees.