mojolearn.PolynomialCountSketch#

class mojolearn.PolynomialCountSketch(*, gamma=1.0, degree=2, coef0=0, n_components=100, random_state=None)[source]#

Bases: _XNeighbors

Polynomial kernel approximation by tensor sketch.

Maps data to a feature space whose inner products approximate the polynomial kernel (gamma * <x, y> + coef0) ** degree, following scikit-learn’s PolynomialCountSketch. The hash tables are drawn from mojolearn’s counter-based generator seeded by random_state: the same distribution as scikit-learn, reproducible for an int seed, but not scikit-learn’s exact numbers. The sketches’ circular convolution is computed directly rather than by FFT. Sparse input is densified; computation is float32.

Parameters:
  • gamma (float, default 1.0) – Kernel coefficient.

  • degree (int, default 2) – Degree of the polynomial kernel; at least 1.

  • coef0 (float, default 0) – Constant term of the kernel.

  • n_components (int, default 100) – Dimension of the output feature space; at least 1.

  • random_state (int, numpy.random.RandomState or None, default None) – Seed for the hash tables. A RandomState or None contributes one draw that seeds the generator. Anything else raises ValueError.

  • numeric_mode ({‘identical’, ‘deterministic’, ‘fast’} or None, default None) – Numeric mode for this estimator ('fast' is Apple only). None uses the process default.

Variables:
  • indexHash_ (Array of shape (degree, n_features_hashed), int32) – Output bucket of each input feature, per degree.

  • bitHash_ (Array of shape (degree, n_features_hashed), int32) – Sign (-1 or 1) of each input feature, per degree. n_features_hashed is n_features + 1 when coef0 != 0, else n_features.

  • n_features_in_ (int) – Number of features seen in fit.

Examples

>>> import numpy as np, mojolearn
>>> X = np.random.default_rng(0).random((1000, 8), dtype=np.float32)
>>> Z = mojolearn.PolynomialCountSketch(n_components=64, random_state=0).fit_transform(X)
__init__(*, gamma=1.0, degree=2, coef0=0, n_components=100, random_state=None)[source]#

Methods

fit(X[, y])

Draw the hash tables.

fit_transform(X[, y])

Fit to X, then transform X: fit(X).transform(X).

get_feature_names_out([input_features])

Output feature names: polynomialcountsketch0, polynomialcountsketch1, ...

get_params([deep])

numeric_mode_used()

The numeric mode this estimator runs in.

set_params(**params)

Set constructor parameters.

transform(X)

Map X to the sketched feature space.

vendor_used()

The GPU backend this estimator runs on.

fit(X, y=None)[source]#

Draw the hash tables.

Parameters:
  • X (array-like or scipy.sparse matrix of shape (n_samples, n_features)) – Training data; only its number of features is used.

  • y (None) – Ignored.

Returns:

self (PolynomialCountSketch) – The fitted transformer.

Raises:

ValueError – If degree or n_components is below 1, or random_state is not an int, a numpy RandomState or None.

transform(X)[source]#

Map X to the sketched feature space.

Parameters:

X (array-like or scipy.sparse matrix of shape (n_samples, n_features)) – Samples; the number of features must match fit (ValueError otherwise). Sparse input is densified.

Returns:

X_new (Array of shape (n_samples, n_components), float32)

fit_transform(X, y=None)[source]#

Fit to X, then transform X: fit(X).transform(X).

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Training data.

  • y (None) – Ignored.

Returns:

X_new (Array of shape (n_samples, n_components), float32)

get_feature_names_out(input_features=None)[source]#

Output feature names: polynomialcountsketch0, polynomialcountsketch1, …

Parameters:

input_features (None) – Ignored.

Returns:

names (list of str)