pyrfm.random_feature.SubfeatureRandomMaclaurin

class pyrfm.random_feature.SubfeatureRandomMaclaurin(n_components=100, n_sub_features=5, p=10, kernel='poly', degree=2, distribution='rademacher', gamma='auto', bias=0.0, coefs=None, max_expansion=50, h01=False, dense_output=True, p_sparse=0.0, random_state=None)[source]

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

Approximates feature map of a dot product kernel by Monte Carlo approximation of its Maclaurin expansion with only sub features.

Parameters
  • n_components (int (default=100)) – Number of Monte Carlo samples per original features. Equals the dimensionality of the computed (mapped) feature space.

  • n_sub_features (int (default=5)) – Number of sub features.

  • p (int (default=10)) – Parameter of the distribution that determines which components of the Maclaurin series are approximated.

  • kernel (str or callable (default="poly")) – Type of kernel function. ‘poly’, ‘exp’, or callable are accepted. If callable, its arguments are two numpy-like objects, and return a numpy-like object. if str, only ‘poly’ or ‘exp’ is acceptable.

  • degree (int (default=2)) – Parameter of the polynomial product kernel.

  • distribution (str, (default="rademacher")) – Distribution for random_weights_. “rademacher”, “gaussian”, “laplace”, “uniform”, or “sparse_rademacher” can be used.

  • gamma (float or str (default="auto")) – Parameter of the exponential kernel.

  • bias (float (default=0)) – Parameter of the polynomial kernel.

  • coefs (list-like (default=None)) – list of coefficients of Maclaurin expansion.

  • max_expansion (int (default=50)) – Threshold of Maclaurin expansion.

  • h01 (bool (default=False)) – Use h01 heuristic or not. See [1].

  • dense_output (bool (default=False)) – Whether randomized feature matrix is dense or sparse. For kernel=’anova’, if dense_output = False, distribution=’sparse_rademacher’, and X is sparse matrix, output random feature matrix will become sparse matrix. For kernel=’anova_cython’, if dense_output=False, output random feature matrix will become sparse matrix.

  • p_sparse (float (default=0.)) – Sparsity parameter for “sparse_rademacher” distribution. If p_sparse = 0, “sparse_rademacher” is equivalent to “rademacher”.

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

orders_

The sampled orders of the Maclaurin expansion. The j-th components of random feature approximates orders_[j]-th order of the Maclaurin expansion.

Type

array, shape (n_components, )

random_weights_

The sampled basis.

Type

csr_matrix, shape (n_features, np.sum(orders_))

References

[1] Random Feature Maps for Dot Product Kernels. Purushottam Kar and Harish Karnick. In AISTATS 2012. (http://proceedings.mlr.press/v22/kar12/kar12.pdf)

[2] Sparse Random Feature Maps for the item-multiset Kernel. Kyohei Atarashi, Satoshi Oyama and Masahito Kurihara. To appear.

fit(X, y=None)[source]

Generate random weights and orders according to n_features.

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)