pypots.imputation package#

pypots.imputation.saits#

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

Refer to the paper “Du, W., Cote, D., & Liu, Y. (2023). SAITS: Self-Attention-based Imputation for Time Series. Expert systems with applications.”

class pypots.imputation.saits.SAITS(n_steps, n_features, n_layers, d_model, d_inner, n_heads, d_k, d_v, 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')[source]#

Bases: BaseNNImputer

The PyTorch implementation of the SAITS model [2].

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.

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

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

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

References

fit(train_set, val_set=None, file_type='h5py')[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 (time 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 (time 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, default = "h5py",) – The type of the given file if train_set and val_set are path strings.

Return type:

None

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

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

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

Return type:

dict,

impute(X, file_type='h5py')[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 – 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).

load_model(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).

Warning

The method load_model is deprecated. Please use load() instead.

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

save_model(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

Warning

The method save_model is deprecated. Please use save() instead.

pypots.imputation.transformer#

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

Refer to the paper “Du, W., Cote, D., & Liu, Y. (2023). SAITS: Self-Attention-based Imputation for Time Series. Expert systems with applications.”

class pypots.imputation.transformer.Transformer(n_steps, n_features, n_layers, d_model, d_inner, n_heads, d_k, d_v, 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')[source]#

Bases: BaseNNImputer

The PyTorch implementation of the Transformer model. Transformer is originally proposed by Vaswani et al. in [3], and gets re-implemented as a time-series imputation model by Du et al. in [2]. Here you should refer to [2] 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.

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

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

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

References

fit(train_set, val_set=None, file_type='h5py')[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 (time 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 (time 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, default = "h5py",) – The type of the given file if train_set and val_set are path strings.

Return type:

None

predict(test_set, file_type='h5py')[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:

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(X, file_type='h5py')[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 – 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).

load_model(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).

Warning

The method load_model is deprecated. Please use load() instead.

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

save_model(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

Warning

The method save_model is deprecated. Please use save() instead.

pypots.imputation.csdi#

class pypots.imputation.csdi.CSDI(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')[source]#

Bases: BaseNNImputer

The PyTorch implementation of the CSDI model [6].

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

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

References

fit(train_set, val_set=None, file_type='h5py', 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 (time 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 (time 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, default = "h5py",) – The type of the given file if train_set and val_set are path strings.

Return type:

None

predict(test_set, file_type='h5py', 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 (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.

  • 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(X, file_type='h5py')[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 – 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).

load_model(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).

Warning

The method load_model is deprecated. Please use load() instead.

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

save_model(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

Warning

The method save_model is deprecated. Please use save() instead.

pypots.imputation.usgan#

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

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')[source]#

Bases: BaseNNImputer

The PyTorch implementation of the USGAN model. Refer to [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.

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

References

fit(train_set, val_set=None, file_type='h5py')[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 (time 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 (time 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, default = "h5py",) – The type of the given file if train_set and val_set are path strings.

Return type:

None

predict(test_set, file_type='h5py')[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:

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(X, file_type='h5py')[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 – 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).

load_model(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).

Warning

The method load_model is deprecated. Please use load() instead.

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

save_model(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

Warning

The method save_model is deprecated. Please use save() instead.

pypots.imputation.gpvae#

The package of the partially-observed time-series imputation method 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')[source]#

Bases: BaseNNImputer

The PyTorch implementation of the GPVAE model [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.

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

References

fit(train_set, val_set=None, file_type='h5py')[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 (time 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 (time 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, default = "h5py",) – The type of the given file if train_set and val_set are path strings.

Return type:

None

predict(test_set, file_type='h5py', 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 (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.

  • 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(X, file_type='h5py', n_sampling_times=1)[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 – 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).

load_model(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).

Warning

The method load_model is deprecated. Please use load() instead.

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

save_model(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

Warning

The method save_model is deprecated. Please use save() instead.

pypots.imputation.brits#

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

Refer to the paper “Cao, W., Wang, D., Li, J., Zhou, H., Li, L., & Li, Y. (2018). BRITS: Bidirectional Recurrent Imputation for Time Series. NeurIPS 2018.”

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')[source]#

Bases: BaseNNImputer

The PyTorch implementation of the BRITS model [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.

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

References

fit(train_set, val_set=None, file_type='h5py')[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 (time 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 (time 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, default = "h5py",) – The type of the given file if train_set and val_set are path strings.

Return type:

None

predict(test_set, file_type='h5py')[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:

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(X, file_type='h5py')[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 – 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).

load_model(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).

Warning

The method load_model is deprecated. Please use load() instead.

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

save_model(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

Warning

The method save_model is deprecated. Please use save() instead.

pypots.imputation.mrnn#

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')[source]#

Bases: BaseNNImputer

The PyTorch implementation of the MRNN model [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.

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

References

fit(train_set, val_set=None, file_type='h5py')[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 (time 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 (time 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, default = "h5py",) – The type of the given file if train_set and val_set are path strings.

Return type:

None

predict(test_set, file_type='h5py')[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:

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(X, file_type='h5py')[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 – 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).

load_model(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).

Warning

The method load_model is deprecated. Please use load() instead.

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

save_model(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

Warning

The method save_model is deprecated. Please use save() instead.

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. 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='h5py')[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='h5py')[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:

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(X, file_type='h5py')[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 – 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).

load_model(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).

Warning

The method load_model is deprecated. Please use load() instead.

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

save_model(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

Warning

The method save_model is deprecated. Please use save() instead.