pyrfm.random_feature.CompactRandomFeature

class pyrfm.random_feature.CompactRandomFeature(transformer_up=None, transformer_down=None, n_components=10, n_components_up=100, degree=2, h01=False, random_state=None)[source]

Bases: sklearn.base.BaseEstimator, sklearn.base.TransformerMixin

Efficient random feature map by Compact Random Feature map.

You can construct (the simplest) CompactRandomFeature by combining random features and sklearn.random_projection, e.g.,

>>> trans_up = RandomMaclaurin(n_components=n_components_up)
>>> trans_down = SparseRandomProjecion(n_components=n_components.
                                   density=1)
>>> X_trans_up = trans_up.fit_transform(X)
>>> X_trans_down = trans_down.fit_transform(X_trans_down)

The advantages of this CompactRandomFeature is its memory efficiency. Above-mentioned combinatorial approach might occur the memory error in the up projection when the size of the original feature matrix is large. CompactRandomFeature for a random feature map with a cython implementation avoid the memory error because it does not compute all of up projection random features at the same time. Although you can avoid MemoryError by creating mini-batches of the training instances, this CompactRandomFeature class save this step.

Parameters
  • transformer_up (sklearn transformer object (default=None)) – A random feature map object. If None, RandomMaclaurin is used.

  • transformer_down (str or sklearn transformer object (default=None)) – Transformer for down projection. {“srht”} can be used. If None, structured projection cannot be used. Standard RandomProjection is used.

  • n_components (int (default=10)) – Number of Monte Carlo samples per randomized features of transformer. It corresponds to E in original paper [1]. Equals the dimensionality of the computed (mapped) feature space.

  • n_components_up (int (default=100)) – Number of Monte Calro samples per original features. It corresponds to D in original paper [1]. It is used when transformer = “random_maclaurin” or “tensor_sketch”

  • degree (int (default=2)) – Degree of polynomial kernel. This argument is used when transformer = None.

  • h01 (int (default=False)) – Using h01 heuristics or not. This argument is used when transformer = None.

  • random_state (int, RandomState instance or None, optional (default=None)) – If int, random_state is the seed used by the random number generator; If np.RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by np.random.

random_weights_
Type

array, shape (n_components, transformer.n_components)

or(transformer.n_components)

The sampled basis for down projection.

References

[1] Compact Random Feature Maps. Raffay Hamid, Ying Xiao, Alex Gittens, and Dennis DeCoste. In ICML 2014. (http://proceedings.mlr.press/v32/hamid14.pdf)

fit(X, y=None)[source]

Fit the transformers for up projection and down projection.

Parameters

X ({array-like, sparse matrix}, shape (n_samples, n_features)) – Training data, where n_samples is the number of samples and n_features is the number of features.

Returns

self – Returns the transformer.

Return type

object

fit_transform(X, y=None, **fit_params)

Fit to data, then transform it.

Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.

Parameters
  • X (numpy array of shape [n_samples, n_features]) – Training set.

  • y (numpy array of shape [n_samples]) – Target values.

Returns

X_new – Transformed array.

Return type

numpy array of shape [n_samples, n_features_new]

get_params(deep=True)

Get parameters for this estimator.

Parameters

deep (boolean, optional) – If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns

params – Parameter names mapped to their values.

Return type

mapping of string to any

set_params(**params)

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as pipelines). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Returns

Return type

self

transform(X)[source]

Apply the approximate feature map to X.

Parameters

X ({array-like, sparse matrix}, shape (n_samples, n_features)) – New data, where n_samples is the number of samples and n_features is the number of features.

Returns

X_new

Return type

array-like, shape (n_samples, n_components)