Dataloader

class StockDataloader(X: Union[ndarray, DataFrame], y: Optional[Union[ndarray, DataFrame]] = None, model_type: Optional[str] = None, task: Optional[str] = None)[source]

Bases: object

Stock DataLoader.

This class is responsible for managing the loading of stock data for machine learning models. It supports different types of models (RNN, FFNN, CNN) and tasks (regression, classification). The data is loaded in batches, and the training data can be split into a training set and a validation set.

X_train

The normalized training features.

Type:

torch.Tensor

y_train

The normalized training targets.

Type:

torch.Tensor

model_type

The type of model (RNN, FFNN, CNN). It is set during initialization.

Type:

str

task

The type of task (regression or classification). It is set during initialization.

Type:

str

get_loader(X: Optional[Union[ndarray, DataFrame]] = None, y: Optional[Union[ndarray, DataFrame]] = None, mode: str = 'train')[source]

Returns the DataLoader for a given mode.

For the training and validation modes, the data is split based on the validation size. For the testing mode, the features and targets are transformed and then loaded.

Parameters:
  • X (Union[np.ndarray, pd.core.frame.DataFrame], optional) – The features of the test data. Default is None.

  • y (Union[np.ndarray, pd.core.frame.DataFrame], optional) – The targets of the test data. Default is None.

  • mode (str, optional) – The mode (‘train’, ‘val’, ‘test’). Default is ‘train’.

Returns:

The DataLoader instance for the given mode.

Return type:

DataLoader

Raises:

ValueError – If the mode or task is invalid.

inverse_transform_output(y_pred)[source]

Applies the inverse transform to the output.

This method is used to denormalize the output of the model.

Parameters:

y_pred (torch.Tensor) – The output of the model.

Returns:

The denormalized output.

Return type:

torch.Tensor

class StockScaler[source]

Bases: object

Stock Scaler.

This class is responsible for normalizing and denormalizing the features and targets of stock data. It supports Z-score, Min-Max, and Robust scaling.

scaler_type

The type of scaler to use. It is set during initialization.

Type:

str

X_normalizer

The normalizer for the features. It is set during initialization.

Type:

TransformMixin

y_normalizer

The normalizer for the targets. It is set during initialization.

Type:

TransformMixin

fit_transform(X_train: Union[ndarray, DataFrame], y_train: Optional[Union[ndarray, DataFrame]] = None, task: Optional[str] = None) Tuple[Tensor, Optional[Tensor]][source]

Fits the normalizers to the training data and then transforms the data.

The task parameter determines the type of task (regression or classification) and adjusts the normalization process accordingly.

Parameters:
  • X_train (Union[np.ndarray, pd.core.frame.DataFrame]) – The training features to fit the normalizer to and then transform.

  • y_train (Union[np.ndarray, pd.core.frame.DataFrame], optional) – The training targets to fit the normalizer to and then transform. Default is None.

  • task (str, optional) – The type of task (‘regression’ or ‘classification’). Default is None.

Returns:

The transformed training features and targets. The targets are None if y_train is None.

Return type:

Tuple[torch.Tensor, Optional[torch.Tensor]]

Raises:

RuntimeError – If the fit method has not been called before the transform method.

inverse_transform(y_pred: Tensor)[source]

Applies the inverse target normalizer to the predictions.

This method transforms the predictions by multiplying by the standard deviation and adding the mean. The fit and transform methods must be called before this method.

Parameters:

y_pred (torch.Tensor) – The predictions to apply the inverse normalizer to.

Returns:

The rescaled predictions.

Return type:

torch.Tensor

Raises:

RuntimeError – If the fit method has not been called before this method.

transform(X_test: Tensor)[source]

Applies the feature normalizer to the test data.

Parameters:

X_test (torch.Tensor) – The test features to apply the normalizer to.

Returns:

The transformed test features.

Return type:

torch.Tensor

Raises:

RuntimeError – If the fit method has not been called before this method.

Datasets

BaseStockDataset

class BaseStockDataset(X: Tensor, y: Optional[Tensor] = None, task: Optional[str] = None)[source]

Bases: Dataset

Base class for Stock Dataset.

This class represents a base dataset for stock data. It extends from PyTorch’s Dataset class.

Parameters:
  • X (torch.Tensor) – The input data tensor.

  • y (torch.Tensor, optional) – The target data tensor. Default is None.

  • task (str, optional) – The type of task. Could be either ‘regression’ or ‘classification’. Default is None.

X

The input data tensor.

Type:

torch.Tensor

y

The target data tensor.

Type:

torch.Tensor

task

The type of task. Could be either ‘regression’ or ‘classification’.

Type:

str

property input_size

The size of the input data.

Returns:

The number of features in the input data.

Return type:

int

property output_size

The size of the output data.

If the task is ‘regression’, it returns the number of targets in the output data. If the task is ‘classification’, it returns the number of unique classes in the output data.

Returns:

The size of the output data.

Return type:

int

StockDatasetRNN

class StockDatasetRNN(X: Tensor, y: Optional[Tensor] = None, task: Optional[str] = None)[source]

Bases: BaseStockDataset

Class for Stock Dataset used for Recurrent Neural Networks (RNNs).

This class extends the BaseStockDataset class and implements the __getitem__ method for RNNs.

Inherits all attributes from the BaseStockDataset class.

StockDatasetCNN

class StockDatasetCNN(X: Union[ndarray, DataFrame], y: Optional[Union[ndarray, DataFrame]] = None, task: str = 'regression')[source]

Bases: BaseStockDataset

Class for Stock Dataset used for Convolutional Neural Networks (CNNs).

This class extends the BaseStockDataset class and implements the __getitem__ method for CNNs.

Inherits all attributes from the BaseStockDataset class.
property input_size

The size of the input data for CNN models.

Returns:

The number of features in the input data.

Return type:

int

StockDatasetFFNN

class StockDatasetFFNN(X: Tensor, y: Optional[Tensor] = None, task: Optional[str] = None)[source]

Bases: BaseStockDataset

Class for Stock Dataset used for Feedforward Neural Networks (FFNNs).

This class extends the BaseStockDataset class and implements the __getitem__ method for FFNNs.

Inherits all attributes from the BaseStockDataset class.