Source code for lightonml.random_projections.base

# -*- coding: utf8
"""Random Projection transformers.

Random Projections are a fast, efficient and distance preserving method to
reduce or boost the dimensionality of the data.
Their formal justification is found in the `Johnson-Lindenstrauss lemma
<https://en.wikipedia.org/wiki/Johnson%E2%80%93Lindenstrauss_lemma>`_

"""
import numpy as np
import time
from sklearn.random_projection import BaseRandomProjection
from sklearn.utils import check_random_state


[docs]def complex_gaussian_random_matrix(n_components, n_features, scale=1.0, random_state=None): """Generate a complex matrix drawing elements at random from a gaussian distribution. Parameters ---------- n_components : int, dimensionality of the target projection space. n_features : int, dimensionality of the original source space. random_state: int, RandomState instance or None, defaults to None, controls the pseudo random number generator used to generate the matrix at fit time. Returns ------- components : np.ndarray [n_components, n_features], the generated random matrix. """ rng = check_random_state(random_state) real_comp = rng.normal(loc=0.0, scale=scale / np.sqrt(n_features), size=(n_components, n_features)) imag_comp = rng.normal(loc=0.0, scale=scale / np.sqrt(n_features), size=(n_components, n_features)) components = real_comp.astype(np.float32) + 1.0j * imag_comp.astype(np.float32) return components
[docs]class ComplexGaussianRandomProjection(BaseRandomProjection): """Implements Complex Gaussian Random Projections. Parameters ---------- n_components : int, dimensionality of the target projection space. random_state : int, RandomState instance or None, optional, defaults to None, controls the pseudo random number generator used to generate the matrix at fit time. """ def __init__(self, n_components=1000, scale=1.0, random_state=None): super(ComplexGaussianRandomProjection, self).__init__(n_components=n_components, dense_output=True, random_state=random_state) self.scale = scale def _make_random_matrix(self, n_components, n_features): """ Generate the random projection matrix. Parameters ---------- n_components : int, Dimensionality of the target projection space. n_features : int, Dimensionality of the original source space. Returns ------- components : 2D np.ndarray [n_components, n_features] The generated random matrix. """ random_state = check_random_state(self.random_state) components = complex_gaussian_random_matrix(n_components, n_features, scale=self.scale, random_state=random_state) return components
[docs]class ModulusComplexGaussianRandomProjection(ComplexGaussianRandomProjection): """Implements the absolute value squared of Complex Gaussian Random Projections. Parameters ---------- n_components : int, dimensionality of the target projection space. random_state : int, RandomState instance or None, optional, defaults to None, controls the pseudo random number generator used to generate the matrix at fit time. Attributes ---------- n_components : int, dimensionality of the target projection space. random_state : int, RandomState instance or None, optional, defaults to None, controls the pseudo random number generator used to generate the matrix at fit time. """ def __init__(self, n_components=1000, scale=1.0, random_state=None): self.n_components = n_components self.random_state = random_state super(ModulusComplexGaussianRandomProjection, self).__init__(n_components=n_components, scale=scale, random_state=random_state) self.projection_times = list()
[docs] def transform(self, X): """ Compute the absolute value squared of Gaussian Random Projections (Nonlinear Random Projections). .. math:: \\mathbf{y} = \\lvert \\mathbf{R} \\mathbf{x} \\rvert^2 Parameters ---------- X : 2D np.ndarray [n_samples, n_features], the input data to project. y : np.ndarray or None, optional, defaults None. the targets data. Returns ------- nonlinear_projections : 2D np.ndarray [n_samples, n_components], result of the nonlinear projection to the target space. """ t0 = time.time() components = super(ModulusComplexGaussianRandomProjection, self).transform(X) nonlinear_projections = np.abs(components) ** 2 self.projection_times.append(time.time() - t0) return nonlinear_projections