# `SLDA`

### *class* capymoa.ocl.strategy.SLDA[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/ocl/strategy/_slda.py#L38)

Bases: [`BatchClassifier`](capymoa.base.BatchClassifier.md#capymoa.base.BatchClassifier)

Streaming Linear Discriminant Analysis.

Streaming Linear Discriminant Analysis (SLDA) <sup>[1](#f0)</sup> is a prototype classifier that
incrementally accumulates the mean of each class and the mean and covariance across
all classes. Note that this method does not gracefully forget and may not handle
concept drift well.

<sup>[1](#f0)</sup> uses SLDA ontop of a pre-trained model to perform continual learning. See
<sup>[2](#f1)</sup> and <sup>[3](#f2)</sup> for more details on incremental LDA outside of continual learning.

```pycon
>>> from capymoa.ocl.strategy import SLDA
>>> from capymoa.ocl.datasets import TinySplitMNIST
>>> from capymoa.ocl.evaluation import ocl_train_eval_loop
>>> scenario = TinySplitMNIST()
>>> learner = SLDA(scenario.schema)
>>> results = ocl_train_eval_loop(
...     learner,
...     scenario.train_loaders(32),
...     scenario.test_loaders(32),
... )
>>> print(f"{results.accuracy_final*100:.1f}%")
75.5%
```

* <a id='f0'>**[1]**</a> [Hayes, T. L., & Kanan, C. (2020). Lifelong Machine Learning with Deep Streaming Linear Discriminant Analysis. CLVision Workshop at CVPR 2020, 1–15.](https://arxiv.org/abs/1909.01520)
* <a id='f1'>**[2]**</a> [Ghassabeh, Y. A., Rudzicz, F., & Moghaddam, H. A. (2015). Fast incremental LDA feature extraction. Pattern Recognition, 48(6), 1999-2012.](https://doi.org/10.1016/j.patcog.2014.12.012)
* <a id='f2'>**[3]**</a> [Linear discriminant analysis. (2025). In Wikipedia.](https://en.wikipedia.org/w/index.php?title=Linear_discriminant_analysis&oldid=1295914652#Incremental_LDA)

#### \_\_init_\_(schema: [Schema](capymoa.stream.Schema.md#capymoa.stream.Schema), pre_processor: [Module](https://docs.pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module) | [None](https://docs.python.org/3/builtins/constants.html#None) = None, num_features: [int](https://docs.python.org/3/builtins/functions.html#int) | [None](https://docs.python.org/3/builtins/constants.html#None) = None, ridge: [float](https://docs.python.org/3/builtins/functions.html#float) = 1e-6, device: [device](https://docs.pytorch.org/docs/stable/tensor_attributes.html#torch.device) | [str](https://docs.python.org/3/builtins/stdtypes.html#str) | [None](https://docs.python.org/3/builtins/constants.html#None) = None)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/ocl/strategy/_slda.py#L73)

Initialize a SLDA classifier.

* **Parameters:**
  * **schema** – Describes the shape and type of the data.
  * **pre_processor** – A pre-processing module to apply to the input
    data, defaults to an identity module.
  * **num_features** – Number of features once pre-processed, defaults to
    the number of attributes in the schema.
  * **ridge** – Ridge regularization term to avoid singular covariance matrix,
    defaults to 1e-6.
  * **device** – Device to run the model on, defaults to CPU.

#### batch_predict(x: [Tensor](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor)) → [Tensor](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/base/_batch_classifier.py#L115)

Predict the labels for a batch of instances.

* **Parameters:**
  **x** – Batch of [`x_dtype`](#capymoa.ocl.strategy.SLDA.x_dtype) valued feature vectors
  `(batch_size, num_features)`
* **Returns:**
  Predicted batch of [`y_dtype`](#capymoa.ocl.strategy.SLDA.y_dtype) valued labels
  `(batch_size,)`.

#### batch_predict_proba(x: [Tensor](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor)) → [Tensor](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/ocl/strategy/_slda.py#L127)

Predict the probabilities of the classes for a batch of instances.

* **Parameters:**
  **x** – Batch of [`x_dtype`](#capymoa.ocl.strategy.SLDA.x_dtype) valued feature vectors
  `(batch_size, num_features)`
* **Returns:**
  Batch of [`x_dtype`](#capymoa.ocl.strategy.SLDA.x_dtype) valued predicted probabilities
  `(batch_size, num_classes)`.

#### batch_train(x: [Tensor](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor), y: [Tensor](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor)) → [None](https://docs.python.org/3/builtins/constants.html#None)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/ocl/strategy/_slda.py#L112)

Train with a batch of instances.

* **Parameters:**
  * **x** – Batch of [`x_dtype`](#capymoa.ocl.strategy.SLDA.x_dtype) valued feature vectors
    `(batch_size, num_features)`
  * **y** – Batch of [`y_dtype`](#capymoa.ocl.strategy.SLDA.y_dtype) valued labels `(batch_size,)`.

#### *classmethod* from_params(schema: [Any](https://docs.python.org/3/library/typing.html#typing.Any) = None, params: [dict](https://docs.python.org/3/builtins/stdtypes.html#dict)[[str](https://docs.python.org/3/builtins/stdtypes.html#str), [Any](https://docs.python.org/3/library/typing.html#typing.Any)] | [None](https://docs.python.org/3/builtins/constants.html#None) = None, random_seed: [int](https://docs.python.org/3/builtins/functions.html#int) = 1) → [Any](https://docs.python.org/3/library/typing.html#typing.Any)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/base/_learner_params.py#L170)

Construct an instance from parameters produced by `get_params`.

#### get_params() → [dict](https://docs.python.org/3/builtins/stdtypes.html#dict)[[str](https://docs.python.org/3/builtins/stdtypes.html#str), [Any](https://docs.python.org/3/library/typing.html#typing.Any)][[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/base/_learner_params.py#L163)

Return the hyper-parameters captured from the constructor.

#### predict(instance: [Instance](capymoa.core.Instance.md#capymoa.core.Instance)) → [int](https://docs.python.org/3/builtins/functions.html#int) | [None](https://docs.python.org/3/builtins/constants.html#None)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/base/_classifier.py#L56)

Predict the label of an instance.

The base implementation calls [`predict_proba()`](#capymoa.ocl.strategy.SLDA.predict_proba) and returns the
label with the highest probability.

* **Parameters:**
  **instance** – The instance to predict the label for.
* **Returns:**
  The predicted label or `None` if the classifier is unable
  to make a prediction.

#### predict_proba(instance: [Instance](capymoa.core.Instance.md#capymoa.core.Instance)) → [ndarray](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray)[[tuple](https://docs.python.org/3/builtins/stdtypes.html#tuple)[[Any](https://docs.python.org/3/library/typing.html#typing.Any), ...], [dtype](https://numpy.org/doc/stable/reference/generated/numpy.dtype.html#numpy.dtype)[float64]] | [None](https://docs.python.org/3/builtins/constants.html#None)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/base/_batch_classifier.py#L134)

Calls [`batch_predict_proba()`](#capymoa.ocl.strategy.SLDA.batch_predict_proba) with a batch of size 1.

#### train(instance: [LabeledInstance](capymoa.core.LabeledInstance.md#capymoa.core.LabeledInstance)) → [None](https://docs.python.org/3/builtins/constants.html#None)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/base/_batch_classifier.py#L125)

Calls [`batch_train()`](#capymoa.ocl.strategy.SLDA.batch_train) with a batch of size 1.

#### device *: [torch.device](https://docs.pytorch.org/docs/stable/tensor_attributes.html#torch.device)* *= device(type='cpu')*

Device on which the batch will be processed.

#### random_seed *: [int](https://docs.python.org/3/builtins/functions.html#int)*

The random seed for reproducibility.

When implementing a classifier ensure random number generators are seeded.

#### schema *: [Schema](capymoa.stream.Schema.md#capymoa.stream.Schema)*

The schema representing the instances.

#### x_dtype *: [torch.dtype](https://docs.pytorch.org/docs/stable/tensor_attributes.html#torch.dtype)* *= torch.float32*

Data type for the input features.

#### y_dtype *: [torch.dtype](https://docs.pytorch.org/docs/stable/tensor_attributes.html#torch.dtype)* *= torch.int64*

Data type for the target value/labels.
