rfflearn_logo

rfflearn.cpu.RFFCCA

Canonical correlation analysis with random Fourier features.

def rfflearn.cpu.RFFCCA(self, dim_kernel=128, std_kernel=0.1, W1=None, b1=None, W2=None, b2=None, **args)

Parameters:

dim_kernel: int  (default=128)

Dimension of the random matrix.

std_kernel: float  (default=0.1)

Standard deviation of the random matrix.

W1: np.ndarray  (default=None)

Random matrix for the input X. If None then generated automatically.

b1: np.ndarray  (default=None)

Random bias for the input X. If None then generated automatically.

W2: np.ndarray  (default=None)

Random matrix for the output Y. If None then generated automatically.

b2: np.ndarray  (default=None)

Random bias for the output Y. If None then generated automatically.

args: dict  (default={})

Extra arguments. This dictionary will be unpacked and passed to CCA class constructor of scikit-learn.

Attributes:

fx1: rfflearn.cpu.Base

RFF extractor for the input X.

fx2: rfflearn.cpu.Base

RFF extractor for the output Y.

cca: sklearn.cross_decomposition.CCA

CCA model instance of the scikit-learn.

Member functions

def fit(self, X, Y)

Extracts feature vectors and train CCA according to the given data.

Parameters:

X: np.ndarray

Input matrix with shape (n_samples, n_features_input).

Y: np.ndarray

Output matrix with shape (n_samples, n_features_output).

Returns:

rfflearn.cpu.RFFCCA

Fitted estimator.

def predict(self, X, copy=True)

Returns prediction results

Parameters:

X: np.ndarray

Input matrix with shape (n_samples, n_features_input).

copy: bool

Make a copy of X if True, otherwise perform in-place operation.

Returns:

np.ndarray

Output matrix with shape (n_samples, n_features_output)

def score(self, X, Y, sample_weight=None)

Returns evaluation score (the coefficient of determination R2).

Parameters:

X: np.ndarray

Input matrix with shape (n_samples, n_features_input).

Y: np.ndarray

Output matrix with shape (n_samples, n_features_output).

sample_weight: np.ndarray

Sample weight of the evaluation with shape (n_samples,).

Returns:

float

Returns the R2 score (coefficient of determination) of the prediction.

def transform(self, X, Y=None, copy=True)

Applies the dimension reduction using the trained CCA.

Parameters:

X: np.ndarray

Input matrix with shape (n_samples, n_features_input).

Y: np.ndarray

Output matrix with shape (n_samples, n_features_output).

copy: bool

Make a copy of X if True, otherwise perform in-place operation.

Returns:

np.ndarray

Applies the dimension reduction and returns transformed X if Y is not given, otherwise returns (x_transformed, y_transformed).

Minimal Example

>>> import numpy as np                              # Import Numpy
>>> import rfflearn.cpu as rfflearn                 # Import module
>>> X = np.array([[0, 3], [1, 1], [2, 2], [3, 0]])  # Define input X data
>>> Y = np.array([[0, 1], [1, 3], [2, 1], [3, 2]])  # Definr input Y data
>>> cca = rfflearn.RFFCCA().fit(X, Y)               # Training (on CPU)
>>> cca.transform(X, Y)                             # Transform (on CPU)
(array([[-1.91956956e+00,  8.66279305e-11],
...