mojolearn.PolynomialCountSketch#
- class mojolearn.PolynomialCountSketch(*, gamma=1.0, degree=2, coef0=0, n_components=100, random_state=None)[source]#
Bases:
_XNeighborsPolynomial 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’sPolynomialCountSketch. The hash tables are drawn from mojolearn’s counter-based generator seeded byrandom_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_hashedisn_features + 1whencoef0 != 0, elsen_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)
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
degreeorn_componentsis below 1, orrandom_stateis 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(ValueErrorotherwise). Sparse input is densified.- Returns:
X_new (Array of shape (n_samples, n_components), float32)