rfflearn.cpu.RFFPCA
Principal Component Analysis with random Fourier features.
def rfflearn.cpu.RFFPCA(self, n_components=None, dim_kernel=128, std_kernel=0.1, W=None, b=None, **args)
Parameters:
n_components: int (default=None)
The number of components to be kept.
dim_kernel: int (default=128)
Dimension of the random matrix.
std_kernel: float (defualt=0.1)
Standard deviation of the random matrix.
W: np.ndarray (default=None)
Random matrix for the input X. If None then generated automatically.
b: np.ndarray (default=None)
Random bias for the input X. If None then generated automatically.
args: dict (defualt={})
Extra arguments. This dictionaly will be unpacked and passed to PCA class constructor of scikit-learn.
Attributes:
dim: int
Dimension of the random matrix.
s_k: float
Standard deviation of the random matrix.
mat: Callable
A function to generate the random matrix W.
W: np.ndarray
Random matrix for the input X.
b: np.ndarray
Random bias for the input X.
pca: sklearn.decomposition.PCA
PCA model instance.
Member functions
def get_covariance(self)
Wrapper function of sklearn.decomposition.PCA.get_covariance
.
Returns:
np.ndarray
Estimated covariance matrix of data with shape (n_features, n_features).
def get_precision(self)
Wrapper function of sklearn.decomposition.PCA.get_precision
.
Returns:
np.ndarray
Estimated precision matrix of data with shape (n_features, n_features).
def fit(self, X, *pargs, **kwargs)
Trains the PCA model. This function is a wrapper of sklearn.decomposition.PCA.fit
.
Parameters:
X: np.ndarray
Input matrix with shape (n_samples, n_features_input).
pargs: tuple (default=())
Extra positional arguments. This will be passed to fit function of sklearn's PCA model.
kwargs: dict (default={})
Extra keywork arguments. This will be passed to fit function of sklearn's PCA model.
Returns:
rfflearn.cpu.PCA
Fitted estimator.
def fit_transform(self, X, *pargs, **kwargs)
Wrapper function of sklearn.decomposition.PCA.fit_transform
.
Parameters:
X: np.ndarray
Input matrix with shape (n_samples, n_features_input).
pargs: tuple (default=())
Extra positional arguments. This will be passed to fit_transform function of sklearn's PCA model.
kwargs: dict (default={})
Extra keywork arguments. This will be passed to fit_transform function of sklearn's PCA model.
Returns:
np.ndarray
Transformed X
.
def score(self, X, *pargs, **kwargs)
Wrapper function of sklearn.decomposition.PCA.score
.
Parameters:
X, np.ndarray
Input matrix with shape (n_samples, n_features_input).
pargs: tuple (default=())
Extra positional arguments. This will be passed to score function of sklearn's PCA model.
kwargs: dict (default={})
Extra keywork arguments. This will be passed to score function of sklearn's PCA model.
Returns:
float
The average log-likelihood of all samples.
def score_samples(self, X, *pargs, **kwargs)
Wrapper function of sklearn.decomposition.PCA.score_samples
.
Parameters:
X: np.ndarray
Input matrix with shape (n_samples, n_features_input).
pargs: tuple (default=())
Extra positional arguments. This will be passed to score function of sklearn's PCA model.
kwargs: dict (default={})
Extra keywork arguments. This will be passed to score function of sklearn's PCA model.
Returns:
np.ndarray
The log-likelihood of each sample with shape (n_samples,).
def transform(self, X, *pargs, **kwargs)
Wrapper function of sklearn.decomposition.PCA.transform
.
Parameters:
X: np.ndarray
Input matrix with shape (n_samples, n_features_input).
pargs: tuple (default=())
Extra positional arguments. This will be passed to score function of sklearn's PCA model.
kwargs:dict (default={})
Extra keywork arguments. This will be passed to score function of sklearn's PCA model.
Returns:
np.ndarray
Transformed X
.
Minimal Example
>>> import numpy as np # Import Numpy
>>> import rfflearn.cpu as rfflearn # Import module
>>> X = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1]]) # Define input data
>>> pca = rfflearn.RFFPCA(n_components=1).fit(X) # Training (on CPU)
>>> pca.transform(X) # Transform (on CPU)
array([[-1.5231749 ],
[-2.37334318],
[ 1.5231749 ],
[ 2.37334318]])