lightonml.random_projections¶

lightonml.random_projections.base¶

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

class ComplexGaussianRandomProjection(n_components=1000, scale=1.0, random_state=None)[source]

Bases: sklearn.random_projection.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.

class ModulusComplexGaussianRandomProjection(n_components=1000, scale=1.0, random_state=None)[source]

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.

n_components

dimensionality of the target projection space.

Type

int,

random_state

controls the pseudo random number generator used to generate the matrix at fit time.

Type

int, RandomState instance or None, optional, defaults to None,

transform(X)[source]

Compute the absolute value squared of Gaussian Random Projections (Nonlinear Random Projections).

$\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 – result of the nonlinear projection to the target space.

Return type

2D np.ndarray [n_samples, n_components],

complex_gaussian_random_matrix(n_components, n_features, scale=1.0, random_state=None)[source]

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 – the generated random matrix.

Return type

np.ndarray [n_components, n_features],

lightonml.random_projections.opu¶

This module is the heart of the library, the interface to the OPU lives here. Here are the essential functions to talk to the OPU and run computations on it.

class OPURandomMapping(opu, n_components=1000, position='2d_macro_pixels', roi_shape=(-1, -1), roi_position=(0, 0), disable_pbar=False)[source]

Interface to the OPU.

$\mathbf{y} = \lvert \mathbf{R} \mathbf{x} \rvert^2$
Parameters
• opu (lightonopu.opu.OPU,) – optical processing unit instance linked to a physical device.

• n_components (int,) – dimensionality of the target projection space.

• position (string or callable,) – type of formatting on the DMD. Built-in options are [‘centered’, ‘2d_macro_pixels’, ‘1d_square_macro_pixels’, ‘1d_rectangle_macro_pixels’, ‘lined’].

• roi_shape (tuple of ints, defaults to (-1, -1),) – shape of the region of interest on the DMD.

• roi_position (tuple of ints,) – position of the region of interest on the DMD. The y axis is oriented downwards.

• disable_pbar (boolean,) – if True, disable the progress bar of the transform.

opu

optical processing unit instance linked to a physical device.

Type

lightonopu.opu.OPU,

n_components

dimensionality of the target projection space.

Type

int,

position

function used to format the input for the DMD.

Type

str, ‘centered’ ,‘1d_macro-pixels’, ‘2d_macro_pixels’ or callable.

roi_shape

shape of the region of interest (ROI) on the DMD.

Type

tuple of ints,

roi_position

position of the upper left corner of the ROI on the DMD. The y axis is oriented downwards.

Type

tuple of ints,

disable_pbar

if True, disable the progress bar of the transform.

Type

boolean,

factor

number of times a single pixel is repeated.

Type

int,

formatting_func

function that builds the macro-pixels in the DMD array format.

Type

callable,

n_features

number of features of the samples

Type

int,

check(X)[source]

Safety checks for DMD input.

Parameters

X (2D np.ndarray,) – input data in bytes.

fit(X, y=None)[source]

Initialize the formatting method.

Parameters
• X (2D or 3D np.ndarray,) – input data.

• y (np.ndarray or None, default to None,) – targets.

Returns

self

Return type

OPURandomMapping.

opu_mechanics_in(X)[source]

Packs bytes to bits to send to the OPU.

Parameters

X (2D np.ndarray,) – input data in bytes.

Returns

X_packed – input data in bits.

Return type

2D np.ndarray

opu_mechanics_out(X)[source]

Selects n_components from the OPU output and casts to float32.

Parameters

X (2D np.ndarray,) – input data in bytes.

Returns

X_out – random features.

Return type

2D np.ndarray

transform(X, y=None, n_samples_by_pass=3000)[source]

Performs the nonlinear random projections batch by batch.

Parameters
• X (2D or 3D np.ndarray,) – input data.

• y (np.ndarray or None,) – targets.

• n_samples_by_pass (int,) – number of samples passed at each iteration to the OPU.

Returns

Y – complete array of nonlinear random projections.

Return type

2D or 3D np.ndarray,

transform_(X, y=None)[source]

Performs the nonlinear random projections. If the tqdm package is available, a progress bar is displayed, unless disable_pbar is True at object creation.

Parameters
• X (2D np.ndarray,) – input data.

• y (np.ndarray or None,) – targets.

Returns

random_features – non linear random features.

Return type

2D np.ndarray,