Model API Cheatsheet

pypots.base

The base (abstract) classes for models in PyPOTS.

class pypots.base.BaseModel(device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: ABC

The base model class for all model implementations.

Parameters:
  • device (Union[str, device, list, None]) – The device for the model to run on. It can be a string, a torch.device object, or a list of them. If not given, will try to use CUDA devices first (will use the default CUDA device if there are multiple), then CPUs, considering CUDA and CPU are so far the main devices for people to train ML models. If given a list of devices, e.g. [‘cuda:0’, ‘cuda:1’], or [torch.device(‘cuda:0’), torch.device(‘cuda:1’)] , the model will be parallely trained on the multiple devices (so far only support parallel training on CUDA devices). Other devices like Google TPU and Apple Silicon accelerator MPS may be added in the future.

  • saving_path (Optional[str]) – The path for automatically saving model checkpoints and tensorboard files (i.e. loss values recorded during training into a tensorboard file). Will not save if not given.

  • model_saving_strategy (Optional[str]) – The strategy to save model checkpoints. It has to be one of [None, “best”, “better”, “all”]. No model will be saved when it is set as None. The “best” strategy will only automatically save the best model after the training finished. The “better” strategy will automatically save the model during training whenever the model performs better than in previous epochs. The “all” strategy will save every model after each epoch training.

  • verbose (bool) – Whether to print out the training logs during the training process.

Attributes:
  • model (object, default = None) – The underlying model or algorithm to finish the task.

  • summary_writer (None or torch.utils.tensorboard.SummaryWriter, default = None,) – The event writer to save training logs. Default as None. It only works when parameter tb_file_saving_path is given, otherwise the training events won’t be saved.

    It is designed as being set up while initializing the model because it’s created to 1). help visualize the model’s training procedure (during training not after) and 2). assist users to tune the model’s hype-parameters. If only setting it up after training with a function like setter(), it cannot achieve the 1st purpose.

save(saving_path, overwrite=False)[source]

Save the model with current parameters to a disk file.

A .pypots extension will be appended to the filename if it does not already have one. Please note that such an extension is not necessary, but to indicate the saved model is from PyPOTS framework so people can distinguish.

Parameters:
  • saving_path (str) – The given path to save the model. The directory will be created if it does not exist.

  • overwrite (bool) – Whether to overwrite the model file if the path already exists.

Return type:

None

load(path)[source]

Load the saved model from a disk file.

Parameters:

path (str) – The local path to a disk file saving the trained model.

Return type:

None

Notes

If the training environment and the deploying/test environment use the same type of device (GPU/CPU), you can load the model directly with torch.load(model_path).

abstract fit(train_set, val_set=None, file_type='hdf5')[source]

Train the classifier on the given data.

Parameters:
  • train_set (Union[dict, str]) – The dataset for model training, should be a dictionary including keys as ‘X’, or a path string locating a data file supported by PyPOTS (e.g. h5 file). If it is a dict, X should be array-like of shape [n_samples, sequence length (n_steps), n_features], which is time-series data for training, can contain missing values, and y should be array-like of shape [n_samples], which is classification labels of X. If it is a path string, the path should point to a data file, e.g. a h5 file, which contains key-value pairs like a dict, and it has to include keys as ‘X’ and ‘y’.

  • val_set (Union[dict, str, None]) – The dataset for model validating, should be a dictionary including keys as ‘X’, or a path string locating a data file supported by PyPOTS (e.g. h5 file). If it is a dict, X should be array-like of shape [n_samples, sequence length (n_steps), n_features], which is time-series data for validating, can contain missing values, and y should be array-like of shape [n_samples], which is classification labels of X. If it is a path string, the path should point to a data file, e.g. a h5 file, which contains key-value pairs like a dict, and it has to include keys as ‘X’ and ‘y’.

  • file_type (str) – The type of the given file if train_set and val_set are path strings.

Return type:

None

abstract predict(test_set, file_type='hdf5')[source]

Make predictions for the input data with the trained model.

Parameters:
  • test_set (Union[dict, str]) – The dataset for model validating, should be a dictionary including keys as ‘X’, or a path string locating a data file supported by PyPOTS (e.g. h5 file). If it is a dict, X should be array-like of shape [n_samples, sequence length (n_steps), n_features], which is time-series data for validating, can contain missing values, and y should be array-like of shape [n_samples], which is classification labels of X. If it is a path string, the path should point to a data file, e.g. a h5 file, which contains key-value pairs like a dict, and it has to include keys as ‘X’ and ‘y’.

  • file_type (str) – The type of the given file if test_set is a path string.

Returns:

Prediction results in a Python Dictionary for the given samples. It should be a dictionary including keys as ‘imputation’, ‘classification’, ‘clustering’, and ‘forecasting’. For sure, only the keys that relevant tasks are supported by the model will be returned.

Return type:

result_dict

class pypots.base.BaseNNModel(batch_size, epochs, patience=None, num_workers=0, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseModel

The abstract class for all neural-network models.

Parameters:
  • batch_size (int) – Size of the batch input into the model for one step.

  • epochs (int) – Training epochs, i.e. the maximum rounds of the model to be trained with.

  • patience (Optional[int]) – Number of epochs the training procedure will keep if loss doesn’t decrease. Once exceeding the number, the training will stop. Must be smaller than or equal to the value of epochs.

  • num_workers (int) – The number of subprocesses to use for data loading. 0 means data loading will be in the main process, i.e. there won’t be subprocesses.

  • device (Union[str, device, list, None]) – The device for the model to run on. It can be a string, a torch.device object, or a list of them. If not given, will try to use CUDA devices first (will use the default CUDA device if there are multiple), then CPUs, considering CUDA and CPU are so far the main devices for people to train ML models. If given a list of devices, e.g. [‘cuda:0’, ‘cuda:1’], or [torch.device(‘cuda:0’), torch.device(‘cuda:1’)] , the model will be parallely trained on the multiple devices (so far only support parallel training on CUDA devices). Other devices like Google TPU and Apple Silicon accelerator MPS may be added in the future.

  • saving_path (Optional[str]) – The path for automatically saving model checkpoints and tensorboard files (i.e. loss values recorded during training into a tensorboard file). Will not save if not given.

  • model_saving_strategy (Optional[str]) – The strategy to save model checkpoints. It has to be one of [None, “best”, “better”, “all”]. No model will be saved when it is set as None. The “best” strategy will only automatically save the best model after the training finished. The “better” strategy will automatically save the model during training whenever the model performs better than in previous epochs. The “all” strategy will save every model after each epoch training.

  • verbose (bool) – Whether to print out the training logs during the training process.

Attributes:
  • best_model_dict (dict, default = None,) – A dictionary contains the trained model that achieves the best performance according to the loss defined, i.e. the lowest loss.

  • best_loss (float, default = inf,) – The criteria to judge whether the model’s performance is the best so far. Usually the lower, the better.

  • best_epoch (int, default = -1,) – The epoch number when the best loss is got.

Notes

Optimizers are necessary for training deep-learning neural networks, but we don’t put a parameter optimizer here because some models (e.g. GANs) need more than one optimizer (e.g. one for generator, one for discriminator), and optimizer is ambiguous for them. Therefore, we leave optimizers as parameters for concrete model implementations, and you can pass any number of optimizers to your model when implementing it, pypots.clustering.crli.CRLI for example.

abstract fit(train_set, val_set=None, file_type='hdf5')[source]

Train the classifier on the given data.

Parameters:
  • train_set (Union[dict, str]) – The dataset for model training, should be a dictionary including keys as ‘X’, or a path string locating a data file supported by PyPOTS (e.g. h5 file). If it is a dict, X should be array-like of shape [n_samples, sequence length (n_steps), n_features], which is time-series data for training, can contain missing values, and y should be array-like of shape [n_samples], which is classification labels of X. If it is a path string, the path should point to a data file, e.g. a h5 file, which contains key-value pairs like a dict, and it has to include keys as ‘X’ and ‘y’.

  • val_set (Union[dict, str, None]) – The dataset for model validating, should be a dictionary including keys as ‘X’, or a path string locating a data file supported by PyPOTS (e.g. h5 file). If it is a dict, X should be array-like of shape [n_samples, sequence length (n_steps), n_features], which is time-series data for validating, can contain missing values, and y should be array-like of shape [n_samples], which is classification labels of X. If it is a path string, the path should point to a data file, e.g. a h5 file, which contains key-value pairs like a dict, and it has to include keys as ‘X’ and ‘y’.

  • file_type (str) – The type of the given file if train_set and val_set are path strings.

Return type:

None

abstract predict(test_set, file_type='hdf5')[source]

Make predictions for the input data with the trained model.

Parameters:
  • test_set (Union[dict, str]) – The dataset for model validating, should be a dictionary including keys as ‘X’, or a path string locating a data file supported by PyPOTS (e.g. h5 file). If it is a dict, X should be array-like of shape [n_samples, sequence length (n_steps), n_features], which is time-series data for validating, can contain missing values, and y should be array-like of shape [n_samples], which is classification labels of X. If it is a path string, the path should point to a data file, e.g. a h5 file, which contains key-value pairs like a dict, and it has to include keys as ‘X’ and ‘y’.

  • file_type (str) – The type of the given file if test_set is a path string.

Returns:

Prediction results in a Python Dictionary for the given samples. It should be a dictionary including keys as ‘imputation’, ‘classification’, ‘clustering’, and ‘forecasting’. For sure, only the keys that relevant tasks are supported by the model will be returned.

Return type:

result_dict

load(path)

Load the saved model from a disk file.

Parameters:

path (str) – The local path to a disk file saving the trained model.

Return type:

None

Notes

If the training environment and the deploying/test environment use the same type of device (GPU/CPU), you can load the model directly with torch.load(model_path).

save(saving_path, overwrite=False)

Save the model with current parameters to a disk file.

A .pypots extension will be appended to the filename if it does not already have one. Please note that such an extension is not necessary, but to indicate the saved model is from PyPOTS framework so people can distinguish.

Parameters:
  • saving_path (str) – The given path to save the model. The directory will be created if it does not exist.

  • overwrite (bool) – Whether to overwrite the model file if the path already exists.

Return type:

None

pypots.imputation.base

The base class for PyPOTS imputation models.

class pypots.imputation.base.BaseImputer(device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseModel

Abstract class for all imputation models.

Parameters:
  • device (Union[str, device, list, None]) – The device for the model to run on. It can be a string, a torch.device object, or a list of them. If not given, will try to use CUDA devices first (will use the default CUDA device if there are multiple), then CPUs, considering CUDA and CPU are so far the main devices for people to train ML models. If given a list of devices, e.g. [‘cuda:0’, ‘cuda:1’], or [torch.device(‘cuda:0’), torch.device(‘cuda:1’)] , the model will be parallely trained on the multiple devices (so far only support parallel training on CUDA devices). Other devices like Google TPU and Apple Silicon accelerator MPS may be added in the future.

  • saving_path (Optional[str]) – The path for automatically saving model checkpoints and tensorboard files (i.e. loss values recorded during training into a tensorboard file). Will not save if not given.

  • model_saving_strategy (Optional[str]) – The strategy to save model checkpoints. It has to be one of [None, “best”, “better”, “all”]. No model will be saved when it is set as None. The “best” strategy will only automatically save the best model after the training finished. The “better” strategy will automatically save the model during training whenever the model performs better than in previous epochs. The “all” strategy will save every model after each epoch training.

  • verbose (bool) – Whether to print out the training logs during the training process.

abstract fit(train_set, val_set=None, file_type='hdf5')[source]

Train the imputer on the given data.

Parameters:
  • train_set (Union[dict, str]) – The dataset for model training, should be a dictionary including the key ‘X’, or a path string locating a data file. If it is a dict, X should be array-like of shape [n_samples, sequence length (n_steps), n_features], which is time-series data for training, can contain missing values. If it is a path string, the path should point to a data file, e.g. a h5 file, which contains key-value pairs like a dict, and it has to include the key ‘X’.

  • val_set (Union[dict, str, None]) – The dataset for model validating, should be a dictionary including the key ‘X’, or a path string locating a data file. If it is a dict, X should be array-like of shape [n_samples, sequence length (n_steps), n_features], which is time-series data for validating, can contain missing values. If it is a path string, the path should point to a data file, e.g. a h5 file, which contains key-value pairs like a dict, and it has to include the key ‘X’.

  • file_type (str) – The type of the given file if train_set and val_set are path strings.

Return type:

None

abstract predict(test_set, file_type='hdf5')[source]

Make predictions for the input data with the trained model.

Parameters:
  • test_set (Union[dict, str]) – The dataset for model validating, should be a dictionary including keys as ‘X’, or a path string locating a data file supported by PyPOTS (e.g. h5 file). If it is a dict, X should be array-like of shape [n_samples, sequence length (n_steps), n_features], which is time-series data for validating, can contain missing values, and y should be array-like of shape [n_samples], which is classification labels of X. If it is a path string, the path should point to a data file, e.g. a h5 file, which contains key-value pairs like a dict, and it has to include keys as ‘X’ and ‘y’.

  • file_type (str) – The type of the given file if test_set is a path string.

Returns:

Prediction results in a Python Dictionary for the given samples. It should be a dictionary including keys as ‘imputation’, ‘classification’, ‘clustering’, and ‘forecasting’. For sure, only the keys that relevant tasks are supported by the model will be returned.

Return type:

result_dict

abstract impute(test_set, file_type='hdf5')[source]

Impute missing values in the given data with the trained model.

Parameters:
  • test_set (Union[dict, str]) – The data samples for testing, should be array-like of shape [n_samples, sequence length (n_steps), n_features], or a path string locating a data file, e.g. h5 file.

  • file_type (str) – The type of the given file if X is a path string.

Returns:

Imputed data.

Return type:

array-like, shape [n_samples, sequence length (n_steps), n_features],

load(path)

Load the saved model from a disk file.

Parameters:

path (str) – The local path to a disk file saving the trained model.

Return type:

None

Notes

If the training environment and the deploying/test environment use the same type of device (GPU/CPU), you can load the model directly with torch.load(model_path).

save(saving_path, overwrite=False)

Save the model with current parameters to a disk file.

A .pypots extension will be appended to the filename if it does not already have one. Please note that such an extension is not necessary, but to indicate the saved model is from PyPOTS framework so people can distinguish.

Parameters:
  • saving_path (str) – The given path to save the model. The directory will be created if it does not exist.

  • overwrite (bool) – Whether to overwrite the model file if the path already exists.

Return type:

None

class pypots.imputation.base.BaseNNImputer(batch_size, epochs, patience=None, num_workers=0, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseNNModel

The abstract class for all neural-network imputation models in PyPOTS.

Parameters:
  • batch_size (int) – Size of the batch input into the model for one step.

  • epochs (int) – Training epochs, i.e. the maximum rounds of the model to be trained with.

  • patience (Optional[int]) – Number of epochs the training procedure will keep if loss doesn’t decrease. Once exceeding the number, the training will stop. Must be smaller than or equal to the value of epochs.

  • num_workers (int) – The number of subprocesses to use for data loading. 0 means data loading will be in the main process, i.e. there won’t be subprocesses.

  • device (Union[str, device, list, None]) – The device for the model to run on. It can be a string, a torch.device object, or a list of them. If not given, will try to use CUDA devices first (will use the default CUDA device if there are multiple), then CPUs, considering CUDA and CPU are so far the main devices for people to train ML models. If given a list of devices, e.g. [‘cuda:0’, ‘cuda:1’], or [torch.device(‘cuda:0’), torch.device(‘cuda:1’)] , the model will be parallely trained on the multiple devices (so far only support parallel training on CUDA devices). Other devices like Google TPU and Apple Silicon accelerator MPS may be added in the future.

  • saving_path (Optional[str]) – The path for automatically saving model checkpoints and tensorboard files (i.e. loss values recorded during training into a tensorboard file). Will not save if not given.

  • model_saving_strategy (Optional[str]) – The strategy to save model checkpoints. It has to be one of [None, “best”, “better”, “all”]. No model will be saved when it is set as None. The “best” strategy will only automatically save the best model after the training finished. The “better” strategy will automatically save the model during training whenever the model performs better than in previous epochs. The “all” strategy will save every model after each epoch training.

  • verbose (bool) – Whether to print out the training logs during the training process.

Notes

Optimizers are necessary for training deep-learning neural networks, but we don’t put a parameter optimizer here because some models (e.g. GANs) need more than one optimizer (e.g. one for generator, one for discriminator), and optimizer is ambiguous for them. Therefore, we leave optimizers as parameters for concrete model implementations, and you can pass any number of optimizers to your model when implementing it, pypots.clustering.crli.CRLI for example.

abstract fit(train_set, val_set=None, file_type='hdf5')[source]

Train the imputer on the given data.

Parameters:
  • train_set (Union[dict, str]) – The dataset for model training, should be a dictionary including the key ‘X’, or a path string locating a data file. If it is a dict, X should be array-like of shape [n_samples, sequence length (n_steps), n_features], which is time-series data for training, can contain missing values. If it is a path string, the path should point to a data file, e.g. a h5 file, which contains key-value pairs like a dict, and it has to include the key ‘X’.

  • val_set (Union[dict, str, None]) – The dataset for model validating, should be a dictionary including the key ‘X’, or a path string locating a data file. If it is a dict, X should be array-like of shape [n_samples, sequence length (n_steps), n_features], which is time-series data for validating, can contain missing values. If it is a path string, the path should point to a data file, e.g. a h5 file, which contains key-value pairs like a dict, and it has to include the key ‘X’.

  • file_type (str) – The type of the given file if train_set and val_set are path strings.

Return type:

None

abstract predict(test_set, file_type='hdf5')[source]

Make predictions for the input data with the trained model.

Parameters:
  • test_set (Union[dict, str]) – The dataset for model validating, should be a dictionary including keys as ‘X’, or a path string locating a data file supported by PyPOTS (e.g. h5 file). If it is a dict, X should be array-like of shape [n_samples, sequence length (n_steps), n_features], which is time-series data for validating, can contain missing values, and y should be array-like of shape [n_samples], which is classification labels of X. If it is a path string, the path should point to a data file, e.g. a h5 file, which contains key-value pairs like a dict, and it has to include keys as ‘X’ and ‘y’.

  • file_type (str) – The type of the given file if test_set is a path string.

Returns:

Prediction results in a Python Dictionary for the given samples. It should be a dictionary including keys as ‘imputation’, ‘classification’, ‘clustering’, and ‘forecasting’. For sure, only the keys that relevant tasks are supported by the model will be returned.

Return type:

result_dict

abstract impute(test_set, file_type='hdf5')[source]

Impute missing values in the given data with the trained model.

Parameters:
  • test_set (Union[dict, str]) – The data samples for testing, should be array-like of shape [n_samples, sequence length (n_steps), n_features], or a path string locating a data file, e.g. h5 file.

  • file_type (str) – The type of the given file if X is a path string.

Returns:

Imputed data.

Return type:

array-like, shape [n_samples, sequence length (n_steps), n_features],

load(path)

Load the saved model from a disk file.

Parameters:

path (str) – The local path to a disk file saving the trained model.

Return type:

None

Notes

If the training environment and the deploying/test environment use the same type of device (GPU/CPU), you can load the model directly with torch.load(model_path).

save(saving_path, overwrite=False)

Save the model with current parameters to a disk file.

A .pypots extension will be appended to the filename if it does not already have one. Please note that such an extension is not necessary, but to indicate the saved model is from PyPOTS framework so people can distinguish.

Parameters:
  • saving_path (str) – The given path to save the model. The directory will be created if it does not exist.

  • overwrite (bool) – Whether to overwrite the model file if the path already exists.

Return type:

None

pypots.classification.base

The base classes for PyPOTS classification models.

class pypots.classification.base.BaseClassifier(n_classes, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseModel

The abstract class for all PyPOTS classification models.

Parameters:
  • n_classes (int) – The number of classes in the classification task.

  • device (Union[str, device, list, None]) – The device for the model to run on. It can be a string, a torch.device object, or a list of them. If not given, will try to use CUDA devices first (will use the default CUDA device if there are multiple), then CPUs, considering CUDA and CPU are so far the main devices for people to train ML models. If given a list of devices, e.g. [‘cuda:0’, ‘cuda:1’], or [torch.device(‘cuda:0’), torch.device(‘cuda:1’)] , the model will be parallely trained on the multiple devices (so far only support parallel training on CUDA devices). Other devices like Google TPU and Apple Silicon accelerator MPS may be added in the future.

  • saving_path (Optional[str]) – The path for automatically saving model checkpoints and tensorboard files (i.e. loss values recorded during training into a tensorboard file). Will not save if not given.

  • model_saving_strategy (Optional[str]) – The strategy to save model checkpoints. It has to be one of [None, “best”, “better”, “all”]. No model will be saved when it is set as None. The “best” strategy will only automatically save the best model after the training finished. The “better” strategy will automatically save the model during training whenever the model performs better than in previous epochs. The “all” strategy will save every model after each epoch training.

  • verbose (bool) – Whether to print out the training logs during the training process.

abstract fit(train_set, val_set=None, file_type='hdf5')[source]

Train the classifier on the given data.

Parameters:
  • train_set (Union[dict, str]) – The dataset for model training, should be a dictionary including keys as ‘X’ and ‘y’, or a path string locating a data file. If it is a dict, X should be array-like of shape [n_samples, sequence length (n_steps), n_features], which is time-series data for training, can contain missing values, and y should be array-like of shape [n_samples], which is classification labels of X. If it is a path string, the path should point to a data file, e.g. a h5 file, which contains key-value pairs like a dict, and it has to include keys as ‘X’ and ‘y’.

  • val_set (Union[dict, str, None]) – The dataset for model validating, should be a dictionary including keys as ‘X’ and ‘y’, or a path string locating a data file. If it is a dict, X should be array-like of shape [n_samples, sequence length (n_steps), n_features], which is time-series data for validating, can contain missing values, and y should be array-like of shape [n_samples], which is classification labels of X. If it is a path string, the path should point to a data file, e.g. a h5 file, which contains key-value pairs like a dict, and it has to include keys as ‘X’ and ‘y’.

  • file_type (str) – The type of the given file if train_set and val_set are path strings.

Return type:

None

abstract predict(test_set, file_type='hdf5')[source]

Make predictions for the input data with the trained model.

Parameters:
  • test_set (Union[dict, str]) – The dataset for model validating, should be a dictionary including keys as ‘X’, or a path string locating a data file supported by PyPOTS (e.g. h5 file). If it is a dict, X should be array-like of shape [n_samples, sequence length (n_steps), n_features], which is time-series data for validating, can contain missing values, and y should be array-like of shape [n_samples], which is classification labels of X. If it is a path string, the path should point to a data file, e.g. a h5 file, which contains key-value pairs like a dict, and it has to include keys as ‘X’ and ‘y’.

  • file_type (str) – The type of the given file if test_set is a path string.

Returns:

Prediction results in a Python Dictionary for the given samples. It should be a dictionary including keys as ‘imputation’, ‘classification’, ‘clustering’, and ‘forecasting’. For sure, only the keys that relevant tasks are supported by the model will be returned.

Return type:

result_dict

abstract classify(test_set, file_type='hdf5')[source]

Classify the input data with the trained model.

Parameters:
  • test_set (Union[dict, str]) – The data samples for testing, should be array-like of shape [n_samples, sequence length (n_steps), n_features], or a path string locating a data file, e.g. h5 file.

  • file_type (str) – The type of the given file if X is a path string.

Returns:

Classification results of the given samples.

Return type:

array-like, shape [n_samples],

load(path)

Load the saved model from a disk file.

Parameters:

path (str) – The local path to a disk file saving the trained model.

Return type:

None

Notes

If the training environment and the deploying/test environment use the same type of device (GPU/CPU), you can load the model directly with torch.load(model_path).

save(saving_path, overwrite=False)

Save the model with current parameters to a disk file.

A .pypots extension will be appended to the filename if it does not already have one. Please note that such an extension is not necessary, but to indicate the saved model is from PyPOTS framework so people can distinguish.

Parameters:
  • saving_path (str) – The given path to save the model. The directory will be created if it does not exist.

  • overwrite (bool) – Whether to overwrite the model file if the path already exists.

Return type:

None

class pypots.classification.base.BaseNNClassifier(n_classes, batch_size, epochs, patience=None, num_workers=0, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseNNModel

The abstract class for all neural-network classification models in PyPOTS.

Parameters:
  • n_classes (int) – The number of classes in the classification task.

  • batch_size (int) – Size of the batch input into the model for one step.

  • epochs (int) – Training epochs, i.e. the maximum rounds of the model to be trained with.

  • patience (Optional[int]) – Number of epochs the training procedure will keep if loss doesn’t decrease. Once exceeding the number, the training will stop. Must be smaller than or equal to the value of epochs.

  • num_workers (int) – The number of subprocesses to use for data loading. 0 means data loading will be in the main process, i.e. there won’t be subprocesses.

  • device (Union[str, device, list, None]) – The device for the model to run on. It can be a string, a torch.device object, or a list of them. If not given, will try to use CUDA devices first (will use the default CUDA device if there are multiple), then CPUs, considering CUDA and CPU are so far the main devices for people to train ML models. If given a list of devices, e.g. [‘cuda:0’, ‘cuda:1’], or [torch.device(‘cuda:0’), torch.device(‘cuda:1’)] , the model will be parallely trained on the multiple devices (so far only support parallel training on CUDA devices). Other devices like Google TPU and Apple Silicon accelerator MPS may be added in the future.

  • saving_path (Optional[str]) – The path for automatically saving model checkpoints and tensorboard files (i.e. loss values recorded during training into a tensorboard file). Will not save if not given.

  • model_saving_strategy (Optional[str]) – The strategy to save model checkpoints. It has to be one of [None, “best”, “better”, “all”]. No model will be saved when it is set as None. The “best” strategy will only automatically save the best model after the training finished. The “better” strategy will automatically save the model during training whenever the model performs better than in previous epochs. The “all” strategy will save every model after each epoch training.

  • verbose (bool) – Whether to print out the training logs during the training process.

Notes

Optimizers are necessary for training deep-learning neural networks, but we don’t put a parameter optimizer here because some models (e.g. GANs) need more than one optimizer (e.g. one for generator, one for discriminator), and optimizer is ambiguous for them. Therefore, we leave optimizers as parameters for concrete model implementations, and you can pass any number of optimizers to your model when implementing it, pypots.clustering.crli.CRLI for example.

abstract fit(train_set, val_set=None, file_type='hdf5')[source]

Train the classifier on the given data.

Parameters:
  • train_set (Union[dict, str]) – The dataset for model training, should be a dictionary including keys as ‘X’ and ‘y’, or a path string locating a data file. If it is a dict, X should be array-like of shape [n_samples, sequence length (n_steps), n_features], which is time-series data for training, can contain missing values, and y should be array-like of shape [n_samples], which is classification labels of X. If it is a path string, the path should point to a data file, e.g. a h5 file, which contains key-value pairs like a dict, and it has to include keys as ‘X’ and ‘y’.

  • val_set (Union[dict, str, None]) – The dataset for model validating, should be a dictionary including keys as ‘X’ and ‘y’, or a path string locating a data file. If it is a dict, X should be array-like of shape [n_samples, sequence length (n_steps), n_features], which is time-series data for validating, can contain missing values, and y should be array-like of shape [n_samples], which is classification labels of X. If it is a path string, the path should point to a data file, e.g. a h5 file, which contains key-value pairs like a dict, and it has to include keys as ‘X’ and ‘y’.

  • file_type (str) – The type of the given file if train_set and val_set are path strings.

Return type:

None

abstract predict(test_set, file_type='hdf5')[source]

Make predictions for the input data with the trained model.

Parameters:
  • test_set (Union[dict, str]) – The dataset for model validating, should be a dictionary including keys as ‘X’, or a path string locating a data file supported by PyPOTS (e.g. h5 file). If it is a dict, X should be array-like of shape [n_samples, sequence length (n_steps), n_features], which is time-series data for validating, can contain missing values, and y should be array-like of shape [n_samples], which is classification labels of X. If it is a path string, the path should point to a data file, e.g. a h5 file, which contains key-value pairs like a dict, and it has to include keys as ‘X’ and ‘y’.

  • file_type (str) – The type of the given file if test_set is a path string.

Returns:

Prediction results in a Python Dictionary for the given samples. It should be a dictionary including keys as ‘imputation’, ‘classification’, ‘clustering’, and ‘forecasting’. For sure, only the keys that relevant tasks are supported by the model will be returned.

Return type:

result_dict

abstract classify(test_set, file_type='hdf5')[source]

Classify the input data with the trained model.

Parameters:
  • test_set (Union[dict, str]) – The data samples for testing, should be array-like of shape [n_samples, sequence length (n_steps), n_features], or a path string locating a data file, e.g. h5 file.

  • file_type (str) – The type of the given file if X is a path string.

Returns:

Classification results of the given samples.

Return type:

array-like, shape [n_samples],

load(path)

Load the saved model from a disk file.

Parameters:

path (str) – The local path to a disk file saving the trained model.

Return type:

None

Notes

If the training environment and the deploying/test environment use the same type of device (GPU/CPU), you can load the model directly with torch.load(model_path).

save(saving_path, overwrite=False)

Save the model with current parameters to a disk file.

A .pypots extension will be appended to the filename if it does not already have one. Please note that such an extension is not necessary, but to indicate the saved model is from PyPOTS framework so people can distinguish.

Parameters:
  • saving_path (str) – The given path to save the model. The directory will be created if it does not exist.

  • overwrite (bool) – Whether to overwrite the model file if the path already exists.

Return type:

None

pypots.clustering.base

The base classes for PyPOTS clustering models.

class pypots.clustering.base.BaseClusterer(n_clusters, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseModel

Abstract class for all clustering models.

Parameters:
  • n_clusters (int) – The number of clusters in the clustering task.

  • device (Union[str, device, list, None]) – The device for the model to run on. It can be a string, a torch.device object, or a list of them. If not given, will try to use CUDA devices first (will use the default CUDA device if there are multiple), then CPUs, considering CUDA and CPU are so far the main devices for people to train ML models. If given a list of devices, e.g. [‘cuda:0’, ‘cuda:1’], or [torch.device(‘cuda:0’), torch.device(‘cuda:1’)] , the model will be parallely trained on the multiple devices (so far only support parallel training on CUDA devices). Other devices like Google TPU and Apple Silicon accelerator MPS may be added in the future.

  • saving_path (Optional[str]) – The path for automatically saving model checkpoints and tensorboard files (i.e. loss values recorded during training into a tensorboard file). Will not save if not given.

  • model_saving_strategy (Optional[str]) – The strategy to save model checkpoints. It has to be one of [None, “best”, “better”, “all”]. No model will be saved when it is set as None. The “best” strategy will only automatically save the best model after the training finished. The “better” strategy will automatically save the model during training whenever the model performs better than in previous epochs. The “all” strategy will save every model after each epoch training.

  • verbose (bool) – Whether to print out the training logs during the training process.

abstract fit(train_set, val_set=None, file_type='hdf5')[source]

Train the cluster.

Parameters:
  • train_set (Union[dict, str]) – The dataset for model training, should be a dictionary including the key ‘X’, or a path string locating a data file. If it is a dict, X should be array-like of shape [n_samples, sequence length (n_steps), n_features], which is time-series data for training, can contain missing values. If it is a path string, the path should point to a data file, e.g. a h5 file, which contains key-value pairs like a dict, and it has to include the key ‘X’.

  • val_set (Union[dict, str, None]) – The dataset for model validating, should be a dictionary including keys as ‘X’ and ‘y’, or a path string locating a data file. If it is a dict, X should be array-like of shape [n_samples, sequence length (n_steps), n_features], which is time-series data for validating, can contain missing values, and y should be array-like of shape [n_samples], which is classification labels of X. If it is a path string, the path should point to a data file, e.g. a h5 file, which contains key-value pairs like a dict, and it has to include keys as ‘X’ and ‘y’.

  • file_type (str) – The type of the given file if train_set and val_set are path strings.

Return type:

None

abstract predict(test_set, file_type='hdf5')[source]

Make predictions for the input data with the trained model.

Parameters:
  • test_set (Union[dict, str]) – The dataset for model validating, should be a dictionary including keys as ‘X’, or a path string locating a data file supported by PyPOTS (e.g. h5 file). If it is a dict, X should be array-like of shape [n_samples, sequence length (n_steps), n_features], which is time-series data for validating, can contain missing values, and y should be array-like of shape [n_samples], which is classification labels of X. If it is a path string, the path should point to a data file, e.g. a h5 file, which contains key-value pairs like a dict, and it has to include keys as ‘X’ and ‘y’.

  • file_type (str) – The type of the given file if test_set is a path string.

Returns:

Prediction results in a Python Dictionary for the given samples. It should be a dictionary including keys as ‘imputation’, ‘classification’, ‘clustering’, and ‘forecasting’. For sure, only the keys that relevant tasks are supported by the model will be returned.

Return type:

result_dict

abstract cluster(test_set, file_type='hdf5')[source]

Cluster the input with the trained model.

Parameters:
  • test_set (Union[dict, str]) – The data samples for testing, should be array-like of shape [n_samples, sequence length (n_steps), n_features], or a path string locating a data file, e.g. h5 file.

  • file_type (str) – The type of the given file if X is a path string.

Returns:

Clustering results.

Return type:

array-like,

load(path)

Load the saved model from a disk file.

Parameters:

path (str) – The local path to a disk file saving the trained model.

Return type:

None

Notes

If the training environment and the deploying/test environment use the same type of device (GPU/CPU), you can load the model directly with torch.load(model_path).

save(saving_path, overwrite=False)

Save the model with current parameters to a disk file.

A .pypots extension will be appended to the filename if it does not already have one. Please note that such an extension is not necessary, but to indicate the saved model is from PyPOTS framework so people can distinguish.

Parameters:
  • saving_path (str) – The given path to save the model. The directory will be created if it does not exist.

  • overwrite (bool) – Whether to overwrite the model file if the path already exists.

Return type:

None

class pypots.clustering.base.BaseNNClusterer(n_clusters, batch_size, epochs, patience=None, num_workers=0, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseNNModel

The abstract class for all neural-network clustering models in PyPOTS.

Parameters:
  • n_clusters (int) – The number of clusters in the clustering task.

  • batch_size (int) – Size of the batch input into the model for one step.

  • epochs (int) – Training epochs, i.e. the maximum rounds of the model to be trained with.

  • patience (Optional[int]) – Number of epochs the training procedure will keep if loss doesn’t decrease. Once exceeding the number, the training will stop. Must be smaller than or equal to the value of epochs.

  • num_workers (int) – The number of subprocesses to use for data loading. 0 means data loading will be in the main process, i.e. there won’t be subprocesses.

  • device (Union[str, device, list, None]) – The device for the model to run on. It can be a string, a torch.device object, or a list of them. If not given, will try to use CUDA devices first (will use the default CUDA device if there are multiple), then CPUs, considering CUDA and CPU are so far the main devices for people to train ML models. If given a list of devices, e.g. [‘cuda:0’, ‘cuda:1’], or [torch.device(‘cuda:0’), torch.device(‘cuda:1’)] , the model will be parallely trained on the multiple devices (so far only support parallel training on CUDA devices). Other devices like Google TPU and Apple Silicon accelerator MPS may be added in the future.

  • saving_path (Optional[str]) – The path for automatically saving model checkpoints and tensorboard files (i.e. loss values recorded during training into a tensorboard file). Will not save if not given.

  • model_saving_strategy (Optional[str]) – The strategy to save model checkpoints. It has to be one of [None, “best”, “better”, “all”]. No model will be saved when it is set as None. The “best” strategy will only automatically save the best model after the training finished. The “better” strategy will automatically save the model during training whenever the model performs better than in previous epochs. The “all” strategy will save every model after each epoch training.

  • verbose (bool) – Whether to print out the training logs during the training process.

Notes

Optimizers are necessary for training deep-learning neural networks, but we don’t put a parameter optimizer here because some models (e.g. GANs) need more than one optimizer (e.g. one for generator, one for discriminator), and optimizer is ambiguous for them. Therefore, we leave optimizers as parameters for concrete model implementations, and you can pass any number of optimizers to your model when implementing it, pypots.clustering.crli.CRLI for example.

abstract fit(train_set, val_set=None, file_type='hdf5')[source]

Train the cluster.

Parameters:
  • train_set (Union[dict, str]) – The dataset for model training, should be a dictionary including the key ‘X’, or a path string locating a data file. If it is a dict, X should be array-like of shape [n_samples, sequence length (n_steps), n_features], which is time-series data for training, can contain missing values. If it is a path string, the path should point to a data file, e.g. a h5 file, which contains key-value pairs like a dict, and it has to include the key ‘X’.

  • val_set (Union[dict, str, None]) – The dataset for model validating, should be a dictionary including keys as ‘X’ and ‘y’, or a path string locating a data file. If it is a dict, X should be array-like of shape [n_samples, sequence length (n_steps), n_features], which is time-series data for validating, can contain missing values, and y should be array-like of shape [n_samples], which is classification labels of X. If it is a path string, the path should point to a data file, e.g. a h5 file, which contains key-value pairs like a dict, and it has to include keys as ‘X’ and ‘y’.

  • file_type (str) – The type of the given file if train_set and val_set are path strings.

Return type:

None

abstract predict(test_set, file_type='hdf5')[source]

Make predictions for the input data with the trained model.

Parameters:
  • test_set (Union[dict, str]) – The dataset for model validating, should be a dictionary including keys as ‘X’, or a path string locating a data file supported by PyPOTS (e.g. h5 file). If it is a dict, X should be array-like of shape [n_samples, sequence length (n_steps), n_features], which is time-series data for validating, can contain missing values, and y should be array-like of shape [n_samples], which is classification labels of X. If it is a path string, the path should point to a data file, e.g. a h5 file, which contains key-value pairs like a dict, and it has to include keys as ‘X’ and ‘y’.

  • file_type (str) – The type of the given file if test_set is a path string.

Returns:

Prediction results in a Python Dictionary for the given samples. It should be a dictionary including keys as ‘imputation’, ‘classification’, ‘clustering’, and ‘forecasting’. For sure, only the keys that relevant tasks are supported by the model will be returned.

Return type:

result_dict

abstract cluster(test_set, file_type='hdf5')[source]

Cluster the input with the trained model.

Parameters:
  • test_set (Union[dict, str]) – The data samples for testing, should be array-like of shape [n_samples, sequence length (n_steps), n_features], or a path string locating a data file, e.g. h5 file.

  • file_type (str) – The type of the given file if X is a path string.

Returns:

Clustering results.

Return type:

array-like,

load(path)

Load the saved model from a disk file.

Parameters:

path (str) – The local path to a disk file saving the trained model.

Return type:

None

Notes

If the training environment and the deploying/test environment use the same type of device (GPU/CPU), you can load the model directly with torch.load(model_path).

save(saving_path, overwrite=False)

Save the model with current parameters to a disk file.

A .pypots extension will be appended to the filename if it does not already have one. Please note that such an extension is not necessary, but to indicate the saved model is from PyPOTS framework so people can distinguish.

Parameters:
  • saving_path (str) – The given path to save the model. The directory will be created if it does not exist.

  • overwrite (bool) – Whether to overwrite the model file if the path already exists.

Return type:

None

pypots.forecasting.base

The base classes for PyPOTS forecasting models.

class pypots.forecasting.base.BaseForecaster(device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseModel

Abstract class for all forecasting models.

Parameters:
  • device (Union[str, device, list, None]) – The device for the model to run on. It can be a string, a torch.device object, or a list of them. If not given, will try to use CUDA devices first (will use the default CUDA device if there are multiple), then CPUs, considering CUDA and CPU are so far the main devices for people to train ML models. If given a list of devices, e.g. [‘cuda:0’, ‘cuda:1’], or [torch.device(‘cuda:0’), torch.device(‘cuda:1’)] , the model will be parallely trained on the multiple devices (so far only support parallel training on CUDA devices). Other devices like Google TPU and Apple Silicon accelerator MPS may be added in the future.

  • saving_path (Optional[str]) – The path for automatically saving model checkpoints and tensorboard files (i.e. loss values recorded during training into a tensorboard file). Will not save if not given.

  • model_saving_strategy (Optional[str]) – The strategy to save model checkpoints. It has to be one of [None, “best”, “better”, “all”]. No model will be saved when it is set as None. The “best” strategy will only automatically save the best model after the training finished. The “better” strategy will automatically save the model during training whenever the model performs better than in previous epochs. The “all” strategy will save every model after each epoch training.

  • verbose (bool) – Whether to print out the training logs during the training process.

abstract fit(train_set, val_set=None, file_type='hdf5')[source]

Train the classifier on the given data.

Parameters:
  • train_set (Union[dict, str]) – The dataset for model training, should be a dictionary including the key ‘X’, or a path string locating a data file. If it is a dict, X should be array-like of shape [n_samples, sequence length (n_steps), n_features], which is time-series data for training, can contain missing values. If it is a path string, the path should point to a data file, e.g. a h5 file, which contains key-value pairs like a dict, and it has to include the key ‘X’.

  • val_set (Union[dict, str, None]) – The dataset for model validating, should be a dictionary including the key ‘X’, or a path string locating a data file. If it is a dict, X should be array-like of shape [n_samples, sequence length (n_steps), n_features], which is time-series data for validation, can contain missing values. If it is a path string, the path should point to a data file, e.g. a h5 file, which contains key-value pairs like a dict, and it has to include the key ‘X’.

  • file_type (str) – The type of the given file if train_set and val_set are path strings.

Return type:

None

abstract predict(test_set, file_type='hdf5')[source]

Make predictions for the input data with the trained model.

Parameters:
  • test_set (Union[dict, str]) – The dataset for model validating, should be a dictionary including keys as ‘X’, or a path string locating a data file supported by PyPOTS (e.g. h5 file). If it is a dict, X should be array-like of shape [n_samples, sequence length (n_steps), n_features], which is time-series data for validating, can contain missing values, and y should be array-like of shape [n_samples], which is classification labels of X. If it is a path string, the path should point to a data file, e.g. a h5 file, which contains key-value pairs like a dict, and it has to include keys as ‘X’ and ‘y’.

  • file_type (str) – The type of the given file if test_set is a path string.

Returns:

Prediction results in a Python Dictionary for the given samples. It should be a dictionary including keys as ‘imputation’, ‘classification’, ‘clustering’, and ‘forecasting’. For sure, only the keys that relevant tasks are supported by the model will be returned.

Return type:

result_dict

abstract forecast(test_set, file_type='hdf5')[source]

Forecast the future the input with the trained model.

Parameters:
  • test_set (Union[dict, str]) – The data samples for testing, should be array-like of shape [n_samples, sequence length (n_steps), n_features], or a path string locating a data file, e.g. h5 file.

  • file_type (str) – The type of the given file if X is a path string.

Returns:

Forecasting results.

Return type:

array-like, shape [n_samples, n_pred_steps, n_features],

load(path)

Load the saved model from a disk file.

Parameters:

path (str) – The local path to a disk file saving the trained model.

Return type:

None

Notes

If the training environment and the deploying/test environment use the same type of device (GPU/CPU), you can load the model directly with torch.load(model_path).

save(saving_path, overwrite=False)

Save the model with current parameters to a disk file.

A .pypots extension will be appended to the filename if it does not already have one. Please note that such an extension is not necessary, but to indicate the saved model is from PyPOTS framework so people can distinguish.

Parameters:
  • saving_path (str) – The given path to save the model. The directory will be created if it does not exist.

  • overwrite (bool) – Whether to overwrite the model file if the path already exists.

Return type:

None

class pypots.forecasting.base.BaseNNForecaster(batch_size, epochs, patience=None, num_workers=0, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseNNModel

The abstract class for all neural-network forecasting models in PyPOTS.

Parameters:
  • batch_size (int) – Size of the batch input into the model for one step.

  • epochs (int) – Training epochs, i.e. the maximum rounds of the model to be trained with.

  • patience (Optional[int]) – Number of epochs the training procedure will keep if loss doesn’t decrease. Once exceeding the number, the training will stop. Must be smaller than or equal to the value of epochs.

  • num_workers (int) – The number of subprocesses to use for data loading. 0 means data loading will be in the main process, i.e. there won’t be subprocesses.

  • device (Union[str, device, list, None]) – The device for the model to run on. It can be a string, a torch.device object, or a list of them. If not given, will try to use CUDA devices first (will use the default CUDA device if there are multiple), then CPUs, considering CUDA and CPU are so far the main devices for people to train ML models. If given a list of devices, e.g. [‘cuda:0’, ‘cuda:1’], or [torch.device(‘cuda:0’), torch.device(‘cuda:1’)] , the model will be parallely trained on the multiple devices (so far only support parallel training on CUDA devices). Other devices like Google TPU and Apple Silicon accelerator MPS may be added in the future.

  • saving_path (Optional[str]) – The path for automatically saving model checkpoints and tensorboard files (i.e. loss values recorded during training into a tensorboard file). Will not save if not given.

  • model_saving_strategy (Optional[str]) – The strategy to save model checkpoints. It has to be one of [None, “best”, “better”, “all”]. No model will be saved when it is set as None. The “best” strategy will only automatically save the best model after the training finished. The “better” strategy will automatically save the model during training whenever the model performs better than in previous epochs. The “all” strategy will save every model after each epoch training.

  • verbose (bool) – Whether to print out the training logs during the training process.

Notes

Optimizers are necessary for training deep-learning neural networks, but we don’t put a parameter optimizer here because some models (e.g. GANs) need more than one optimizer (e.g. one for generator, one for discriminator), and optimizer is ambiguous for them. Therefore, we leave optimizers as parameters for concrete model implementations, and you can pass any number of optimizers to your model when implementing it, pypots.clustering.crli.CRLI for example.

abstract fit(train_set, val_set=None, file_type='hdf5')[source]

Train the classifier on the given data.

Parameters:
  • train_set (Union[dict, str]) – The dataset for model training, should be a dictionary including the key ‘X’, or a path string locating a data file. If it is a dict, X should be array-like of shape [n_samples, sequence length (n_steps), n_features], which is time-series data for training, can contain missing values. If it is a path string, the path should point to a data file, e.g. a h5 file, which contains key-value pairs like a dict, and it has to include the key ‘X’.

  • val_set (Union[dict, str, None]) – The dataset for model validating, should be a dictionary including the key ‘X’, or a path string locating a data file. If it is a dict, X should be array-like of shape [n_samples, sequence length (n_steps), n_features], which is time-series data for validation, can contain missing values. If it is a path string, the path should point to a data file, e.g. a h5 file, which contains key-value pairs like a dict, and it has to include the key ‘X’.

  • file_type (str) – The type of the given file if train_set and val_set are path strings.

Return type:

None

abstract predict(test_set, file_type='hdf5')[source]

Make predictions for the input data with the trained model.

Parameters:
  • test_set (Union[dict, str]) – The dataset for model validating, should be a dictionary including keys as ‘X’, or a path string locating a data file supported by PyPOTS (e.g. h5 file). If it is a dict, X should be array-like of shape [n_samples, sequence length (n_steps), n_features], which is time-series data for validating, can contain missing values, and y should be array-like of shape [n_samples], which is classification labels of X. If it is a path string, the path should point to a data file, e.g. a h5 file, which contains key-value pairs like a dict, and it has to include keys as ‘X’ and ‘y’.

  • file_type (str) – The type of the given file if test_set is a path string.

Returns:

Prediction results in a Python Dictionary for the given samples. It should be a dictionary including keys as ‘imputation’, ‘classification’, ‘clustering’, and ‘forecasting’. For sure, only the keys that relevant tasks are supported by the model will be returned.

Return type:

result_dict

abstract forecast(test_set, file_type='hdf5')[source]

Forecast the future the input with the trained model.

Parameters:
  • test_set (Union[dict, str]) – The data samples for testing, should be array-like of shape [n_samples, sequence length (n_steps), n_features], or a path string locating a data file, e.g. h5 file.

  • file_type (str) – The type of the given file if X is a path string.

Returns:

Forecasting results.

Return type:

array-like, shape [n_samples, n_pred_steps, n_features],

load(path)

Load the saved model from a disk file.

Parameters:

path (str) – The local path to a disk file saving the trained model.

Return type:

None

Notes

If the training environment and the deploying/test environment use the same type of device (GPU/CPU), you can load the model directly with torch.load(model_path).

save(saving_path, overwrite=False)

Save the model with current parameters to a disk file.

A .pypots extension will be appended to the filename if it does not already have one. Please note that such an extension is not necessary, but to indicate the saved model is from PyPOTS framework so people can distinguish.

Parameters:
  • saving_path (str) – The given path to save the model. The directory will be created if it does not exist.

  • overwrite (bool) – Whether to overwrite the model file if the path already exists.

Return type:

None