Polynomial kernel approximation via Tensor Sketch.
Implements Tensor Sketch, which approximates the feature map of the polynomial kernel:
K(X, Y) = (gamma * <X, Y> + coef0)^degree
by efficiently computing a Count Sketch of the outer product of a vector with itself using Fast Fourier Transforms (FFT). Read more in the User Guide.
Added in version 0.24.
Parameter of the polynomial kernel whose feature map will be approximated.
Degree of the polynomial kernel whose feature map will be approximated.
Constant term of the polynomial kernel whose feature map will be approximated.
Dimensionality of the output feature space. Usually, n_components should be greater than the number of features in input samples in order to achieve good performance. The optimal score / run time balance is typically achieved around n_components = 10 * n_features, but this depends on the specific dataset being used.
Determines random number generation for indexHash and bitHash initialization. Pass an int for reproducible results across multiple function calls. See Glossary.
Array of indexes in range [0, n_components) used to represent the 2-wise independent hash functions for Count Sketch computation.
Array with random entries in {+1, -1}, used to represent the 2-wise independent hash functions for Count Sketch computation.
Number of features seen during fit.
Added in version 0.24.
n_features_in_,)
Names of features seen during fit. Defined only when X has feature names that are all strings.
Added in version 1.0.
See also
AdditiveChi2SamplerApproximate feature map for additive chi2 kernel.
NystroemApproximate a kernel map using a subset of the training data.
RBFSamplerApproximate a RBF kernel feature map using random Fourier features.
SkewedChi2SamplerApproximate feature map for “skewed chi-squared” kernel.
sklearn.metrics.pairwise.kernel_metricsList of built-in kernels.
>>> from sklearn.kernel_approximation import PolynomialCountSketch >>> from sklearn.linear_model import SGDClassifier >>> X = [[0, 0], [1, 1], [1, 0], [0, 1]] >>> y = [0, 0, 1, 1] >>> ps = PolynomialCountSketch(degree=3, random_state=1) >>> X_features = ps.fit_transform(X) >>> clf = SGDClassifier(max_iter=10, tol=1e-3) >>> clf.fit(X_features, y) SGDClassifier(max_iter=10) >>> clf.score(X_features, y) 1.0
For a more detailed example of usage, see Scalable learning with polynomial kernel approximation
Fit the model with X.
Initializes the internal variables. The method needs no information about the distribution of data, so we only care about n_features in X.
Training data, where n_samples is the number of samples and n_features is the number of features.
Target values (None for unsupervised transformations).
Returns the instance itself.
Fit to data, then transform it.
Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.
Input samples.
Target values (None for unsupervised transformations).
Additional fit parameters.
Transformed array.
Get output feature names for transformation.
The feature names out will prefixed by the lowercased class name. For example, if the transformer outputs 3 features, then the feature names out are: ["class_name0", "class_name1", "class_name2"].
Only used to validate feature names with the names seen in fit.
Transformed feature names.
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
A MetadataRequest encapsulating routing information.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Set output container.
See Introducing the set_output API for an example on how to use the API.
Configure output of transform and fit_transform.
"default": Default output format of a transformer"pandas": DataFrame output"polars": Polars outputNone: Transform configuration is unchangedAdded in version 1.4: "polars" option was added.
Estimator instance.
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Estimator parameters.
Estimator instance.
Generate the feature map approximation for X.
New data, where n_samples is the number of samples and n_features is the number of features.
Returns the instance itself.
© 2007–2025 The scikit-learn developers
Licensed under the 3-clause BSD License.
https://scikit-learn.org/1.6/modules/generated/sklearn.kernel_approximation.PolynomialCountSketch.html