Separate Waves Classifier¶
Abstract of Separate Waves (SepWav)
Extracted from "A New Longitudinal Classification Method Based on Stacking Predictions for Separate Time Points" (BCS SGAI AI-2025).
Biomedical research often uses longitudinal data with repeated measurements of variables across time (e.g. cholesterol measured across time), which is challenging for standard machine learning algorithms due to intrinsic temporal dependencies. The Separate Waves (SepWav) data-transformation method trains a base classifier for each time point ("wave") and aggregates their predictions via voting. However, the simplicity of the voting mechanism may not be enough to capture complex patterns of time-dependent interactions involving the base classifiers' predictions. Hence, we propose a novel SepWav method where the simple voting mechanism is replaced by a stacking-based meta-classifier that integrates the base classifiers' wave-specific predictions into a final predicted class label, aiming at improving predictive performance. Experiments with 20 datasets of ageing-related diseases have shown that, overall, the proposed Stacking-based SepWav method achieved significantly better predictive performance than two other methods for longitudinal classification in most cases, when using class-weight adjustment as a class-balancing method.
SepWav ¶
Bases: BaseEstimator, ClassifierMixin, DataPreparationMixin
SepWav stands for Separate Waves, a training done wave-by-wave for longitudinal dataset.
The SepWav class implements the Separate Waves strategy, treating each wave (time point) as a separate dataset.
A classifier is trained on each wave independently, and their predictions are combined using ensemble methods
such as voting or stacking. The workflow supports both binary and multiclass classification. When stacking is
selected, the base wave estimators must implement predict_proba, because the meta-learner is trained on
wave-level class-probability outputs.
Ensemble Strategies
Supported ensemble methods include:
- Simple majority voting
- Weighted voting (e.g., decaying weights for older waves)
- Stacking with a meta-learner trained on wave-level class probabilities
Refer to LongitudinalVoting and LongitudinalStacking for mathematical details.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
estimator
|
Union[ClassifierMixin, CustomClassifierMixinEstimator]
|
Base classifier for each wave. Defaults to None. |
None
|
features_group
|
List[List[int]]
|
Temporal matrix where each sublist contains indices of a longitudinal attribute's waves. Defaults to None. |
None
|
non_longitudinal_features
|
List[Union[int, str]]
|
List of indices or names of non-longitudinal features. Defaults to None. |
None
|
feature_list_names
|
List[str]
|
List of feature names in the dataset. Defaults to None. |
None
|
voting
|
LongitudinalEnsemblingStrategy
|
Ensemble strategy. Defaults to |
MAJORITY_VOTING
|
stacking_meta_learner
|
Union[CustomClassifierMixinEstimator, ClassifierMixin, None]
|
Meta-learner for stacking. Defaults to |
LogisticRegression()
|
n_jobs
|
int
|
Number of parallel jobs. Defaults to None. |
None
|
parallel
|
bool
|
Whether to run wave fitting in parallel. Defaults to False. |
False
|
num_cpus
|
int
|
Number of CPUs for parallel processing. Defaults to -1 (all available CPUs). |
-1
|
class_weight
|
Any
|
Class-weight specification to forward to wave estimators when supported. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
dataset |
DataFrame
|
Training dataset. |
estimator |
BaseEstimator
|
Base classifier for each wave. |
estimators |
List
|
List of trained classifiers for each wave. |
voting |
LongitudinalEnsemblingStrategy
|
Ensemble strategy used. |
stacking_meta_learner |
Union[CustomClassifierMixinEstimator, ClassifierMixin]
|
Meta-learner for stacking. |
clf_ensemble |
BaseEstimator
|
Combined ensemble classifier. |
n_jobs |
int
|
Number of parallel jobs. |
parallel |
bool
|
Whether parallel processing is enabled. |
num_cpus |
int
|
Number of CPUs used. |
class_weight |
Any
|
Requested class-weight configuration applied to compatible estimators. |
Examples:
Below are examples using the "stroke.csv" dataset. Replace "stroke.csv" with your actual dataset path.
Basic Usage
from scikit_longitudinal.data_preparation import LongitudinalDataset
from scikit_longitudinal.data_preparation import SepWav
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from scikit_longitudinal.estimators.ensemble.longitudinal_voting.longitudinal_voting import (
LongitudinalEnsemblingStrategy,
)
# 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)
# Initialize classifier
classifier = RandomForestClassifier()
# Initialize SepWav
sepwav = SepWav(
estimator=classifier,
features_group=dataset.feature_groups(),
non_longitudinal_features=dataset.non_longitudinal_features(),
feature_list_names=dataset.data.columns.tolist(),
voting=LongitudinalEnsemblingStrategy.MAJORITY_VOTING
)
# Fit and predict
sepwav.fit(dataset.X_train, dataset.y_train)
y_pred = sepwav.predict(dataset.X_test)
# Evaluate
accuracy = accuracy_score(dataset.y_test, y_pred)
print(f"Accuracy: {accuracy}")
Advanced: stacking ensemble
from scikit_longitudinal.data_preparation import LongitudinalDataset
from scikit_longitudinal.data_preparation import SepWav
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression
from scikit_longitudinal.estimators.ensemble.longitudinal_voting.longitudinal_voting import (
LongitudinalEnsemblingStrategy,
)
# 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)
# Initialize classifier
classifier = RandomForestClassifier()
# Initialize SepWav with stacking
sepwav = SepWav(
estimator=classifier,
features_group=dataset.feature_groups(),
non_longitudinal_features=dataset.non_longitudinal_features(),
feature_list_names=dataset.data.columns.tolist(),
voting=LongitudinalEnsemblingStrategy.STACKING,
stacking_meta_learner=LogisticRegression()
)
# Fit and predict
sepwav.fit(dataset.X_train, dataset.y_train)
y_pred = sepwav.predict(dataset.X_test)
# Evaluate
accuracy = accuracy_score(dataset.y_test, y_pred)
print(f"Accuracy: {accuracy}")
Advanced: parallel processing
# ... Similar to the previous example, but with parallel processing enabled ...
# Initialize SepWav with parallel processing
sepwav = SepWav(
estimator=classifier,
features_group=dataset.feature_groups(),
non_longitudinal_features=dataset.non_longitudinal_features(),
feature_list_names=dataset.data.columns.tolist(),
parallel=True, # Enable parallel processing
num_cpus=4 # Specify number of CPUs to use (or -1 for all available CPUs)
)
# ... Similar to the previous example, but with parallel processing enabled ...
Source code in scikit_longitudinal/data_preparation/separate_waves.py
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 | |
fit(X, y, sample_weight=None)
¶
Fit the SepWav model to the training data.
Trains a classifier for each wave and combines them using the specified ensemble strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Union[List[List[float]], ndarray]
|
Input samples. |
required |
y
|
Union[List[float], ndarray]
|
Target values. |
required |
sample_weight
|
Union[List[float], ndarray]
|
Sample weights. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
SepWav |
Fitted instance. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If required parameters (estimator, features_group) are None or ensemble strategy is invalid. |
Source code in scikit_longitudinal/data_preparation/separate_waves.py
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 | |
predict(X)
¶
Predict class labels for input samples.
Uses the ensemble classifier to combine predictions from individual wave classifiers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Union[List[List[float]], ndarray]
|
Input samples. |
required |
Returns:
| Type | Description |
|---|---|
Union[List[float], ndarray]
|
Union[List[float], np.ndarray]: Predicted class labels. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If the ensemble classifier does not support prediction. |
Source code in scikit_longitudinal/data_preparation/separate_waves.py
predict_proba(X)
¶
Predict class probabilities for input samples.
Computes probabilities using the ensemble classifier's predict_proba method, if available.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Union[List[List[float]], ndarray]
|
Input samples. |
required |
Returns:
| Type | Description |
|---|---|
Union[List[List[float]], ndarray]
|
Union[List[List[float]], np.ndarray]: Predicted class probabilities. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If the ensemble classifier does not support probability predictions. |
Source code in scikit_longitudinal/data_preparation/separate_waves.py
predict_wave(wave, X)
¶
Predict class labels using the classifier for a specific wave.
Useful for analyzing wave-specific performance or custom ensemble strategies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wave
|
int
|
Wave number (0-based index). |
required |
X
|
Union[List[List[float]], ndarray]
|
Input samples. |
required |
Returns:
| Type | Description |
|---|---|
Union[List[float], ndarray]
|
Union[List[float], np.ndarray]: Predicted class labels for the specified wave. |
Source code in scikit_longitudinal/data_preparation/separate_waves.py
SepWav ensemble back-ends¶
SepWav delegates the final aggregation of per-wave predictions to one of the
two classifiers below.
Longitudinal Voting Classifier¶
Aggregates per-wave predictions with a configurable voting rule: simple majority, linear or exponential recency decay, or cross-validation-weighted voting.
LongitudinalVotingClassifier ¶
Bases: CustomClassifierMixinEstimator
Aggregates predictions from pre-trained base estimators using the voting rule specified by
LongitudinalEnsemblingStrategy (majority, linear or exponential decay, or cross-validation-weighted). Supports
both binary and multiclass targets, and wraps scikit-learn's VotingClassifier under the hood.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
voting
|
LongitudinalEnsemblingStrategy, default=LongitudinalEnsemblingStrategy.MAJORITY_VOTING
|
The voting strategy to be used for the ensemble. Refer to the LongitudinalEnsemblingStrategy enum. |
MAJORITY_VOTING
|
estimators
|
List[CustomClassifierMixinEstimator]
|
A list of classifiers for the ensemble. Note that the classifiers need to be trained before being passed to the LongitudinalVotingClassifier. |
required |
extract_wave
|
Callable
|
A function to extract specific wave data for training. Defaults to None. When provided, the order of
|
None
|
n_jobs
|
int, default=1
|
The number of jobs to run in parallel. |
1
|
Attributes:
| Name | Type | Description |
|---|---|---|
clf_ensemble |
LongitudinalCustomVoting
|
The underlying custom voting classifier instance. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no estimators are provided or if an invalid voting strategy is specified. |
NotFittedError
|
If attempting to predict or predict_proba before fitting the model. |
Notes
predict_probareturns normalised vote shares across classes. These are consistent with the hard-voting decision returned bypredict, but they are not calibrated probabilities.
Source code in scikit_longitudinal/estimators/ensemble/longitudinal_voting/longitudinal_voting.py
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 | |
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
LongitudinalEnsemblingStrategy
¶
Bases: Enum
An enum for the different longitudinal voting strategies.
Attributes:
| Name | Type | Description |
|---|---|---|
MAJORITY_VOTING |
int
|
Simple consensus voting where the most frequent prediction is selected. |
DECAY_LINEAR_VOTING |
int
|
Weights each classifier's vote based on the recency of its wave using a linear decay. Weight formula: \[w_i = \frac{i}{\sum_{j=1}^{N} j}\]
|
DECAY_EXPONENTIAL_VOTING |
int
|
Weights each classifier's vote based on the recency of its wave using an exponential decay. Weight formula: \[w_i = \frac{e^{i}}{\sum_{j=1}^{N} e^{j}}\]
|
CV_BASED_VOTING |
int
|
Weights each classifier based on its cross-validation accuracy on the training data. Weight formula: \[w_i = \frac{A_i}{\sum_{j=1}^{N} A_j}\]
|
STACKING |
int
|
Stacking ensemble strategy uses a meta-learner to combine predictions of base classifiers. The meta-learner is trained on meta-features formed from the base classifiers' predicted class probabilities. This approach is suitable when the cardinality of meta-features is smaller than the original feature set. In stacking, for each wave \(i\) (\(i \in \{1, 2, \ldots, N\}\)), a base classifier \(C_i\) is trained on \((X_i, T_N)\). The class-probability output from \(C_i\) is denoted as \(V_i\), forming the meta-features \(\mathbf{V} = [V_1, V_2, ..., V_N]\). The meta-learner \(M\) is then trained on \((\mathbf{V}, T_N)\), and for a new instance \(x\), the final prediction is \(P(x) = M(\mathbf{V}(x))\). |
Source code in scikit_longitudinal/estimators/ensemble/longitudinal_voting/longitudinal_voting.py
Longitudinal Stacking Classifier¶
Trains a meta-learner on the class probabilities emitted by the per-wave
classifiers fitted by SepWav.
LongitudinalStackingClassifier ¶
Bases: CustomClassifierMixinEstimator
Trains a meta-learner on the class-probability outputs of the pre-trained base estimators. Each base estimator
must implement predict_proba; the meta-learner is then fitted on the stacked probabilities to produce the
final prediction. Supports both binary and multiclass targets, and wraps scikit-learn's StackingClassifier
under the hood. When extract_wave is provided, internal refits remain wave-specific.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
estimators
|
List[CustomClassifierMixinEstimator]
|
The base estimators for the ensemble. These can be passed directly, or as estimators prepared by |
required |
meta_learner
|
Optional[Union[CustomClassifierMixinEstimator, ClassifierMixin]], default=LogisticRegression()
|
The meta-learner to be used in stacking. Can be any scikit-learn compliant classifier. |
LogisticRegression()
|
n_jobs
|
int, default=1
|
The number of jobs to run in parallel for fitting base estimators. |
1
|
extract_wave
|
Callable
|
Optional wave extractor used when estimators should remain wave-specific inside stacking, such as the
|
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
clf_ensemble |
StackingClassifier
|
The underlying scikit-learn StackingClassifier instance. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no base estimators are provided, if a base estimator does not implement |
NotFittedError
|
If attempting to predict or predict_proba before fitting the model. |
Source code in scikit_longitudinal/estimators/ensemble/longitudinal_stacking/longitudinal_stacking.py
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 | |
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 |