cca_zoo.nonparametricο
- class cca_zoo.nonparametric.KCCA(latent_dimensions: int = 1, copy_data=True, random_state=None, c: Iterable[float] | float = None, eps=0.001, kernel: Iterable[str | float | callable] = None, gamma: Iterable[float] = None, degree: Iterable[float] = None, coef0: Iterable[float] = None, kernel_params: Iterable[dict] = None)[source]ο
Bases:
KernelMixin
,MCCA
A class used to fit KCCA model. This model extends MCCA to nonlinear relationships by using kernel functions on each view.
The objective function of KCCA is:
\[ \begin{align}\begin{aligned}\begin{split}\alpha_{opt}=\underset{\alpha}{\mathrm{argmax}}\{\sum_i\sum_{j\neq i} \alpha_i^TK_i^TK_j\alpha_j \}\\\end{split}\\\text{subject to:}\\c_i\alpha_i^TK_i\alpha_i + (1-c_i)\alpha_i^TK_i^TK_i\alpha_i=1\end{aligned}\end{align} \]where \(K_i\) are the kernel matrices for each view and \(c_i\) are the regularization parameters for each view.
- 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 seed for reproducibility, by default None
c (Union[Iterable[float], float], optional) β Regularization parameter or list of parameters for each view, by default None. If None, it will be set to zero for each view.
eps (float, optional) β Small value to add to the diagonal of the kernel matrices, by default 1e-3
kernel (Iterable[Union[float, callable]], optional) β Kernel function or list of functions for each view, by default None. If None, it will use a linear kernel for each view.
gamma (Iterable[float], optional) β Gamma parameter or list of parameters for the RBF kernel for each view, by default None. Ignored if kernel is not RBF.
degree (Iterable[float], optional) β Degree parameter or list of parameters for the polynomial kernel for each view, by default None. Ignored if kernel is not polynomial.
coef0 (Iterable[float], optional) β Coef0 parameter or list of parameters for the polynomial or sigmoid kernel for each view, by default None. Ignored if kernel is not polynomial or sigmoid.
kernel_params (Iterable[dict], optional) β Additional parameters or list of parameters for the kernel function for each view, by default None.
Examples
>>> from cca_zoo.linear import KCCA >>> import numpy as np >>> rng=np.random.RandomState(0) >>> X1 = rng.random((10,5)) >>> X2 = rng.random((10,5)) >>> X3 = rng.random((10,5)) >>> model = KCCA() >>> model.fit((X1,X2,X3)).score((X1,X2,X3)) array([0.96893666])
- 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, **kwargs)ο
Fits the model to the given data
- 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:
self
- Return type:
- 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.
- 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:
- set_fit_request(*, views: bool | None | str = '$UNCHANGED$') KCCA ο
Request metadata passed to the
fit
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed tofit
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it tofit
.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
. Otherwise it has no effect.
- 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$') KCCA ο
Request metadata passed to the
predict
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed topredict
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it topredict
.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
. Otherwise it has no effect.
- set_score_request(*, views: bool | None | str = '$UNCHANGED$') KCCA ο
Request metadata passed to the
score
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed toscore
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it toscore
.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
. Otherwise it has no effect.
- set_transform_request(*, views: bool | None | str = '$UNCHANGED$') KCCA ο
Request metadata passed to the
transform
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed totransform
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it totransform
.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
. Otherwise it has no effect.
- class cca_zoo.nonparametric.KGCCA(latent_dimensions: int = 1, copy_data=True, random_state=None, c: Iterable[float] | float = None, kernel: Iterable[float | callable] = None, gamma: Iterable[float] = None, degree: Iterable[float] = None, coef0: Iterable[float] = None, kernel_params: Iterable[dict] = None, view_weights: Iterable[float] = None, eps: float = 1e-06)[source]ο
Bases:
KernelMixin
,GCCA
A class used to fit KGCCA model. This model extends GCCA to nonlinear relationships by using kernel functions on each view.
The objective function of KGCCA is:
\[\]alpha_{opt}=underset{alpha}{mathrm{argmax}}{ sum_ialpha_i^TK_i^TT }\
text{subject to:}
T^TT=1
where \(K_i\) are the kernel matrices for each view and \(T\) is the auxiliary vector.
- 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 seed for reproducibility, by default None
c (Union[Iterable[float], float], optional) β Regularization parameter or list of parameters for each view, by default None. If None, it will be set to zero for each view.
kernel (Iterable[Union[float, callable]], optional) β Kernel function or list of functions for each view, by default None. If None, it will use a linear kernel for each view.
gamma (Iterable[float], optional) β Gamma parameter or list of parameters for the RBF kernel for each view, by default None. Ignored if kernel is not RBF.
degree (Iterable[float], optional) β Degree parameter or list of parameters for the polynomial kernel for each view, by default None. Ignored if kernel is not polynomial.
coef0 (Iterable[float], optional) β Coef0 parameter or list of parameters for the polynomial or sigmoid kernel for each view, by default None. Ignored if kernel is not polynomial or sigmoid.
kernel_params (Iterable[dict], optional) β Additional parameters or list of parameters for the kernel function for each view, by default None.
view_weights (Iterable[float], optional) β Weights for each view in the objective function, by default None. If None, it will use equal weights for each view.
References
Tenenhaus, Arthur, Cathy Philippe, and Vincent Frouin. βKernel generalized canonical correlation analysis.β Computational Statistics & Data Analysis 90 (2015): 114-131.
Examples
>>> from cca_zoo.linear import KGCCA >>> import numpy as np >>> rng=np.random.RandomState(0) >>> X1 = rng.random((10,5)) >>> X2 = rng.random((10,5)) >>> X3 = rng.random((10,5)) >>> model = KGCCA() >>> model.fit((X1,X2,X3)).score((X1,X2,X3)) array([0.97019284])
- 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, K=None, **kwargs)ο
Fits the model to the given data
- 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:
self
- Return type:
- 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.
- 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:
- set_fit_request(*, K: bool | None | str = '$UNCHANGED$', views: bool | None | str = '$UNCHANGED$') KGCCA ο
Request metadata passed to the
fit
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed tofit
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it tofit
.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
. Otherwise it has no effect.- Parameters:
- Returns:
self β The updated object.
- Return type:
- 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$') KGCCA ο
Request metadata passed to the
predict
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed topredict
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it topredict
.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
. Otherwise it has no effect.
- set_score_request(*, views: bool | None | str = '$UNCHANGED$') KGCCA ο
Request metadata passed to the
score
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed toscore
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it toscore
.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
. Otherwise it has no effect.
- set_transform_request(*, views: bool | None | str = '$UNCHANGED$') KGCCA ο
Request metadata passed to the
transform
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed totransform
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it totransform
.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
. Otherwise it has no effect.
- class cca_zoo.nonparametric.KTCCA(latent_dimensions: int = 1, copy_data=True, random_state=None, eps=0.001, c: Iterable[float] | float = None, kernel: Iterable[float | callable] = None, gamma: Iterable[float] = None, degree: Iterable[float] = None, coef0: Iterable[float] = None, kernel_params: Iterable[dict] = None)[source]ο
Bases:
KernelMixin
,TCCA
A class used to fit KTCCA model. This model extends TCCA to nonlinear relationships by using kernel functions on each view.
The objective function of KTCCA is:
\[ \begin{align}\begin{aligned}\begin{split}\alpha_{opt}=\underset{\alpha}{\mathrm{argmax}}\{ \alpha_1^TK_1^T\otimes \alpha_2^TK_2^T\otimes \cdots \otimes \alpha_m^TK_m^T\alpha \}\\\end{split}\\\text{subject to:}\\c_i\alpha_i^TK_i\alpha_i + (1-c_i)\alpha_i^TK_i^TK_i\alpha_i=1\end{aligned}\end{align} \]where \(K_i\) are the kernel matrices for each view and \(c_i\) are the regularization parameters for each view.
References
Kim, Tae-Kyun, Shu-Fai Wong, and Roberto Cipolla. βTensor canonical correlation analysis for action classification.β 2007 IEEE Conference on Computer Vision and Pattern Recognition. IEEE, 2007
Examples
>>> from cca_zoo.linear import KTCCA >>> rng=np.random.RandomState(0) >>> X1 = rng.random((10,5)) >>> X2 = rng.random((10,5)) >>> X3 = rng.random((10,5)) >>> model = KTCCA() >>> model.fit((X1,X2,X3)).score((X1,X2,X3)) array([1.69896269])
- 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
- correlations(views: Iterable[ndarray], **kwargs)ο
Predicts the correlation for the given data using the fit model
- 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
- 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, **kwargs)ο
Fits the model to the given data
- 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:
self
- Return type:
- 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.
- 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], **kwargs)ο
Returns the higher order correlations 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
- set_fit_request(*, views: bool | None | str = '$UNCHANGED$') KTCCA ο
Request metadata passed to the
fit
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed tofit
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it tofit
.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
. Otherwise it has no effect.
- 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$') KTCCA ο
Request metadata passed to the
predict
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed topredict
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it topredict
.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
. Otherwise it has no effect.
- set_score_request(*, views: bool | None | str = '$UNCHANGED$') KTCCA ο
Request metadata passed to the
score
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed toscore
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it toscore
.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
. Otherwise it has no effect.
- set_transform_request(*, views: bool | None | str = '$UNCHANGED$') KTCCA ο
Request metadata passed to the
transform
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed totransform
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it totransform
.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
. Otherwise it has no effect.
- class cca_zoo.nonparametric.NCCA(latent_dimensions: int = 1, copy_data=True, accept_sparse=False, random_state: int | RandomState = None, nearest_neighbors=None, gamma: Iterable[float] = None)[source]ο
Bases:
BaseModel
A class used to fit nonparametric (NCCA) model. This model extends CCA to nonlinear relationships by using local linear projections based on nearest neighbors.
- 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
accept_sparse (bool, optional) β Whether to accept sparse data as input, by default False
random_state (Union[int, np.random.RandomState], optional) β Random seed for reproducibility, by default None
nearest_neighbors (int, optional) β Number of nearest neighbors to use for local linear projections, by default None. If None, it will use the square root of the number of samples.
gamma (Iterable[float], optional) β Bandwidth parameter or list of parameters for the RBF kernel for each view, by default None. If None, it will use the median heuristic.
References
Michaeli, Tomer, Weiran Wang, and Karen Livescu. βNonparametric canonical correlation analysis.β International conference on machine learning. PMLR, 2016.
Example
>>> from cca_zoo.linear import NCCA >>> X1 = np.random.rand(10,5) >>> X2 = np.random.rand(10,5) >>> model = NCCA() >>> model.fit((X1,X2)).score((X1,X2)) array([1.])
- 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, **kwargs)[source]ο
Fits the model to the given data
- 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:
self
- Return type:
- 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.
- 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:
- set_fit_request(*, views: bool | None | str = '$UNCHANGED$') NCCA ο
Request metadata passed to the
fit
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed tofit
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it tofit
.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
. Otherwise it has no effect.
- 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$') NCCA ο
Request metadata passed to the
predict
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed topredict
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it topredict
.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
. Otherwise it has no effect.
- set_score_request(*, views: bool | None | str = '$UNCHANGED$') NCCA ο
Request metadata passed to the
score
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed toscore
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it toscore
.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
. Otherwise it has no effect.
- set_transform_request(*, views: bool | None | str = '$UNCHANGED$') NCCA ο
Request metadata passed to the
transform
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed totransform
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it totransform
.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
. Otherwise it has no effect.