From 6d9e8a9a8de62bad3114a3429f7b2de4034459e9 Mon Sep 17 00:00:00 2001 From: jameschapman19 Date: Thu, 26 Oct 2023 00:50:45 +0100 Subject: [PATCH] Validation set for Gradient Based Models woohoo --- cca_zoo/__init__.py | 2 ++ cca_zoo/linear/_tcca.py | 28 ++++++++++++++++++++-------- cca_zoo/nonparametric/_ncca.py | 2 +- tests/test_unregularized.py | 3 ++- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/cca_zoo/__init__.py b/cca_zoo/__init__.py index 2e7a7678..3928574c 100644 --- a/cca_zoo/__init__.py +++ b/cca_zoo/__init__.py @@ -6,6 +6,7 @@ visualisation, preprocessing, sequential, + nonparametric, ) __all__ = [ @@ -16,6 +17,7 @@ "visualisation", "preprocessing", "sequential", + "nonparametric", ] try: from . import probabilistic diff --git a/cca_zoo/linear/_tcca.py b/cca_zoo/linear/_tcca.py index 94a476e2..8f61fd5f 100644 --- a/cca_zoo/linear/_tcca.py +++ b/cca_zoo/linear/_tcca.py @@ -96,15 +96,27 @@ def correlations(self, views: Iterable[np.ndarray], **kwargs): corrs = multiplied_views / norms return corrs - def score(self, views: Iterable[np.ndarray], **kwargs): - """ - Returns the higher order correlations in each dimension + def average_pairwise_correlations( + self, views: Iterable[np.ndarray], **kwargs + ) -> np.ndarray: + transformed_views = self.transform(views, **kwargs) + transformed_views = [ + transformed_view - transformed_view.mean(axis=0) + for transformed_view in transformed_views + ] + multiplied_views = np.stack(transformed_views, axis=0).prod(axis=0).sum(axis=0) + norms = np.stack( + [ + np.linalg.norm(transformed_view, axis=0) + for transformed_view in transformed_views + ], + axis=0, + ).prod(axis=0) + corrs = multiplied_views / norms + return corrs - :param views: list/tuple of numpy arrays or array likes with the same number of rows (samples) - :param kwargs: any additional keyword arguments required by the given model - """ - dim_corrs = self.correlations(views, **kwargs) - return dim_corrs + def score(self, views: Iterable[np.ndarray], **kwargs): + return self.average_pairwise_correlations(views, **kwargs).mean() def _setup_tensor(self, views: Iterable[np.ndarray], **kwargs): covs = [ diff --git a/cca_zoo/nonparametric/_ncca.py b/cca_zoo/nonparametric/_ncca.py index 0b44a43f..c86a89ee 100644 --- a/cca_zoo/nonparametric/_ncca.py +++ b/cca_zoo/nonparametric/_ncca.py @@ -5,7 +5,7 @@ from sklearn.neighbors import NearestNeighbors from cca_zoo._base import BaseModel -from cca_zoo.utils.check_values import _process_parameter +from cca_zoo._utils.check_values import _process_parameter class NCCA(BaseModel): diff --git a/tests/test_unregularized.py b/tests/test_unregularized.py index fff09254..25e8da5c 100644 --- a/tests/test_unregularized.py +++ b/tests/test_unregularized.py @@ -34,6 +34,8 @@ def test_unregularized_methods(data): CCA(latent_dimensions=latent_dims), KCCA(latent_dimensions=latent_dims), PCACCA(latent_dimensions=latent_dims), + TCCA(latent_dimensions=latent_dims), + KTCCA(latent_dimensions=latent_dims), ] scores = [ @@ -57,7 +59,6 @@ def test_unregularized_multi(data): MCCA(latent_dimensions=latent_dims, pca=False), MCCA(latent_dimensions=latent_dims, pca=True), KGCCA(latent_dimensions=latent_dims), - TCCA(latent_dimensions=latent_dims), ] scores = [method.fit((X, Y, Z)).score((X, Y, Z)) for method in methods]