Skip to content

Instantly share code, notes, and snippets.

@rhngla
Last active March 25, 2020 07:06
Show Gist options
  • Save rhngla/3ad1e8ad24bfa9a0c7402e7a60817a35 to your computer and use it in GitHub Desktop.
Save rhngla/3ad1e8ad24bfa9a0c7402e7a60817a35 to your computer and use it in GitHub Desktop.
Extend the CCA class of scikit-learn to return reconstructions of the Y as well.
from sklearn.cross_decomposition import CCA
from sklearn.utils import check_array
from sklearn.utils.validation import check_is_fitted, FLOAT_DTYPES
class CCA_extended(CCA):
def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)
return
def inverse_transform_xy(self,X,Y):
"""
This module calculates the inverse transform for both X and Y
CCA.inverse_transform module only calculates reconstructions for X
"""
check_is_fitted(self)
X = check_array(X, dtype=FLOAT_DTYPES)
Y = check_array(Y, dtype=FLOAT_DTYPES)
x = np.matmul(X, cca.x_loadings_.T)
x *= cca.x_std_
x += cca.x_mean_
y = np.matmul(Y, cca.y_loadings_.T)
y *= cca.y_std_
y += cca.y_mean_
return x,y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment