Lexico Deep Forest Classifier¶
Abstract of LexicoDeepForestClassifier
Extracted from Ribeiro & Freitas (2024), "Lexicographical random forests for longitudinal data classification".
Standard supervised machine learning methods often ignore the temporal information represented in longitudinal data, but that information can lead to more precise predictions in classification tasks. Data preprocessing techniques and classification algorithms can be adapted to cope directly with longitudinal data inputs, making use of temporal information such as the time-index of features and previous measurements of the class variable. In this article, we propose two changes to the classification task of predicting age-related diseases in a real-world dataset created from the English Longitudinal Study of Ageing. First, we explore the addition of previous measurements of the class variable, and estimating the missing data in those added features using intermediate classifiers. Second, we propose a new split-feature selection procedure for a random forest's decision trees, which considers the candidate features' time-indexes, in addition to the information gain ratio. Our experiments compared the proposed approaches to baseline approaches, in 3 prediction scenarios, varying the "time gap" for the prediction - how many years in advance the class (occurrence of an age-related disease) is predicted. The experiments were performed on 10 datasets varying the class variable, and showed that the proposed approaches increased the random forest's predictive accuracy.
Adapted and integrated into a Deep Forest cascade, this estimator stacks layers of LexicoRandomForestClassifiers (and optional diversity learners) so that each layer applies the lexicographic split-selection procedure above while propagating wave-aware predictions through the cascade.
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.
LexicoDeepForestClassifier ¶
Bases: CustomClassifierMixinEstimator
Lexico Deep Forest Classifier for longitudinal data analysis.
This classifier extends the Deep Forest framework for longitudinal data by stacking layers of
longitudinal-adapted base estimators (typically LexicoRandomForestClassifier) so each layer's predictions
become additional features for the next. Every base tree applies a lexicographic split-selection rule: the
primary objective maximises the information-gain ratio (entropy criterion), and the secondary objective
favours features from more recent waves whenever competing gain ratios are within threshold_gain. For more
information on Deep Forest, see DF21.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features_group
|
List[List[int]]
|
Temporal matrix of feature indices for longitudinal attributes, ordered by recency. Required for longitudinal functionality. |
None
|
longitudinal_base_estimators
|
Optional[List[LongitudinalEstimatorConfig]]
|
List of configurations for longitudinal base estimators. Each config specifies the classifier type, count,
and optional hyperparameters. Available types: |
None
|
non_longitudinal_features
|
List[Union[int, str]]
|
Indices of non-longitudinal features. Defaults to None. |
None
|
diversity_estimators
|
bool, default=True
|
Whether to include diversity estimators (weak learners) in the ensemble. If True, two completely random
|
True
|
class_weight
|
Optional[Union[dict, List[dict], str]]
|
Class weights passed to each longitudinal base estimator unless explicitly provided in the estimator's hyperparameters. |
None
|
random_state
|
int
|
Seed for random number generation. Defaults to None. |
None
|
single_classifier_type
|
Optional[Union[LongitudinalClassifierType, str]]
|
Type of a single classifier to use if |
None
|
single_count
|
Optional[int]
|
Number of instances of the single classifier type. |
None
|
max_layers
|
int, default=5
|
Maximum number of cascade layers in the deep forest. |
5
|
Attributes:
| Name | Type | Description |
|---|---|---|
_deep_forest |
CascadeForestClassifier
|
The underlying deep forest model. |
classes_ |
ndarray
|
The class labels. |
Examples:
Basic Usage
from scikit_longitudinal.estimators.ensemble.lexicographical.lexico_deep_forest import LexicoDeepForestClassifier, LongitudinalEstimatorConfig, LongitudinalClassifierType
import numpy as np
from sklearn.metrics import accuracy_score
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)
# Configure base estimators
lexico_rf_config = LongitudinalEstimatorConfig(
classifier_type=LongitudinalClassifierType.LEXICO_RF,
count=3,
)
clf = LexicoDeepForestClassifier(
features_group=dataset.feature_groups(),
longitudinal_base_estimators=[lexico_rf_config],
)
clf.fit(dataset.X_train, dataset.y_train)
y_pred = clf.predict(dataset.X_train)
print(f"Predictions: {y_pred}")
Advanced: multiple estimator types
# ... Similar setup as above ...
complete_random_lexico_rf = LongitudinalEstimatorConfig(
classifier_type=LongitudinalClassifierType.COMPLETE_RANDOM_LEXICO_RF,
count=2,
)
clf = LexicoDeepForestClassifier(
features_group=features_group,
longitudinal_base_estimators=[lexico_rf_config, complete_random_lexico_rf],
)
clf.fit(X, y)
# ... Similar prediction and evaluation as above ...
Advanced: disabling diversity estimators
Source code in scikit_longitudinal/estimators/ensemble/lexicographical/lexico_deep_forest.py
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 | |
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
LongitudinalClassifierType
¶
Bases: Enum
Enumeration of classifier types that are adapted for longitudinal data analysis.
This enumeration provides identifiers for longitudinal-adapted classifiers that can be used within the LexicoDeepForestClassifier ensemble.
Attributes:
| Name | Type | Description |
|---|---|---|
LEXICO_RF |
Identifier for a Lexico Random Forest Classifier. |
|
COMPLETE_RANDOM_LEXICO_RF |
Identifier for a Lexico Random Forest Classifier with complete randomness. |
Source code in scikit_longitudinal/estimators/ensemble/lexicographical/lexico_deep_forest.py
LongitudinalEstimatorConfig
dataclass
¶
Configuration for a longitudinal base estimator within the LexicoDeepForestClassifier ensemble.
This configuration class is used to specify the type of longitudinal classifier, the number of times it should be instantiated within the ensemble, and any hyperparameters for the individual classifiers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
classifier_type
|
LongitudinalClassifierType
|
The type of longitudinal classifier to be used. |
required |
count
|
int
|
The number of times the classifier should be replicated in the ensemble. Defaults to 2. |
2
|
hyperparameters
|
Optional[Dict[str, Any]]
|
A dictionary of hyperparameters for the classifier. Defaults to None. |
None
|