pypots.imputation package

pypots.imputation.saits

The package of the partially-observed time-series imputation model SAITS.

Refer to the paper Wenjie Du, David Cote, and Yan Liu. SAITS: Self-Attention-based Imputation for Time Series. Expert Systems with Applications, 219:119619, 2023.

Notes

This implementation is inspired by the official one https://github.com/WenjieDu/SAITS

class pypots.imputation.saits.SAITS(n_steps, n_features, n_layers, d_model, n_heads, d_k, d_v, d_ffn, dropout=0, attn_dropout=0, diagonal_attention_mask=True, ORT_weight=1, MIT_weight=1, batch_size=32, epochs=100, patience=None, customized_loss_func=<function calc_mae>, optimizer=<pypots.optim.adam.Adam object>, num_workers=0, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseNNImputer

The PyTorch implementation of the SAITS model [1].

Parameters:
  • n_steps (int) – The number of time steps in the time-series data sample.

  • n_features (int) – The number of features in the time-series data sample.

  • n_layers (int) – The number of layers in the 1st and 2nd DMSA blocks in the SAITS model.

  • d_model (int) – The dimension of the model’s backbone. It is the input dimension of the multi-head DMSA layers.

  • n_heads (int) – The number of heads in the multi-head DMSA mechanism. d_model must be divisible by n_heads, and the result should be equal to d_k.

  • d_k (int) – The dimension of the keys (K) and the queries (Q) in the DMSA mechanism. d_k should be the result of d_model divided by n_heads. Although d_k can be directly calculated with given d_model and n_heads, we want it be explicitly given together with d_v by users to ensure users be aware of them and to avoid any potential mistakes.

  • d_v (int) – The dimension of the values (V) in the DMSA mechanism.

  • d_ffn (int) – The dimension of the layer in the Feed-Forward Networks (FFN).

  • dropout (float) – The dropout rate for all fully-connected layers in the model.

  • attn_dropout (float) – The dropout rate for DMSA.

  • diagonal_attention_mask (bool) – Whether to apply a diagonal attention mask to the self-attention mechanism. If so, the attention layers will use DMSA. Otherwise, the attention layers will use the original.

  • ORT_weight (int) – The weight for the ORT loss.

  • MIT_weight (int) – The weight for the MIT loss.

  • batch_size (int) – The batch size for training and evaluating the model.

  • epochs (int) – The number of epochs for training the model.

  • patience (Optional[int]) – The patience for the early-stopping mechanism. Given a positive integer, the training process will be stopped when the model does not perform better after that number of epochs. Leaving it default as None will disable the early-stopping.

  • customized_loss_func (Callable) – The customized loss function designed by users for the model to optimize. If not given, will use the default MAE loss as claimed in the original paper.

  • optimizer (Optional[Optimizer]) – The optimizer for model training. If not given, will use a default Adam optimizer.

  • 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.

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

predict(test_set, file_type='hdf5', diagonal_attention_mask=True, return_latent_vars=False)[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.

  • diagonal_attention_mask (bool) – Whether to apply a diagonal attention mask to the self-attention mechanism in the testing stage.

  • return_latent_vars (bool) – Whether to return the latent variables in SAITS, e.g. attention weights of two DMSA blocks and the weight matrix from the combination block, etc.

Returns:

The dictionary containing the clustering results and latent variables if necessary.

Return type:

file_type

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.imputation.transformer

The package of the partially-observed time-series imputation model Transformer.

Refer to the papers Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N Gomez, Ł ukasz Kaiser, and Illia Polosukhin. Attention is all you need. In Advances in Neural Information Processing Systems, volume 30. Curran Associates, Inc., 2017. and Wenjie Du, David Cote, and Yan Liu. SAITS: Self-Attention-based Imputation for Time Series. Expert Systems with Applications, 219:119619, 2023.

Notes

This implementation is inspired by https://github.com/WenjieDu/SAITS

class pypots.imputation.transformer.Transformer(n_steps, n_features, n_layers, d_model, n_heads, d_k, d_v, d_ffn, dropout=0, attn_dropout=0, ORT_weight=1, MIT_weight=1, batch_size=32, epochs=100, patience=None, optimizer=<pypots.optim.adam.Adam object>, num_workers=0, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseNNImputer

The PyTorch implementation of the Transformer model. Transformer is originally proposed by Vaswani et al. in [37], and gets re-implemented as a time-series imputation model by Du et al. in [1]. Here you should refer to [1] for details about this Transformer imputation model.

Parameters:
  • n_steps (int) – The number of time steps in the time-series data sample.

  • n_features (int) – The number of features in the time-series data sample.

  • n_layers (int) – The number of layers in the 1st and 2nd DMSA blocks in the SAITS model.

  • d_model (int) – The dimension of the model’s backbone. It is the input dimension of the multi-head self-attention layers.

  • n_heads (int) – The number of heads in the multi-head self-attention mechanism. d_model must be divisible by n_heads, and the result should be equal to d_k.

  • d_k (int) – The dimension of the keys (K) and the queries (Q) in the DMSA mechanism. d_k should be the result of d_model divided by n_heads. Although d_k can be directly calculated with given d_model and n_heads, we want it be explicitly given together with d_v by users to ensure users be aware of them and to avoid any potential mistakes.

  • d_v (int) – The dimension of the values (V) in the DMSA mechanism.

  • d_ffn (int) – The dimension of the layer in the Feed-Forward Networks (FFN).

  • dropout (float) – The dropout rate for all fully-connected layers in the model.

  • attn_dropout (float) – The dropout rate for the attention mechanism.

  • ORT_weight (int) – The weight for the ORT loss.

  • MIT_weight (int) – The weight for the MIT loss.

  • batch_size (int) – The batch size for training and evaluating the model.

  • epochs (int) – The number of epochs for training the model.

  • patience (Optional[int]) – The patience for the early-stopping mechanism. Given a positive integer, the training process will be stopped when the model does not perform better after that number of epochs. Leaving it default as None will disable the early-stopping.

  • optimizer (Optional[Optimizer]) – The optimizer for model training. If not given, will use a default Adam optimizer.

  • 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.

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

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

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.imputation.timemixer

The package including the modules of TimeMixer.

Refer to the paper Shiyu Wang, Haixu Wu, Xiaoming Shi, Tengge Hu, Huakun Luo, Lintao Ma, James Y. Zhang, and Jun Zhou. “TimeMixer: Decomposable Multiscale Mixing for Time Series Forecasting”. In ICLR 2024.

Notes

This implementation is inspired by the official one https://github.com/kwuking/TimeMixer

class pypots.imputation.timemixer.TimeMixer(n_steps, n_features, n_layers, d_model, d_ffn, top_k, dropout=0, channel_independence=False, decomp_method='moving_avg', moving_avg=5, downsampling_layers=3, downsampling_window=2, apply_nonstationary_norm=False, batch_size=32, epochs=100, patience=None, optimizer=<pypots.optim.adam.Adam object>, num_workers=0, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseNNImputer

The PyTorch implementation of the TimeMixer model. TimeMixer is originally proposed by Wang et al. in [3].

Parameters:
  • n_steps (int) – The number of time steps in the time-series data sample.

  • n_features (int) – The number of features in the time-series data sample.

  • n_layers (int) – The number of layers in the TimeMixer model.

  • d_model (int) – The dimension of the model.

  • d_ffn (int) – The dimension of the feed-forward network.

  • top_k (int) – The number of top-k amplitude values to be selected to obtain the most significant frequencies.

  • dropout (float) – The dropout rate for the model.

  • channel_independence (bool) – Whether to use channel independence in the model.

  • decomp_method (str) – The decomposition method for the model. It has to be one of [‘moving_avg’, ‘dft_decomp’].

  • moving_avg (int) – The window size for moving average decomposition.

  • downsampling_layers (int) – The number of downsampling layers in the model.

  • downsampling_window (int) – The window size for downsampling.

  • apply_nonstationary_norm (bool) – Whether to apply non-stationary normalization to the input data for TimeMixer. Please refer to [17] for details about non-stationary normalization, which is not the idea of the original TimeMixer paper. Hence, we make it optional and default not to use here.

  • batch_size (int) – The batch size for training and evaluating the model.

  • epochs (int) – The number of epochs for training the model.

  • patience (Optional[int]) – The patience for the early-stopping mechanism. Given a positive integer, the training process will be stopped when the model does not perform better after that number of epochs. Leaving it default as None will disable the early-stopping.

  • optimizer (Optional[Optimizer]) – The optimizer for model training. If not given, will use a default Adam optimizer.

  • 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.

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

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

Make predictions for the input data with the trained model.

Parameters:
  • test_set (dict or 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:

The dictionary containing the clustering results and latent variables if necessary.

Return type:

file_type

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.imputation.moderntcn

The package of the partially-observed time-series imputation model ModernTCN.

Refer to the paper Donghao Luo, and Xue Wang. ModernTCN: A Modern Pure Convolution Structure for General Time Series Analysis. In The Twelfth International Conference on Learning Representations. 2024.

Notes

This implementation is inspired by the official one https://github.com/luodhhh/ModernTCN

class pypots.imputation.moderntcn.ModernTCN(n_steps, n_features, patch_size, patch_stride, downsampling_ratio, ffn_ratio, num_blocks, large_size, small_size, dims, small_kernel_merged=False, backbone_dropout=0.1, head_dropout=0.1, use_multi_scale=True, individual=False, apply_nonstationary_norm=False, batch_size=32, epochs=100, patience=None, optimizer=<pypots.optim.adam.Adam object>, num_workers=0, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseNNImputer

The PyTorch implementation of the ModernTCN model. ModernTCN is originally proposed by Luo et al. in [5].

Parameters:
  • n_steps (int) – The number of time steps in the time-series data sample.

  • n_features (int) – The number of features in the time-series data sample.

  • patch_size (int) – The size of the patch for the patching mechanism.

  • patch_stride (int) – The stride for the patching mechanism.

  • downsampling_ratio (float) – The downsampling ratio for the downsampling mechanism.

  • ffn_ratio (float) – The ratio for the feed-forward neural network in the model.

  • num_blocks (list) – The number of blocks for the model. It should be a list of integers.

  • large_size (list) – The size of the large kernel. It should be a list of odd integers.

  • small_size (list) – The size of the small kernel. It should be a list of odd integers.

  • dims (list) – The dimensions for the model. It should be a list of integers.

  • small_kernel_merged (bool) – Whether the small kernel is merged.

  • backbone_dropout (float) – The dropout rate for the backbone of the model.

  • head_dropout (float) – The dropout rate for the head of the model.

  • use_multi_scale (bool) – Whether to use multi-scale fusing.

  • individual (bool) – Whether to make a linear layer for each variate/channel/feature individually.

  • apply_nonstationary_norm (bool) – Whether to apply non-stationary normalization.

  • batch_size (int) – The batch size for training and evaluating the model.

  • epochs (int) – The number of epochs for training the model.

  • patience (Optional[int]) – The patience for the early-stopping mechanism. Given a positive integer, the training process will be stopped when the model does not perform better after that number of epochs. Leaving it default as None will disable the early-stopping.

  • optimizer (Optional[Optimizer]) – The optimizer for model training. If not given, will use a default Adam optimizer.

  • 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.

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

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

Make predictions for the input data with the trained model.

Parameters:
  • test_set (dict or 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:

The dictionary containing the clustering results and latent variables if necessary.

Return type:

file_type

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.imputation.imputeformer

The package of the partially-observed time-series imputation model ImputeFormer.

Refer to the papers Tong Nie, Guoyang Qin, Wei Ma, Yuewen Mei, Jian Sun. “ImputeFormer: Low Rankness-Induced Transformers for Generalizable Spatiotemporal Imputation” KDD 2024.

class pypots.imputation.imputeformer.ImputeFormer(n_steps, n_features, n_layers, d_input_embed, d_learnable_embed, d_proj, d_ffn, n_temporal_heads, dropout=0.0, input_dim=1, output_dim=1, ORT_weight=1, MIT_weight=1, batch_size=32, epochs=100, patience=None, optimizer=<pypots.optim.adam.Adam object>, num_workers=0, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseNNImputer

The PyTorch implementation of the ImputeFormer model. ImputeFormer is originally proposed by Nie et al. in KDD’24: cite:nie2024imputeformer.

Parameters:
  • n_steps (int) – The number of time steps in the time-series data sample.

  • n_features (int) – The number of features in the time-series data sample.

  • n_layers (int) – The number of layers in the 1st and 2nd DMSA blocks in the SAITS model.

  • d_input_embed (int) – The dimension of the input embedding. It is the input dimension of the input embedding layer.

  • d_learnable_embed (int) – The dimension of the learnable node embedding. It is the dimension of the learnable node embedding (spatial positional embedding) used in spatial attention layers.

  • d_proj (int) – The dimension of the learnable projector. It is the dimension of the learnable projector used in temporal attention layers.

  • d_ffn (int) – The dimension of the layer in the Feed-Forward Networks (FFN).

  • dropout (float) – The dropout rate for all fully-connected layers in the model.

  • n_temporal_heads (int) – The number of attention heads in temporal attention layers.

  • input_dim (int) – The dimension of the input feature dimension, default is 1.

  • output_dim (int) – The dimension of the output feature dimension, default is 1.

  • ORT_weight (float) – The weight for the ORT loss.

  • MIT_weight (float) – The weight for the MIT loss.

  • batch_size (int) – The batch size for training and evaluating the model.

  • epochs (int) – The number of epochs for training the model.

  • patience (Optional[int]) – The patience for the early-stopping mechanism. Given a positive integer, the training process will be stopped when the model does not perform better after that number of epochs. Leaving it default as None will disable the early-stopping.

  • optimizer (Optional[Optimizer]) – The optimizer for model training. If not given, will use a default Adam optimizer.

  • 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.

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

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

impute(X, file_type='hdf5')[source]

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

Warning

The method impute is deprecated. Please use predict() instead.

Parameters:
  • X (Union[dict, str]) – The data samples for testing, should be array-like of shape [n_samples, sequence length (time 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 (time 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.imputation.itransformer

The package of the partially-observed time-series imputation model iTransformer.

Refer to the papers Liu, Yong, Tengge Hu, Haoran Zhang, Haixu Wu, Shiyu Wang, Lintao Ma, and Mingsheng Long. “iTransformer: Inverted transformers are effective for time series forecasting.” ICLR 2024.

Notes

This implementation is inspired by the official one https://github.com/thuml/iTransformer

class pypots.imputation.itransformer.iTransformer(n_steps, n_features, n_layers, d_model, n_heads, d_k, d_v, d_ffn, dropout=0, attn_dropout=0, ORT_weight=1, MIT_weight=1, batch_size=32, epochs=100, patience=None, optimizer=<pypots.optim.adam.Adam object>, num_workers=0, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseNNImputer

The PyTorch implementation of the iTransformer model. iTransformer is originally proposed by Liu et al. in [4].

Parameters:
  • n_steps (int) – The number of time steps in the time-series data sample.

  • n_features (int) – The number of features in the time-series data sample.

  • n_layers (int) – The number of layers in the 1st and 2nd DMSA blocks in the SAITS model.

  • d_model (int) – The dimension of the model’s backbone. It is the input dimension of the multi-head self-attention layers.

  • n_heads (int) – The number of heads in the multi-head self-attention mechanism. d_model must be divisible by n_heads, and the result should be equal to d_k.

  • d_k (int) – The dimension of the keys (K) and the queries (Q) in the DMSA mechanism. d_k should be the result of d_model divided by n_heads. Although d_k can be directly calculated with given d_model and n_heads, we want it be explicitly given together with d_v by users to ensure users be aware of them and to avoid any potential mistakes.

  • d_v (int) – The dimension of the values (V) in the DMSA mechanism.

  • d_ffn (int) – The dimension of the layer in the Feed-Forward Networks (FFN).

  • dropout (float) – The dropout rate for all fully-connected layers in the model.

  • attn_dropout (float) – The dropout rate for DMSA.

  • ORT_weight (int) – The weight for the ORT loss.

  • MIT_weight (int) – The weight for the MIT loss.

  • batch_size (int) – The batch size for training and evaluating the model.

  • epochs (int) – The number of epochs for training the model.

  • patience (Optional[int]) – The patience for the early-stopping mechanism. Given a positive integer, the training process will be stopped when the model does not perform better after that number of epochs. Leaving it default as None will disable the early-stopping.

  • optimizer (Optional[Optimizer]) – The optimizer for model training. If not given, will use a default Adam optimizer.

  • 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.

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

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

impute(X, file_type='hdf5')[source]

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

Warning

The method impute is deprecated. Please use predict() instead.

Parameters:
  • X (Union[dict, str]) – The data samples for testing, should be array-like of shape [n_samples, sequence length (time 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 (time 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.imputation.koopa

The package of the partially-observed time-series imputation model Koopa.

Refer to the paper Yong Liu, Chenyu Li, Jianmin Wang, and Mingsheng Long. “Koopa: Learning Non-stationary Time Series Dynamics with Koopman Predictors”. Advances in Neural Information Processing Systems 36 (2023).

Notes

This implementation is inspired by the official one https://github.com/thuml/Koopa

class pypots.imputation.koopa.Koopa(n_steps, n_features, n_seg_steps, d_dynamic, d_hidden, n_hidden_layers, n_blocks, multistep=False, alpha=0.2, ORT_weight=1, MIT_weight=1, batch_size=32, epochs=100, patience=None, optimizer=<pypots.optim.adam.Adam object>, num_workers=0, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseNNImputer

The PyTorch implementation of the Koopa model. Koopa is originally proposed by Liu et al. in [8].

Parameters:
  • n_steps (int) – The number of time steps in the time-series data sample.

  • n_features (int) – The number of features in the time-series data sample.

  • n_seg_steps (int) – Segment length of time series.

  • d_dynamic (int) – Latent dimension of koopman embedding.

  • d_hidden (int) – The dimension of the hidden layers in the model.

  • n_hidden_layers (int) – The number of hidden layers in the model.

  • n_blocks (int) – The number of blocks in the model.

  • multistep (bool) – Whether to use the multistep approximation for multistep K in the model.

  • alpha (int) – spectrum filter ratio

  • ORT_weight (float) – The weight for the ORT loss, the same as SAITS.

  • MIT_weight (float) – The weight for the MIT loss, the same as SAITS.

  • batch_size (int) – The batch size for training and evaluating the model.

  • epochs (int) – The number of epochs for training the model.

  • patience (Optional[int]) – The patience for the early-stopping mechanism. Given a positive integer, the training process will be stopped when the model does not perform better after that number of epochs. Leaving it default as None will disable the early-stopping.

  • optimizer (Optional[Optimizer]) – The optimizer for model training. If not given, will use a default Adam optimizer.

  • 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.

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

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

Make predictions for the input data with the trained model.

Parameters:
  • test_set (dict or 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:

The dictionary containing the clustering results and latent variables if necessary.

Return type:

file_type

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.imputation.frets

The package of the partially-observed time-series imputation model FreTS.

Refer to the paper Kun Yi, Qi Zhang, Wei Fan, Shoujin Wang, Pengyang Wang, Hui He, Ning An, Defu Lian, Longbing Cao, and Zhendong Niu. “Frequency-domain MLPs are More Effective Learners in Time Series Forecasting.” Advances in Neural Information Processing Systems 36 (2024).

Notes

This implementation is inspired by the official one https://github.com/aikunyi/FreTS

class pypots.imputation.frets.FreTS(n_steps, n_features, embed_size=128, hidden_size=256, channel_independence=False, ORT_weight=1, MIT_weight=1, batch_size=32, epochs=100, patience=None, optimizer=<pypots.optim.adam.Adam object>, num_workers=0, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseNNImputer

The PyTorch implementation of the FreTS model. FreTS is originally proposed by Yi et al. in [7].

Parameters:
  • n_steps (int) – The number of time steps in the time-series data sample.

  • n_features (int) – The number of features in the time-series data sample.

  • embed_size (int) – The size of the embedding layer in the FreTS model.

  • hidden_size (int) – The size of the hidden layer in the FreTS model.

  • channel_independence (bool) – Whether to use the channel independence mechanism in the FreTS model.

  • ORT_weight (float) – The weight for the ORT loss, the same as SAITS.

  • MIT_weight (float) – The weight for the MIT loss, the same as SAITS.

  • batch_size (int) – The batch size for training and evaluating the model.

  • epochs (int) – The number of epochs for training the model.

  • patience (Optional[int]) – The patience for the early-stopping mechanism. Given a positive integer, the training process will be stopped when the model does not perform better after that number of epochs. Leaving it default as None will disable the early-stopping.

  • optimizer (Optional[Optimizer]) – The optimizer for model training. If not given, will use a default Adam optimizer.

  • 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.

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

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

Make predictions for the input data with the trained model.

Parameters:
  • test_set (dict or 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:

The dictionary containing the clustering results and latent variables if necessary.

Return type:

file_type

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.imputation.crossformer

The package of the partially-observed time-series imputation model Transformer.

Refer to the paper Yunhao Zhang and Junchi Yan. Crossformer: Transformer utilizing cross-dimension dependency for multivariate time series forecasting. In The 11th ICLR, 2023.

Notes

This implementation is inspired by the official one https://github.com/Thinklab-SJTU/Crossformer

class pypots.imputation.crossformer.Crossformer(n_steps, n_features, n_layers, d_model, n_heads, d_ffn, factor, seg_len, win_size, dropout=0, ORT_weight=1, MIT_weight=1, batch_size=32, epochs=100, patience=None, optimizer=<pypots.optim.adam.Adam object>, num_workers=0, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseNNImputer

The PyTorch implementation of the Crossformer model. Crossformer is originally proposed by Zhang et al. in [9].

Parameters:
  • n_steps (int) – The number of time steps in the time-series data sample.

  • n_features (int) – The number of features in the time-series data sample.

  • n_layers (int) – The number of layers in the 1st and 2nd DMSA blocks in the SAITS model.

  • d_model (int) – The dimension of the model.

  • n_heads (int) – The number of heads in the multi-head attention mechanism.

  • d_ffn (int) – The dimension of the feed-forward network.

  • factor (int) – The num of routers in Cross-Dimension Stage of TSA (c).

  • seg_len (int) – The length of the segment in the model.

  • win_size (int) – The window size for merging segment.

  • dropout (float) – The dropout rate for the model.

  • ORT_weight (float) – The weight for the ORT loss, the same as SAITS.

  • MIT_weight (float) – The weight for the MIT loss, the same as SAITS.

  • batch_size (int) – The batch size for training and evaluating the model.

  • epochs (int) – The number of epochs for training the model.

  • patience (Optional[int]) – The patience for the early-stopping mechanism. Given a positive integer, the training process will be stopped when the model does not perform better after that number of epochs. Leaving it default as None will disable the early-stopping.

  • optimizer (Optional[Optimizer]) – The optimizer for model training. If not given, will use a default Adam optimizer.

  • 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.

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

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

Make predictions for the input data with the trained model.

Parameters:
  • test_set (dict or 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:

The dictionary containing the clustering results and latent variables if necessary.

Return type:

file_type

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.imputation.timesnet

The package of the partially-observed time-series imputation model TimesNet.

Refer to the paper Haixu Wu, Tengge Hu, Yong Liu, Hang Zhou, Jianmin Wang, and Mingsheng Long. TimesNet: Temporal 2D-Variation Modeling for General Time Series Analysis. In ICLR, 2023.

Notes

This implementation is inspired by the official one https://github.com/thuml/Time-Series-Library

class pypots.imputation.timesnet.TimesNet(n_steps, n_features, n_layers, top_k, d_model, d_ffn, n_kernels, dropout=0, apply_nonstationary_norm=False, batch_size=32, epochs=100, patience=None, optimizer=<pypots.optim.adam.Adam object>, num_workers=0, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseNNImputer

The PyTorch implementation of the TimesNet model. TimesNet is originally proposed by Wu et al. in [10].

Parameters:
  • n_steps (int) – The number of time steps in the time-series data sample.

  • n_features (int) – The number of features in the time-series data sample.

  • n_layers (int) – The number of layers in the TimesNet model.

  • top_k (int) – The number of top-k amplitude values to be selected to obtain the most significant frequencies.

  • d_model (int) – The dimension of the model.

  • d_ffn (int) – The dimension of the feed-forward network.

  • n_kernels (int) – The number of 2D kernels (2D convolutional layers) to use in the submodule InceptionBlockV1.

  • dropout (float) – The dropout rate for the model.

  • apply_nonstationary_norm (bool) – Whether to apply non-stationary normalization to the input data for TimesNet. Please refer to [17] for details about non-stationary normalization, which is not the idea of the original TimesNet paper. Hence, we make it optional and default not to use here.

  • batch_size (int) – The batch size for training and evaluating the model.

  • epochs (int) – The number of epochs for training the model.

  • patience (Optional[int]) – The patience for the early-stopping mechanism. Given a positive integer, the training process will be stopped when the model does not perform better after that number of epochs. Leaving it default as None will disable the early-stopping.

  • optimizer (Optional[Optimizer]) – The optimizer for model training. If not given, will use a default Adam optimizer.

  • 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.

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

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

Make predictions for the input data with the trained model.

Parameters:
  • test_set (dict or 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:

The dictionary containing the clustering results and latent variables if necessary.

Return type:

file_type

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.imputation.patchtst

The package of the partially-observed time-series imputation model PatchTST.

Refer to the paper Yuqi Nie, Nam H Nguyen, Phanwadee Sinthong, and Jayant Kalagnanam. A time series is worth 64 words: Long-term forecasting with transformers. In ICLR, 2023.

Notes

This implementation is inspired by the official one https://github.com/yuqinie98/PatchTST

class pypots.imputation.patchtst.PatchTST(n_steps, n_features, patch_len, stride, n_layers, d_model, n_heads, d_k, d_v, d_ffn, dropout, attn_dropout, ORT_weight=1, MIT_weight=1, batch_size=32, epochs=100, patience=None, optimizer=<pypots.optim.adam.Adam object>, num_workers=0, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseNNImputer

The PyTorch implementation of the PatchTST model. TimesNet is originally proposed by Wu et al. in [11].

Parameters:
  • n_steps (int) – The number of time steps in the time-series data sample.

  • n_features (int) – The number of features in the time-series data sample.

  • patch_len (int) – The patch length for patch embedding.

  • stride (int) – The stride for patch embedding.

  • n_layers (int) – The number of layers in the PatchTST model.

  • d_model (int) – The dimension of the model.

  • n_heads (int) – The number of heads in each layer of PatchTST.

  • d_k (int) – The dimension of the keys (K) and the queries (Q) in the DMSA mechanism. d_k should be the result of d_model divided by n_heads. Although d_k can be directly calculated with given d_model and n_heads, we want it be explicitly given together with d_v by users to ensure users be aware of them and to avoid any potential mistakes.

  • d_v (int) – The dimension of the values (V) in the DMSA mechanism.

  • d_ffn (int) – The dimension of the feed-forward network.

  • dropout (float) – The dropout rate for the model.

  • ORT_weight (float) – The weight for the ORT loss, the same as SAITS.

  • MIT_weight (float) – The weight for the MIT loss, the same as SAITS.

  • batch_size (int) – The batch size for training and evaluating the model.

  • epochs (int) – The number of epochs for training the model.

  • patience (Optional[int]) – The patience for the early-stopping mechanism. Given a positive integer, the training process will be stopped when the model does not perform better after that number of epochs. Leaving it default as None will disable the early-stopping.

  • optimizer (Optional[Optimizer]) – The optimizer for model training. If not given, will use a default Adam optimizer.

  • 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.

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

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

Make predictions for the input data with the trained model.

Parameters:
  • test_set (dict or 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:

The dictionary containing the clustering results and latent variables if necessary.

Return type:

file_type

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.imputation.etsformer

The package of the partially-observed time-series imputation model ETSformer.

Refer to the paper Gerald Woo, Chenghao Liu, Doyen Sahoo, Akshat Kumar, and Steven Hoi. ETSformer: Exponential smoothing transformers for time-series forecasting. In ICLR, 2023.

Notes

This implementation is inspired by the official one https://github.com/salesforce/ETSformer

class pypots.imputation.etsformer.ETSformer(n_steps, n_features, n_e_layers, n_d_layers, d_model, n_heads, d_ffn, top_k, dropout=0, ORT_weight=1, MIT_weight=1, batch_size=32, epochs=100, patience=None, optimizer=<pypots.optim.adam.Adam object>, num_workers=0, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseNNImputer

The PyTorch implementation of the ETSformer model. ETSformer is originally proposed by Woo et al. in [12].

Parameters:
  • n_steps (int) – The number of time steps in the time-series data sample.

  • n_features (int) – The number of features in the time-series data sample.

  • n_e_layers (int) – The number of layers in the ETSformer encoder.

  • n_d_layers (int) – The number of layers in the ETSformer decoder.

  • d_model (int) – The dimension of the model.

  • n_heads (int) – The number of heads in each layer of ETSformer.

  • d_ffn (int) – The dimension of the feed-forward network.

  • top_k (int) – Top-K Fourier bases.

  • dropout (float) – The dropout rate for the model.

  • ORT_weight (float) – The weight for the ORT loss, the same as SAITS.

  • MIT_weight (float) – The weight for the MIT loss, the same as SAITS.

  • batch_size (int) – The batch size for training and evaluating the model.

  • epochs (int) – The number of epochs for training the model.

  • patience (Optional[int]) – The patience for the early-stopping mechanism. Given a positive integer, the training process will be stopped when the model does not perform better after that number of epochs. Leaving it default as None will disable the early-stopping.

  • optimizer (Optional[Optimizer]) – The optimizer for model training. If not given, will use a default Adam optimizer.

  • 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.

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

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

Make predictions for the input data with the trained model.

Parameters:
  • test_set (dict or 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:

The dictionary containing the clustering results and latent variables if necessary.

Return type:

file_type

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.imputation.micn

The package of the partially-observed time-series imputation model MICN.

Refer to the paper Huiqiang Wang, Jian Peng, Feihu Huang, Jince Wang, Junhui Chen, and Yifei Xiao “MICN: Multi-scale Local and Global Context Modeling for Long-term Series Forecasting”. In the Eleventh International Conference on Learning Representations, 2023.

Notes

This implementation is inspired by the official one https://github.com/wanghq21/MICN

class pypots.imputation.micn.MICN(n_steps, n_features, n_layers, d_model, conv_kernel, dropout=0, ORT_weight=1, MIT_weight=1, batch_size=32, epochs=100, patience=None, optimizer=<pypots.optim.adam.Adam object>, num_workers=0, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseNNImputer

The PyTorch implementation of the MICN model. MICN is originally proposed by Huang et al. in [13].

Parameters:
  • n_steps (int) – The number of time steps in the time-series data sample.

  • n_features (int) – The number of features in the time-series data sample.

  • n_layers (int) – The number of layers in the MICN model.

  • d_model (int) – The dimension of the model.

  • conv_kernel (list) – The kernel size for the convolutional layers in the model. It should be a list of integers, and the maximum value in the list should be less than or equal to the minimum value of n_steps and n_features.

  • dropout (float) – The dropout rate for the model.

  • ORT_weight (float) – The weight for the ORT loss, the same as SAITS.

  • MIT_weight (float) – The weight for the MIT loss, the same as SAITS.

  • batch_size (int) – The batch size for training and evaluating the model.

  • epochs (int) – The number of epochs for training the model.

  • patience (Optional[int]) – The patience for the early-stopping mechanism. Given a positive integer, the training process will be stopped when the model does not perform better after that number of epochs. Leaving it default as None will disable the early-stopping.

  • optimizer (Optional[Optimizer]) – The optimizer for model training. If not given, will use a default Adam optimizer.

  • 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.

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

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

Make predictions for the input data with the trained model.

Parameters:
  • test_set (dict or 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:

The dictionary containing the clustering results and latent variables if necessary.

Return type:

file_type

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.imputation.dlinear

The package of the partially-observed time-series imputation model DLinear.

Refer to the paper Ailing Zeng, Muxi Chen, Lei Zhang, and Qiang Xu. Are transformers effective for time series forecasting? In AAAI, volume 37, pages 11121–11128, Jun. 2023.

Notes

This implementation is inspired by the official one https://github.com/cure-lab/LTSF-Linear

class pypots.imputation.dlinear.DLinear(n_steps, n_features, moving_avg_window_size, individual=False, d_model=None, ORT_weight=1, MIT_weight=1, batch_size=32, epochs=100, patience=None, optimizer=<pypots.optim.adam.Adam object>, num_workers=0, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseNNImputer

The PyTorch implementation of the DLinear model. DLinear is originally proposed by Zeng et al. in [14].

Parameters:
  • n_steps (int) – The number of time steps in the time-series data sample.

  • n_features (int) – The number of features in the time-series data sample.

  • moving_avg_window_size (int) – The window size of moving average.

  • individual (bool) – Whether to make a linear layer for each variate/channel/feature individually.

  • d_model (Optional[int]) – The dimension of the space in which the time-series data will be embedded and modeled. It is necessary only for DLinear in the non-individual mode.

  • ORT_weight (float) – The weight for the ORT loss, the same as SAITS.

  • MIT_weight (float) – The weight for the MIT loss, the same as SAITS.

  • batch_size (int) – The batch size for training and evaluating the model.

  • epochs (int) – The number of epochs for training the model.

  • patience (Optional[int]) – The patience for the early-stopping mechanism. Given a positive integer, the training process will be stopped when the model does not perform better after that number of epochs. Leaving it default as None will disable the early-stopping.

  • optimizer (Optional[Optimizer]) – The optimizer for model training. If not given, will use a default Adam optimizer.

  • 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.

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

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

Make predictions for the input data with the trained model.

Parameters:
  • test_set (dict or 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:

The dictionary containing the clustering results and latent variables if necessary.

Return type:

file_type

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.imputation.tide

The package of the partially-observed time-series imputation model TiDE.

Refer to the paper Abhimanyu Das, Weihao Kong, Andrew Leach, Shaan Mathur, Rajat Sen, and Rose Yu. “Long-term Forecasting with TiDE: Time-series Dense Encoder”. In Transactions on Machine Learning Research, 2023.

Notes

This implementation is inspired by the official one https://github.com/google-research/google-research/blob/master/tide and https://github.com/lich99/TiDE

class pypots.imputation.tide.TiDE(n_steps, n_features, n_layers, d_model, d_hidden, d_feature_encode, d_temporal_decoder_hidden, dropout=0, ORT_weight=1, MIT_weight=1, batch_size=32, epochs=100, patience=None, optimizer=<pypots.optim.adam.Adam object>, num_workers=0, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseNNImputer

The PyTorch implementation of the TiDE model. TiDE is originally proposed by Das et al. in [15].

Parameters:
  • n_steps (int) – The number of time steps in the time-series data sample.

  • n_features (int) – The number of features in the time-series data sample.

  • n_layers (int) – The number of layers in the TiDE model.

  • d_model (int) – The dimension of the model.

  • d_hidden (int) – The dimension of the hidden layer in the model.

  • d_feature_encode (int) – The dimension of the feature encoder.

  • d_temporal_decoder_hidden (int) – The dimension of the hidden layer in the temporal decoder.

  • dropout (float) – The dropout rate for the model.

  • ORT_weight (float) – The weight for the ORT loss, the same as SAITS.

  • MIT_weight (float) – The weight for the MIT loss, the same as SAITS.

  • batch_size (int) – The batch size for training and evaluating the model.

  • epochs (int) – The number of epochs for training the model.

  • patience (Optional[int]) – The patience for the early-stopping mechanism. Given a positive integer, the training process will be stopped when the model does not perform better after that number of epochs. Leaving it default as None will disable the early-stopping.

  • optimizer (Optional[Optimizer]) – The optimizer for model training. If not given, will use a default Adam optimizer.

  • 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.

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

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

Make predictions for the input data with the trained model.

Parameters:
  • test_set (dict or 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:

The dictionary containing the clustering results and latent variables if necessary.

Return type:

file_type

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.imputation.scinet

The package of the partially-observed time-series imputation model SCINet.

Refer to the paper Minhao LIU, Ailing Zeng, Muxi Chen, Zhijian Xu, Qiuxia LAI, Lingna Ma, and Qiang Xu. “SCINet: Time Series Modeling and Forecasting with Sample Convolution and Interaction”. In Advances in Neural Information Processing Systems, 2022.

Notes

This implementation is inspired by the official one https://github.com/cure-lab/SCINet

class pypots.imputation.scinet.SCINet(n_steps, n_features, n_stacks, n_levels, n_groups, n_decoder_layers, d_hidden, kernel_size=3, concat_len=0, dropout=0.5, pos_enc=False, ORT_weight=1, MIT_weight=1, batch_size=32, epochs=100, patience=None, optimizer=<pypots.optim.adam.Adam object>, num_workers=0, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseNNImputer

The PyTorch implementation of the SCINet model. SCINet is originally proposed by Liu et al. in [16].

Parameters:
  • n_steps (int) – The number of time steps in the time-series data sample.

  • n_features (int) – The number of features in the time-series data sample.

  • n_stacks (int) – The number of stacks in the model.

  • n_levels (int) – The number of levels in the model.

  • n_groups (int) – The number of groups in the model.

  • n_decoder_layers (int) – The number of decoder layers in the model.

  • d_hidden (int) – The hidden dimension for the model.

  • kernel_size (int) – The kernel size for the model.

  • dropout (float) – The dropout rate for the model.

  • concat_len (int) – The length for concatenation.

  • pos_enc (bool) – Whether to use positional encoding in the model.

  • ORT_weight (float) – The weight for the ORT loss, the same as SAITS.

  • MIT_weight (float) – The weight for the MIT loss, the same as SAITS.

  • batch_size (int) – The batch size for training and evaluating the model.

  • epochs (int) – The number of epochs for training the model.

  • patience (Optional[int]) – The patience for the early-stopping mechanism. Given a positive integer, the training process will be stopped when the model does not perform better after that number of epochs. Leaving it default as None will disable the early-stopping.

  • optimizer (Optional[Optimizer]) – The optimizer for model training. If not given, will use a default Adam optimizer.

  • 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.

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

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

Make predictions for the input data with the trained model.

Parameters:
  • test_set (dict or 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:

The dictionary containing the clustering results and latent variables if necessary.

Return type:

file_type

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.imputation.nonstationary_transformer

The package of the partially-observed time-series imputation model Nonstationary-Transformer.

Refer to the paper Yong Liu, Haixu Wu, Jianmin Wang, Mingsheng Long. Non-stationary Transformers: Exploring the Stationarity in Time Series Forecasting. Advances in Neural Information Processing Systems 35 (2022): 9881-9893.

Notes

This implementation is inspired by the official one https://github.com/thuml/Nonstationary_Transformers

class pypots.imputation.nonstationary_transformer.NonstationaryTransformer(n_steps, n_features, n_layers, d_model, n_heads, d_ffn, d_projector_hidden, n_projector_hidden_layers, dropout=0, ORT_weight=1, MIT_weight=1, batch_size=32, epochs=100, patience=None, optimizer=<pypots.optim.adam.Adam object>, num_workers=0, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseNNImputer

The PyTorch implementation of the Nonstationary-Transformer model. NonstationaryTransformer is originally proposed by Liu et al. in [17].

Parameters:
  • n_steps (int) – The number of time steps in the time-series data sample.

  • n_features (int) – The number of features in the time-series data sample.

  • n_layers (int) – The number of layers in the NonstationaryTransformer model.

  • d_model (int) – The dimension of the model.

  • n_heads (int) – The number of heads in each layer of NonstationaryTransformer.

  • d_ffn (int) – The dimension of the feed-forward network.

  • d_projector_hidden (list) – The dimensions of hidden layers in MLP projectors. It should be a list of integers and the length of the list should be equal to n_projector_hidden_layers.

  • n_projector_hidden_layers (int) – The number of hidden layers in MLP projectors.

  • dropout (float) – The dropout rate for the model.

  • ORT_weight (float) – The weight for the ORT loss, the same as SAITS.

  • MIT_weight (float) – The weight for the MIT loss, the same as SAITS.

  • batch_size (int) – The batch size for training and evaluating the model.

  • epochs (int) – The number of epochs for training the model.

  • patience (Optional[int]) – The patience for the early-stopping mechanism. Given a positive integer, the training process will be stopped when the model does not perform better after that number of epochs. Leaving it default as None will disable the early-stopping.

  • optimizer (Optional[Optimizer]) – The optimizer for model training. If not given, will use a default Adam optimizer.

  • 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.

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

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

Make predictions for the input data with the trained model.

Parameters:
  • test_set (dict or 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 (time 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:

The dictionary containing the clustering results and latent variables if necessary.

Return type:

file_type

impute(X, file_type='hdf5')[source]

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

Warning

The method impute is deprecated. Please use predict() instead.

Parameters:
  • X (Union[dict, str]) – The data samples for testing, should be array-like of shape [n_samples, sequence length (time 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 (time 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.imputation.film

The package of the partially-observed time-series imputation model FiLM.

Refer to the paper Zhou, Tian, Ziqing Ma, Qingsong Wen, Liang Sun, Tao Yao, Wotao Yin, and Rong Jin. “FiLM: Frequency improved legendre memory model for long-term time series forecasting.” In Advances in Neural Information Processing Systems 35 (2022): 12677-12690.

Notes

This implementation is inspired by the official one https://github.com/tianzhou2011/FiLM

class pypots.imputation.film.FiLM(n_steps, n_features, window_size, multiscale, modes1=32, dropout=0.5, mode_type=0, d_model=128, ORT_weight=1, MIT_weight=1, batch_size=32, epochs=100, patience=None, optimizer=<pypots.optim.adam.Adam object>, num_workers=0, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseNNImputer

The PyTorch implementation of the FiLM model. FiLM is originally proposed by Zhou et al. in [18].

Parameters:
  • n_steps (int) – The number of time steps in the time-series data sample.

  • n_features (int) – The number of features in the time-series data sample.

  • window_size (list) – A list including the window sizes for the HiPPO projection layers.

  • multiscale (list) – A list including the multiscale factors for the HiPPO projection layers.

  • dropout (float) – The dropout ratio for the HiPPO projection layers. It only works when mode_type == 1.

  • mode_type (int) – The mode type of the SpectralConv1d layers. It has to be one of [0, 1, 2].

  • d_model (int) – The dimension of the model.

  • ORT_weight (float) – The weight for the ORT loss, the same as SAITS.

  • MIT_weight (float) – The weight for the MIT loss, the same as SAITS.

  • batch_size (int) – The batch size for training and evaluating the model.

  • epochs (int) – The number of epochs for training the model.

  • patience (Optional[int]) – The patience for the early-stopping mechanism. Given a positive integer, the training process will be stopped when the model does not perform better after that number of epochs. Leaving it default as None will disable the early-stopping.

  • optimizer (Optional[Optimizer]) – The optimizer for model training. If not given, will use a default Adam optimizer.

  • 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.

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

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

Make predictions for the input data with the trained model.

Parameters:
  • test_set (dict or 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:

The dictionary containing the clustering results and latent variables if necessary.

Return type:

file_type

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.imputation.revin_scinet

The package of the partially-observed time-series imputation model FiLM.

Refer to the paper Zhou, Tian, Ziqing Ma, Qingsong Wen, Liang Sun, Tao Yao, Wotao Yin, and Rong Jin. “FiLM: Frequency improved legendre memory model for long-term time series forecasting.” In Advances in Neural Information Processing Systems 35 (2022): 12677-12690.

Notes

This implementation is inspired by the official one https://github.com/tianzhou2011/FiLM

class pypots.imputation.film.FiLM(n_steps, n_features, window_size, multiscale, modes1=32, dropout=0.5, mode_type=0, d_model=128, ORT_weight=1, MIT_weight=1, batch_size=32, epochs=100, patience=None, optimizer=<pypots.optim.adam.Adam object>, num_workers=0, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseNNImputer

The PyTorch implementation of the FiLM model. FiLM is originally proposed by Zhou et al. in [18].

Parameters:
  • n_steps (int) – The number of time steps in the time-series data sample.

  • n_features (int) – The number of features in the time-series data sample.

  • window_size (list) – A list including the window sizes for the HiPPO projection layers.

  • multiscale (list) – A list including the multiscale factors for the HiPPO projection layers.

  • dropout (float) – The dropout ratio for the HiPPO projection layers. It only works when mode_type == 1.

  • mode_type (int) – The mode type of the SpectralConv1d layers. It has to be one of [0, 1, 2].

  • d_model (int) – The dimension of the model.

  • ORT_weight (float) – The weight for the ORT loss, the same as SAITS.

  • MIT_weight (float) – The weight for the MIT loss, the same as SAITS.

  • batch_size (int) – The batch size for training and evaluating the model.

  • epochs (int) – The number of epochs for training the model.

  • patience (Optional[int]) – The patience for the early-stopping mechanism. Given a positive integer, the training process will be stopped when the model does not perform better after that number of epochs. Leaving it default as None will disable the early-stopping.

  • optimizer (Optional[Optimizer]) – The optimizer for model training. If not given, will use a default Adam optimizer.

  • 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.

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

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

Make predictions for the input data with the trained model.

Parameters:
  • test_set (dict or 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:

The dictionary containing the clustering results and latent variables if necessary.

Return type:

file_type

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.imputation.pyraformer

The package of the partially-observed time-series imputation model Pyraformer.

Refer to the paper Shizhan Liu, Hang Yu, Cong Liao, Jianguo Li, Weiyao Lin, Alex X. Liu, and Schahram Dustdar. “Pyraformer: Low-Complexity Pyramidal Attention for Long-Range Time Series Modeling and Forecasting”. International Conference on Learning Representations. 2022.

Notes

This implementation is inspired by the official one https://github.com/ant-research/Pyraformer

class pypots.imputation.pyraformer.Pyraformer(n_steps, n_features, n_layers, d_model, n_heads, d_ffn, window_size, inner_size, dropout=0, attn_dropout=0, ORT_weight=1, MIT_weight=1, batch_size=32, epochs=100, patience=None, optimizer=<pypots.optim.adam.Adam object>, num_workers=0, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseNNImputer

The PyTorch implementation of the Pyraformer model. Pyraformer is originally proposed by Liu et al. in [20].

Parameters:
  • n_steps (int) – The number of time steps in the time-series data sample.

  • n_features (int) – The number of features in the time-series data sample.

  • n_layers (int) – The number of layers in the Pyraformer model.

  • d_model (int) – The dimension of the model.

  • n_heads (int) – The number of heads in each layer of Pyraformer.

  • d_ffn (int) – The dimension of the feed-forward network.

  • window_size (list) – The downsample window size in pyramidal attention.

  • inner_size (int) – The size of neighbour attention

  • dropout (float) – The dropout rate for the model.

  • attn_dropout (float) – The dropout rate for the attention mechanism.

  • ORT_weight (float) – The weight for the ORT loss, the same as SAITS.

  • MIT_weight (float) – The weight for the MIT loss, the same as SAITS.

  • batch_size (int) – The batch size for training and evaluating the model.

  • epochs (int) – The number of epochs for training the model.

  • patience (Optional[int]) – The patience for the early-stopping mechanism. Given a positive integer, the training process will be stopped when the model does not perform better after that number of epochs. Leaving it default as None will disable the early-stopping.

  • optimizer (Optional[Optimizer]) – The optimizer for model training. If not given, will use a default Adam optimizer.

  • 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.

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

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

Make predictions for the input data with the trained model.

Parameters:
  • test_set (dict or 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:

The dictionary containing the clustering results and latent variables if necessary.

Return type:

file_type

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.imputation.fedformer

The package of the partially-observed time-series imputation model FEDformer.

Refer to the paper Tian Zhou, Ziqing Ma, Qingsong Wen, Xue Wang, Liang Sun, and Rong Jin. FEDformer: Frequency enhanced decomposed transformer for long-term series forecasting. In ICML, volume 162 of Proceedings of Machine Learning Research, pages 27268–27286. PMLR, 17–23 Jul 2022.

Notes

This implementation is inspired by the official one https://github.com/MAZiqing/FEDformer

class pypots.imputation.fedformer.FEDformer(n_steps, n_features, n_layers, d_model, n_heads, d_ffn, moving_avg_window_size, dropout=0, version='Fourier', modes=32, mode_select='random', ORT_weight=1, MIT_weight=1, batch_size=32, epochs=100, patience=None, optimizer=<pypots.optim.adam.Adam object>, num_workers=0, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseNNImputer

The PyTorch implementation of the FEDformer model. FEDformer is originally proposed by Woo et al. in [22].

Parameters:
  • n_steps (int) – The number of time steps in the time-series data sample.

  • n_features (int) – The number of features in the time-series data sample.

  • n_layers (int) – The number of layers in the FEDformer.

  • n_heads (int) – The number of heads in the multi-head attention mechanism.

  • d_model (int) – The dimension of the model.

  • d_ffn (int) – The dimension of the feed-forward network.

  • moving_avg_window_size (int) – The window size of moving average.

  • dropout (float) – The dropout rate for the model.

  • version – The version of the model. It has to be one of [“Wavelets”, “Fourier”]. The default value is “Fourier”.

  • modes – The number of modes to be selected. The default value is 32.

  • mode_select – Get modes on frequency domain. It has to “random” or “low”. The default value is “random”. ‘random’ means sampling randomly; ‘low’ means sampling the lowest modes;

  • ORT_weight (float) – The weight for the ORT loss, the same as SAITS.

  • MIT_weight (float) – The weight for the MIT loss, the same as SAITS.

  • batch_size (int) – The batch size for training and evaluating the model.

  • epochs (int) – The number of epochs for training the model.

  • patience (Optional[int]) – The patience for the early-stopping mechanism. Given a positive integer, the training process will be stopped when the model does not perform better after that number of epochs. Leaving it default as None will disable the early-stopping.

  • optimizer (Optional[Optimizer]) – The optimizer for model training. If not given, will use a default Adam optimizer.

  • 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.

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

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

Make predictions for the input data with the trained model.

Parameters:
  • test_set (dict or 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:

The dictionary containing the clustering results and latent variables if necessary.

Return type:

file_type

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.imputation.informer

The package of the partially-observed time-series imputation model Informer.

Refer to the paper Haoyi Zhou, Shanghang Zhang, Jieqi Peng, Shuai Zhang, Jianxin Li, Hui Xiong, and Wancai Zhang. Informer: Beyond efficient transformer for long sequence time-series forecasting. In Proceedings of the AAAI conference on artificial intelligence, volume 35, pages 11106–11115, 2021.

Notes

This implementation is inspired by the official one https://github.com/zhouhaoyi/Informer2020

class pypots.imputation.informer.Informer(n_steps, n_features, n_layers, d_model, n_heads, d_ffn, factor, dropout=0, ORT_weight=1, MIT_weight=1, batch_size=32, epochs=100, patience=None, optimizer=<pypots.optim.adam.Adam object>, num_workers=0, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseNNImputer

The PyTorch implementation of the Informer model. Informer is originally proposed by Wu et al. in [25].

Parameters:
  • n_steps (int) – The number of time steps in the time-series data sample.

  • n_features (int) – The number of features in the time-series data sample.

  • n_layers (int) – The number of layers in the Informer model.

  • d_model (int) – The dimension of the model.

  • n_heads (int) – The number of heads in each layer of Informer.

  • d_ffn (int) – The dimension of the feed-forward network.

  • factor (int) – The factor of the ProbSparse self-attention mechanism for the Informer model.

  • dropout (float) – The dropout rate for the model.

  • ORT_weight (float) – The weight for the ORT loss, the same as SAITS.

  • MIT_weight (float) – The weight for the MIT loss, the same as SAITS.

  • batch_size (int) – The batch size for training and evaluating the model.

  • epochs (int) – The number of epochs for training the model.

  • patience (Optional[int]) – The patience for the early-stopping mechanism. Given a positive integer, the training process will be stopped when the model does not perform better after that number of epochs. Leaving it default as None will disable the early-stopping.

  • optimizer (Optional[Optimizer]) – The optimizer for model training. If not given, will use a default Adam optimizer.

  • 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.

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

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

Make predictions for the input data with the trained model.

Parameters:
  • test_set (dict or 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:

The dictionary containing the clustering results and latent variables if necessary.

Return type:

file_type

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.imputation.autoformer

The package of the partially-observed time-series imputation model Autoformer.

Refer to the paper Haixu Wu, Jiehui Xu, Jianmin Wang, and Mingsheng Long. Autoformer: Decomposition transformers with autocorrelation for long-term series forecasting. In Advances in Neural Information Processing Systems, volume 34, pages 22419–22430. Curran Associates, Inc., 2021.

Notes

This implementation is inspired by the official one https://github.com/thuml/Autoformer

class pypots.imputation.autoformer.Autoformer(n_steps, n_features, n_layers, d_model, n_heads, d_ffn, factor, moving_avg_window_size, dropout=0, ORT_weight=1, MIT_weight=1, batch_size=32, epochs=100, patience=None, optimizer=<pypots.optim.adam.Adam object>, num_workers=0, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseNNImputer

The PyTorch implementation of the Autoformer model. Autoformer is originally proposed by Wu et al. in [23].

Parameters:
  • n_steps (int) – The number of time steps in the time-series data sample.

  • n_features (int) – The number of features in the time-series data sample.

  • n_layers (int) – The number of layers in the Autoformer model.

  • d_model (int) – The dimension of the model.

  • n_heads (int) – The number of heads in each layer of Autoformer.

  • d_ffn (int) – The dimension of the feed-forward network.

  • factor (int) – The factor of the auto correlation mechanism for the Autoformer model.

  • moving_avg_window_size (int) – The window size of moving average.

  • dropout (float) – The dropout rate for the model.

  • ORT_weight (float) – The weight for the ORT loss, the same as SAITS.

  • MIT_weight (float) – The weight for the MIT loss, the same as SAITS.

  • batch_size (int) – The batch size for training and evaluating the model.

  • epochs (int) – The number of epochs for training the model.

  • patience (Optional[int]) – The patience for the early-stopping mechanism. Given a positive integer, the training process will be stopped when the model does not perform better after that number of epochs. Leaving it default as None will disable the early-stopping.

  • optimizer (Optional[Optimizer]) – The optimizer for model training. If not given, will use a default Adam optimizer.

  • 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.

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

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

Make predictions for the input data with the trained model.

Parameters:
  • test_set (dict or 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:

The dictionary containing the clustering results and latent variables if necessary.

Return type:

file_type

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.imputation.csdi

The implementation of CSDI for the partially-observed time-series imputation task.

Refer to the paper Yusuke Tashiro, Jiaming Song, Yang Song, and Stefano Ermon. CSDI: Conditional Score-based Diffusion Models for Probabilistic Time Series Imputation. In NeurIPS, 2021.

Notes

This implementation is inspired by the official one the official implementation https://github.com/ermongroup/CSDI.

class pypots.imputation.csdi.CSDI(n_steps, n_features, n_layers, n_heads, n_channels, d_time_embedding, d_feature_embedding, d_diffusion_embedding, n_diffusion_steps=50, target_strategy='random', is_unconditional=False, schedule='quad', beta_start=0.0001, beta_end=0.5, batch_size=32, epochs=100, patience=None, optimizer=<pypots.optim.adam.Adam object>, num_workers=0, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseNNImputer

The PyTorch implementation of the CSDI model [24].

Parameters:
  • n_steps (int) – The number of time steps in the time-series data sample.

  • n_features (int) – The number of features in the time-series data sample.

  • n_layers (int) – The number of layers in the CSDI model.

  • n_heads (int) – The number of heads in the multi-head attention mechanism.

  • n_channels (int) – The number of residual channels.

  • d_time_embedding (int) – The dimension number of the time (temporal) embedding.

  • d_feature_embedding (int) – The dimension number of the feature embedding.

  • d_diffusion_embedding (int) – The dimension number of the diffusion embedding.

  • is_unconditional (bool) – Whether the model is unconditional or conditional.

  • target_strategy (str) – The strategy for selecting the target for the diffusion process. It has to be one of [“mix”, “random”].

  • n_diffusion_steps (int) – The number of the diffusion step T in the original paper.

  • schedule (str) – The schedule for other noise levels. It has to be one of [“quad”, “linear”].

  • beta_start (float) – The minimum noise level.

  • beta_end (float) – The maximum noise level.

  • batch_size (int) – The batch size for training and evaluating the model.

  • epochs (int) – The number of epochs for training the model.

  • patience (Optional[int]) – The patience for the early-stopping mechanism. Given a positive integer, the training process will be stopped when the model does not perform better after that number of epochs. Leaving it default as None will disable the early-stopping.

  • optimizer (Optional[Optimizer]) – The optimizer for model training. If not given, will use a default Adam optimizer.

  • 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.

fit(train_set, val_set=None, file_type='hdf5', n_sampling_times=1)[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

predict(test_set, file_type='hdf5', n_sampling_times=1)[source]
Parameters:
  • test_set (dict or str) – 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 test_set is a path string.

  • n_sampling_times (int) – The number of sampling times for the model to sample from the diffusion process.

Returns:

result_dict – Prediction results in a Python Dictionary for the given samples. It should be a dictionary including a key named ‘imputation’.

Return type:

dict

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.imputation.usgan

The package of the partially-observed time-series imputation method USGAN.

Refer to the paper Xiaoye Miao, Yangyang Wu, Jun Wang, Yunjun Gao, Xudong Mao, and Jianwei Yin. Generative Semi-supervised Learning for Multivariate Time Series Imputation. In AAAI, 35(10):8983–8991, May 2021.

class pypots.imputation.usgan.USGAN(n_steps, n_features, rnn_hidden_size, lambda_mse=1, hint_rate=0.7, dropout=0.0, G_steps=1, D_steps=1, batch_size=32, epochs=100, patience=None, G_optimizer=<pypots.optim.adam.Adam object>, D_optimizer=<pypots.optim.adam.Adam object>, num_workers=0, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseNNImputer

The PyTorch implementation of the USGAN model. Refer to [26].

Parameters:
  • n_steps (int) – The number of time steps in the time-series data sample.

  • n_features (int) – The number of features in the time-series data sample.

  • rnn_hidden_size (int) – The hidden size of the RNN cell

  • lambda_mse (float) – The weight of the reconstruction loss

  • hint_rate (float) – The hint rate for the discriminator

  • dropout (float) – The dropout rate for the last layer in Discriminator

  • G_steps (int) – The number of steps to train the generator in each iteration.

  • D_steps (int) – The number of steps to train the discriminator in each iteration.

  • batch_size (int) – The batch size for training and evaluating the model.

  • epochs (int) – The number of epochs for training the model.

  • patience (int) – The patience for the early-stopping mechanism. Given a positive integer, the training process will be stopped when the model does not perform better after that number of epochs. Leaving it default as None will disable the early-stopping.

  • G_optimizer (pypots.optim.Optimizer) – The optimizer for the generator training. If not given, will use a default Adam optimizer.

  • D_optimizer (pypots.optim.Optimizer) – The optimizer for the discriminator training. If not given, will use a default Adam optimizer.

  • 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, torch.device, list]) – 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 (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 (str) – The strategy to save model checkpoints. It has to be one of [None, “best”, “better”]. 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.

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

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

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.imputation.stemgnn

The package of the partially-observed time-series imputation model StemGNN.

Refer to the paper Defu Cao, Yujing Wang, Juanyong Duan, Ce Zhang, Xia Zhu, Congrui Huang, Yunhai Tong, Bixiong Xu, Jing Bai, Jie Tong, Qi Zhang. “Spectral Temporal Graph Neural Network for Multivariate Time-series Forecasting”. In Advances in Neural Information Processing Systems, 2020.

Notes

This implementation is inspired by the official one https://github.com/microsoft/StemGNN

class pypots.imputation.stemgnn.StemGNN(n_steps, n_features, n_layers, n_stacks, d_model, dropout=0.5, leaky_rate=0.2, ORT_weight=1, MIT_weight=1, batch_size=32, epochs=100, patience=None, optimizer=<pypots.optim.adam.Adam object>, num_workers=0, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseNNImputer

The PyTorch implementation of the StemGNN model. StemGNN is originally proposed by Cao et al. in [29].

Parameters:
  • n_steps (int) – The number of time steps in the time-series data sample.

  • n_features (int) – The number of features in the time-series data sample.

  • n_layers (int) – The number of layers in each block stack of the StemGNN model.

  • n_stacks (int) – The number of stacks in the StemGNN model.

  • d_model (int) – The dimension of the embedding layer and the model.

  • dropout (float) – The dropout rate for the model.

  • (leaky_rate) – The leaky rate for the model.

  • ORT_weight (float) – The weight for the ORT loss, the same as SAITS.

  • MIT_weight (float) – The weight for the MIT loss, the same as SAITS.

  • batch_size (int) – The batch size for training and evaluating the model.

  • epochs (int) – The number of epochs for training the model.

  • patience (Optional[int]) – The patience for the early-stopping mechanism. Given a positive integer, the training process will be stopped when the model does not perform better after that number of epochs. Leaving it default as None will disable the early-stopping.

  • optimizer (Optional[Optimizer]) – The optimizer for model training. If not given, will use a default Adam optimizer.

  • 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.

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

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

Make predictions for the input data with the trained model.

Parameters:
  • test_set (dict or 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:

The dictionary containing the clustering results and latent variables if necessary.

Return type:

file_type

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.imputation.reformer

The package of the partially-observed time-series imputation model Reformer.

Refer to the paper Kitaev, Nikita, Łukasz Kaiser, and Anselm Levskaya. Reformer: The Efficient Transformer. International Conference on Learning Representations, 2020.

Notes

This implementation is inspired by the official one https://github.com/google/trax/tree/master/trax/models/reformer and https://github.com/lucidrains/reformer-pytorch

class pypots.imputation.reformer.Reformer(n_steps, n_features, n_layers, d_model, n_heads, bucket_size, n_hashes, causal, d_ffn, dropout=0, ORT_weight=1, MIT_weight=1, batch_size=32, epochs=100, patience=None, optimizer=<pypots.optim.adam.Adam object>, num_workers=0, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseNNImputer

The PyTorch implementation of the Reformer model. Reformer is originally proposed by Kitaev et al. in [30].

Parameters:
  • n_steps (int) – The number of time steps in the time-series data sample.

  • n_features (int) – The number of features in the time-series data sample.

  • n_layers (int) – The number of layers in the Reformer model.

  • d_model (int) – The dimension of the model.

  • n_heads (int) – The number of heads in each layer of Reformer.

  • bucket_size (int) – Average size of qk per bucket, 64 was recommended in paper.

  • n_hashes (int) – 4 is permissible per author, 8 is the best but slower.

  • causal (bool) – Auto-regressive or not.

  • d_ffn (int) – The dimension of the feed-forward network. The window size of moving average.

  • dropout (float) – The dropout rate for the model.

  • ORT_weight (float) – The weight for the ORT loss, the same as SAITS.

  • MIT_weight (float) – The weight for the MIT loss, the same as SAITS.

  • batch_size (int) – The batch size for training and evaluating the model.

  • epochs (int) – The number of epochs for training the model.

  • patience (Optional[int]) – The patience for the early-stopping mechanism. Given a positive integer, the training process will be stopped when the model does not perform better after that number of epochs. Leaving it default as None will disable the early-stopping.

  • optimizer (Optional[Optimizer]) – The optimizer for model training. If not given, will use a default Adam optimizer.

  • 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.

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

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

Make predictions for the input data with the trained model.

Parameters:
  • test_set (dict or 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:

The dictionary containing the clustering results and latent variables if necessary.

Return type:

file_type

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.imputation.gpvae

The package of the partially-observed time-series imputation model GP-VAE.

Refer to the paper Vincent Fortuin, Dmitry Baranchuk, Gunnar Rätsch, and Stephan Mandt. GP-VAE: Deep probabilistic time series imputation. In International conference on artificial intelligence and statistics, pages 1651–1661. PMLR, 2020.

Notes

This implementation is inspired by the official one https://github.com/ratschlab/GP-VAE

class pypots.imputation.gpvae.GPVAE(n_steps, n_features, latent_size, encoder_sizes=(64, 64), decoder_sizes=(64, 64), kernel='cauchy', beta=0.2, M=1, K=1, sigma=1.0, length_scale=7.0, kernel_scales=1, window_size=3, batch_size=32, epochs=100, patience=None, optimizer=<pypots.optim.adam.Adam object>, num_workers=0, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseNNImputer

The PyTorch implementation of the GPVAE model [31].

Parameters:
  • n_steps (int) – The number of time steps in the time-series data sample.

  • n_features (int) – The number of features in the time-series data sample.

  • latent_size (int,) – The feature dimension of the latent embedding

  • encoder_sizes (tuple,) – The tuple of the network size in encoder

  • decoder_sizes (tuple,) – The tuple of the network size in decoder

  • beta (float,) – The weight of KL divergence in ELBO.

  • M (int,) – The number of Monte Carlo samples for ELBO estimation during training.

  • K (int,) – The number of importance weights for IWAE model training loss.

  • kernel (str) – The type of kernel function chosen in the Gaussain Process Proir. [“cauchy”, “diffusion”, “rbf”, “matern”]

  • sigma (float,) – The scale parameter for a kernel function

  • length_scale (float,) – The length scale parameter for a kernel function

  • kernel_scales (int,) – The number of different length scales over latent space dimensions

  • window_size (int,) – Window size for the inference CNN.

  • batch_size (int) – The batch size for training and evaluating the model.

  • epochs (int) – The number of epochs for training the model.

  • patience (int) – The patience for the early-stopping mechanism. Given a positive integer, the training process will be stopped when the model does not perform better after that number of epochs. Leaving it default as None will disable the early-stopping.

  • optimizer (pypots.optim.base.Optimizer) – The optimizer for model training. If not given, will use a default Adam optimizer.

  • 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 (torch.device or list) – 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 (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 (str) – The strategy to save model checkpoints. It has to be one of [None, “best”, “better”]. 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.

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

predict(test_set, file_type='hdf5', n_sampling_times=1)[source]
Parameters:
  • test_set (dict or str) – 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 test_set is a path string.

  • n_sampling_times (int) – The number of sampling times for the model to produce predictions.

Returns:

result_dict – Prediction results in a Python Dictionary for the given samples. It should be a dictionary including a key named ‘imputation’.

Return type:

dict

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.imputation.brits

The package of the partially-observed time-series imputation model BRITS.

Refer to the paper Wei Cao, Dong Wang, Jian Li, Hao Zhou, Lei Li, and Yitan Li. BRITS: Bidirectional recurrent imputation for time series. In Advances in Neural Information Processing Systems, volume 31. Curran Associates, Inc., 2018.

Notes

This implementation is inspired by the official one https://github.com/caow13/BRITS The bugs in the original implementation are fixed here.

class pypots.imputation.brits.BRITS(n_steps, n_features, rnn_hidden_size, batch_size=32, epochs=100, patience=None, optimizer=<pypots.optim.adam.Adam object>, num_workers=0, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseNNImputer

The PyTorch implementation of the BRITS model [34].

Parameters:
  • n_steps (int) – The number of time steps in the time-series data sample.

  • n_features (int) – The number of features in the time-series data sample.

  • rnn_hidden_size (int) – The size of the RNN hidden state, also the number of hidden units in the RNN cell.

  • batch_size (int) – The batch size for training and evaluating the model.

  • epochs (int) – The number of epochs for training the model.

  • patience (Optional[int]) – The patience for the early-stopping mechanism. Given a positive integer, the training process will be stopped when the model does not perform better after that number of epochs. Leaving it default as None will disable the early-stopping.

  • optimizer (Optional[Optimizer]) – The optimizer for model training. If not given, will use a default Adam optimizer.

  • 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.

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

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

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.imputation.mrnn

The package of the partially-observed time-series imputation model M-RNN.

Refer to the paper Jinsung Yoon, William R. Zame, and Mihaela van der Schaar. Estimating missing data in temporal data streams using multi-directional recurrent neural networks. IEEE Transactions on Biomedical Engineering, 66(5):14771490, 2019.

Notes

This implementation is inspired by the official one https://github.com/jsyoon0823/MRNN and https://github.com/WenjieDu/SAITS

class pypots.imputation.mrnn.MRNN(n_steps, n_features, rnn_hidden_size, batch_size=32, epochs=100, patience=None, optimizer=<pypots.optim.adam.Adam object>, num_workers=0, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseNNImputer

The PyTorch implementation of the MRNN model [33].

Parameters:
  • n_steps (int) – The number of time steps in the time-series data sample.

  • n_features (int) – The number of features in the time-series data sample.

  • rnn_hidden_size (int) – The size of the RNN hidden state, also the number of hidden units in the RNN cell.

  • batch_size (int) – The batch size for training and evaluating the model.

  • epochs (int) – The number of epochs for training the model.

  • patience (Optional[int]) – The patience for the early-stopping mechanism. Given a positive integer, the training process will be stopped when the model does not perform better after that number of epochs. Leaving it default as None will disable the early-stopping.

  • optimizer (Optional[Optimizer]) – The optimizer for model training. If not given, will use a default Adam optimizer.

  • 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.

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

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

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.imputation.grud

The package of the partially-observed time-series imputation model GRU-D.

Refer to the paper Zhengping Che, Sanjay Purushotham, Kyunghyun Cho, David Sontag, and Yan Liu. Recurrent Neural Networks for Multivariate Time Series with Missing Values. Scientific Reports, 8(1):6085, April 2018.

Notes

This implementation is inspired by the official one https://github.com/PeterChe1990/GRU-D

class pypots.imputation.grud.GRUD(n_steps, n_features, rnn_hidden_size, batch_size=32, epochs=100, patience=None, optimizer=<pypots.optim.adam.Adam object>, num_workers=0, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseNNImputer

The PyTorch implementation of the GRU-D model [35].

Parameters:
  • n_steps (int) – The number of time steps in the time-series data sample.

  • n_features (int) – The number of features in the time-series data sample.

  • rnn_hidden_size (int) – The size of the RNN hidden state.

  • batch_size (int) – The batch size for training and evaluating the model.

  • epochs (int) – The number of epochs for training the model.

  • patience (Optional[int]) – The patience for the early-stopping mechanism. Given a positive integer, the training process will be stopped when the model does not perform better after that number of epochs. Leaving it default as None will disable the early-stopping.

  • optimizer (Optional[Optimizer]) – The optimizer for model training. If not given, will use a default Adam optimizer.

  • 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.

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

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

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.imputation.tcn

The package of the partially-observed time-series imputation model TCN.

Refer to the paper Shaojie Bai, J. Zico Kolter, and Vladlen Koltun. “An Empirical Evaluation of Generic Convolutional and Recurrent Networks for Sequence Modeling”. arXiv preprint arXiv:1803.01271.

Notes

This implementation is inspired by the official one https://github.com/locuslab/TCN

class pypots.imputation.tcn.TCN(n_steps, n_features, n_levels, d_hidden, kernel_size, dropout=0, ORT_weight=1, MIT_weight=1, batch_size=32, epochs=100, patience=None, optimizer=<pypots.optim.adam.Adam object>, num_workers=0, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]

Bases: BaseNNImputer

The PyTorch implementation of the TCN model. TCN is originally proposed by Bai et al. in [36].

Parameters:
  • n_steps (int) – The number of time steps in the time-series data sample.

  • n_features (int) – The number of features in the time-series data sample.

  • n_levels (int) – The number of levels in the TCN model.

  • d_hidden (int) – The hidden dimension for the TCN model.

  • kernel_size (int) – The kernel size for the TCN model.

  • dropout (float) – The dropout rate for the model.

  • ORT_weight (float) – The weight for the ORT loss, the same as SAITS.

  • MIT_weight (float) – The weight for the MIT loss, the same as SAITS.

  • batch_size (int) – The batch size for training and evaluating the model.

  • epochs (int) – The number of epochs for training the model.

  • patience (Optional[int]) – The patience for the early-stopping mechanism. Given a positive integer, the training process will be stopped when the model does not perform better after that number of epochs. Leaving it default as None will disable the early-stopping.

  • optimizer (Optional[Optimizer]) – The optimizer for model training. If not given, will use a default Adam optimizer.

  • 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.

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

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

Make predictions for the input data with the trained model.

Parameters:
  • test_set (dict or 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:

The dictionary containing the clustering results and latent variables if necessary.

Return type:

file_type

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.imputation.lerp

The package of the partially-observed time-series imputation method linear interpolation.

class pypots.imputation.lerp.Lerp[source]

Bases: BaseImputer

Linear interpolation (Lerp) imputation method.

Lerp will linearly interpolate missing values between the nearest non-missing values. If there are missing values at the beginning or end of the series, they will be back-filled or forward-filled with the nearest non-missing value, respectively. If an entire series is empty, all ‘nan’ values will be filled with zeros.

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

Train the imputer on the given data. :rtype: None

Warning

Linear interpolation class does not need to run fit(). Please run func predict() directly.

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

Make predictions for the input data with the trained model.

Parameters:
  • test_set (dict or 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:

result_dict – 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:

dict

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.imputation.locf

The package of the partially-observed time-series imputation method LOCF.

class pypots.imputation.locf.LOCF(first_step_imputation='zero', device=None)[source]

Bases: BaseImputer

LOCF (Last Observed Carried Forward) imputation method. A naive imputation method that fills missing values with the last observed value. When time-series data gets inverse on the time dimension, this method can also be seen as NOCB (Next Observation Carried Backward). Simple but commonly used in practice.

Parameters:

first_step_imputation (str, default='backward') – With LOCF, the observed values are carried forward to impute the missing ones. But if the first value is missing, there is no value to carry forward. This parameter is used to determine the strategy to impute the missing values at the beginning of the time-series sequence after LOCF is applied. It can be one of [‘backward’, ‘zero’, ‘median’, ‘nan’]. If ‘nan’, the missing values at the sequence beginning will be left as NaNs. If ‘zero’, the missing values at the sequence beginning will be imputed with 0. If ‘backward’, the missing values at the beginning of the time-series sequence will be imputed with the first observed value in the sequence, i.e. the first observed value will be carried backward to impute the missing values at the beginning of the sequence. This method is also known as NOCB (Next Observation Carried Backward). If ‘median’, the missing values at the sequence beginning will be imputed with the overall median values of features in the dataset. If first_step_imputation is not “nan”, if missing values still exist (this is usually caused by whole feature missing) after applying first_step_imputation, they will be filled with 0.

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

Train the imputer on the given data. :rtype: None

Warning

LOCF does not need to run fit(). Please run func predict() directly.

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

Make predictions for the input data with the trained model.

Parameters:
  • test_set (dict or 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:

result_dict – 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:

dict

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.imputation.locf.locf_numpy(X, first_step_imputation='backward')[source]

Numpy implementation of LOCF.

Parameters:
  • X (np.ndarray,) – Time series containing missing values (NaN) to be imputed.

  • first_step_imputation (str, default='backward') – With LOCF, the observed values are carried forward to impute the missing ones. But if the first value is missing, there is no value to carry forward. This parameter is used to determine the strategy to impute the missing values at the beginning of the time-series sequence after LOCF is applied. It can be one of [‘backward’, ‘zero’, ‘median’, ‘nan’]. If ‘nan’, the missing values at the sequence beginning will be left as NaNs. If ‘zero’, the missing values at the sequence beginning will be imputed with 0. If ‘backward’, the missing values at the beginning of the time-series sequence will be imputed with the first observed value in the sequence, i.e. the first observed value will be carried backward to impute the missing values at the beginning of the sequence. This method is also known as NOCB (Next Observation Carried Backward). If ‘median’, the missing values at the sequence beginning will be imputed with the overall median values of features in the dataset. If first_step_imputation is not “nan”, if missing values still exist (this is usually caused by whole feature missing) after applying first_step_imputation, they will be filled with 0.

Returns:

X_imputed – Imputed time series.

Return type:

array,

Notes

This implementation gets inspired by the question on StackOverflow: https://stackoverflow.com/questions/41190852/most-efficient-way-to-forward-fill-nan-values-in-numpy-array

pypots.imputation.locf.locf_torch(X, first_step_imputation='backward')[source]

Torch implementation of LOCF.

Parameters:
  • X (tensor,) – Time series containing missing values (NaN) to be imputed.

  • first_step_imputation (str, default='backward') – With LOCF, the observed values are carried forward to impute the missing ones. But if the first value is missing, there is no value to carry forward. This parameter is used to determine the strategy to impute the missing values at the beginning of the time-series sequence after LOCF is applied. It can be one of [‘backward’, ‘zero’, ‘median’, ‘nan’]. If ‘nan’, the missing values at the sequence beginning will be left as NaNs. If ‘zero’, the missing values at the sequence beginning will be imputed with 0. If ‘backward’, the missing values at the beginning of the time-series sequence will be imputed with the first observed value in the sequence, i.e. the first observed value will be carried backward to impute the missing values at the beginning of the sequence. This method is also known as NOCB (Next Observation Carried Backward). If ‘median’, the missing values at the sequence beginning will be imputed with the overall median values of features in the dataset. If first_step_imputation is not “nan”, if missing values still exist (this is usually caused by whole feature missing) after applying first_step_imputation, they will be filled with 0.

Returns:

X_imputed – Imputed time series.

Return type:

tensor,

pypots.imputation.median

The package of the partially-observed time-series imputation method Median.

class pypots.imputation.median.Median[source]

Bases: BaseImputer

Median value imputation method.

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

Train the imputer on the given data. :rtype: None

Warning

Median imputation class does not need to run fit(). Please run func predict() directly.

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

Make predictions for the input data with the trained model.

Parameters:
  • test_set (dict or 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:

result_dict – 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:

dict

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.imputation.mean

The package of the partially-observed time-series imputation method Median.

class pypots.imputation.mean.Mean[source]

Bases: BaseImputer

Mean value imputation method.

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

Train the imputer on the given data. :rtype: None

Warning

Mean imputation class does not need to run fit(). Please run func predict() directly.

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

Make predictions for the input data with the trained model.

Parameters:
  • test_set (dict or 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:

result_dict – 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:

dict

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