cca_zoo.probabilistic

class cca_zoo.probabilistic.ProbabilisticCCA(latent_dimensions: int = 1, copy_data=True, random_state: int = 0, num_samples=100, num_warmup=100)[source]

Bases: BaseModel

A class used to fit a Probabilistic CCA model using variational inference.

Probabilistic CCA is a generative model that assumes each view of data is generated from a shared latent variable z and some view-specific parameters (mu: mean, psi: covariance, W: weight matrix). The model can be written as:

z ~ N(0, I) x_i ~ N(W_i z + mu_i, psi_i)

The model parameters and the latent variables are inferred using MCMC sampling with the NUTS algorithm.

Parameters:
  • latent_dimensions (int, optional) – Number of latent dimensions to use, by default 1

  • copy_data (bool, optional) – Whether to copy the data, by default True

  • random_state (int, optional) – Random state, by default 0

  • num_samples (int, optional) – Number of samples to use in MCMC, by default 100

  • num_warmup (int, optional) – Number of warmup samples to use in MCMC, by default 100

References

Bach, Francis R., and Michael I. Jordan. β€œA probabilistic interpretation of canonical correlation analysis.” (2005). Wang, Chong. β€œVariational Bayesian approach to canonical correlation analysis.” IEEE Transactions on Neural Networks 18.3 (2007): 905-910.

canonical_loadings(views: Iterable[ndarray], normalize: bool = True, **kwargs) List[ndarray]

Calculate canonical loadings for each view.

Canonical loadings represent the correlation between the original variables in a view and their respective canonical variates. Canonical variates are linear combinations of the original variables formed to maximize the correlation with canonical variates from another view.

Mathematically, given two views (X_i), canonical variates from the views are:

(Z_i = w_i^T X_i)

The canonical loading for a variable in (X_i) is the correlation between that variable and (Z_i).

Parameters:

views (list/tuple of numpy arrays) – Each array corresponds to a view. All views must have the same number of rows (observations).

Returns:

loadings – Canonical loadings for each view. High absolute values indicate that the respective original variables play a significant role in defining the canonical variate.

Return type:

list of numpy arrays

explained_covariance(views: Iterable[ndarray]) ndarray

Calculates the covariance matrix of the transformed components for each view.

Parameters:

views (list/tuple of numpy arrays or array likes with the same number of rows (samples)) –

Returns:

explained_covariances – Covariance matrices for the transformed components of each view.

Return type:

list of numpy arrays

explained_covariance_cumulative(views: Iterable[ndarray]) ndarray

Calculates the cumulative explained covariance ratio for each latent dimension for each view.

Returns:

cumulative_ratios

Return type:

list of numpy arrays

explained_variance(views: Iterable[ndarray]) List[ndarray]

Calculates the variance captured by each latent dimension for each view.

Returns:

transformed_vars

Return type:

list of numpy arrays

explained_variance_cumulative(views: Iterable[ndarray]) List[ndarray]

Calculates the cumulative explained variance ratio for each latent dimension for each view.

Returns:

cumulative_ratios

Return type:

list of numpy arrays

explained_variance_ratio(views: Iterable[ndarray]) List[ndarray]

Calculates the ratio of the variance captured by each latent dimension to the total variance for each view.

Returns:

explained_variance_ratios

Return type:

list of numpy arrays

fit(views: Iterable[ndarray], y=None)[source]

Infer the parameters and latent variables of the Probabilistic CCA model.

Parameters:

views (Iterable[np.ndarray]) – A list or tuple of numpy arrays or array likes with the same number of rows (samples)

Returns:

self – Returns the instance itself.

Return type:

object

fit_transform(views: Iterable[ndarray], **kwargs) List[ndarray]

Fits the model to the given data and returns the transformed views

Parameters:
  • views (list/tuple of numpy arrays or array likes with the same number of rows (samples)) –

  • kwargs (any additional keyword arguments required by the given model) –

Returns:

transformed_views

Return type:

list of numpy arrays

get_metadata_routing()

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns:

routing – A MetadataRequest encapsulating routing information.

Return type:

MetadataRequest

get_params(deep=True)

Get parameters for this estimator.

Parameters:

deep (bool, default=True) – If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:

params – Parameter names mapped to their values.

Return type:

dict

property loadings: List[ndarray]

Compute and return loadings for each view. These are cached for performance optimization.

In the context of the cca-zoo models, loadings are the normalized weights. Due to the structure of these models, weight vectors are normalized such that w’X’Xw = 1, as opposed to w’w = 1, which is commonly used in PCA. As a result, when computing the loadings, the weights are normalized to have unit norm, ensuring that the loadings range between -1 and 1.

It’s essential to differentiate between these loadings and canonical loadings. The latter are correlations between the original variables and their corresponding canonical variates.

Returns:

Loadings for each view.

Return type:

List[np.ndarray]

pairwise_correlations(views: Iterable[ndarray], **kwargs) ndarray

Returns the pairwise correlations between the views in each dimension

Parameters:
  • views (list/tuple of numpy arrays or array likes with the same number of rows (samples)) –

  • kwargs (any additional keyword arguments required by the given model) –

Returns:

pairwise_correlations

Return type:

numpy array of shape (n_views, n_views, latent_dimensions)

predict(views: Iterable[ndarray]) List[ndarray]

Predicts the missing view from the given views.

Parameters:

views (list/tuple of numpy arrays or array likes with the same number of rows (samples)) –

Returns:

predicted_views – Predicted views.

Return type:

list of numpy arrays. None if the view is missing.

Examples

>>> import numpy as np
>>> X1 = np.random.rand(100, 5)
>>> X2 = np.random.rand(100, 5)
>>> cca = CCA()
>>> cca.fit([X1, X2])
>>> X1_pred, X2_pred = cca.predict([X1, None])
score(views: Iterable[ndarray], y: Any | None = None, **kwargs) float

Returns the average pairwise correlation between the views

Parameters:
  • views (list/tuple of numpy arrays or array likes with the same number of rows (samples)) –

  • y (None) –

  • kwargs (any additional keyword arguments required by the given model) –

Returns:

score

Return type:

float

set_fit_request(*, views: bool | None | str = '$UNCHANGED$') ProbabilisticCCA

Request metadata passed to the fit method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a pipeline.Pipeline. Otherwise it has no effect.

Parameters:

views (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for views parameter in fit.

Returns:

self – The updated object.

Return type:

object

set_params(**params)

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters:

**params (dict) – Estimator parameters.

Returns:

self – Estimator instance.

Return type:

estimator instance

set_predict_request(*, views: bool | None | str = '$UNCHANGED$') ProbabilisticCCA

Request metadata passed to the predict method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to predict if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a pipeline.Pipeline. Otherwise it has no effect.

Parameters:

views (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for views parameter in predict.

Returns:

self – The updated object.

Return type:

object

set_score_request(*, views: bool | None | str = '$UNCHANGED$') ProbabilisticCCA

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a pipeline.Pipeline. Otherwise it has no effect.

Parameters:

views (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for views parameter in score.

Returns:

self – The updated object.

Return type:

object

set_transform_request(*, views: bool | None | str = '$UNCHANGED$') ProbabilisticCCA

Request metadata passed to the transform method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to transform if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to transform.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a pipeline.Pipeline. Otherwise it has no effect.

Parameters:

views (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for views parameter in transform.

Returns:

self – The updated object.

Return type:

object

transform(views: Iterable[ndarray], y=None)[source]

Predict the latent variables that generate the data in views using the sampled model parameters.

Parameters:

views (Iterable[np.ndarray]) – A list or tuple of numpy arrays or array likes with the same number of rows (samples)

Returns:

z – An array of shape (n_samples, latent_dimensions) containing the predicted latent variables for each sample.

Return type:

np.ndarray