2011-12-20 01:08:03 +08:00
|
|
|
.. _kernel_approximation:
|
|
|
|
|
|
2011-12-18 20:48:36 +08:00
|
|
|
Kernel Approximation
|
|
|
|
|
====================
|
|
|
|
|
|
|
|
|
|
This submodule contains functions that approximate the feature mappings that
|
|
|
|
|
correspond to certain kernels, as they are used for example in support vector
|
|
|
|
|
machines (see :ref:`svm`).
|
|
|
|
|
The following feature functions perform non-linear transformations of the
|
|
|
|
|
input, which can serve as a basis for linear classification or other
|
|
|
|
|
algorithms.
|
|
|
|
|
|
2011-12-20 21:15:44 +08:00
|
|
|
.. currentmodule:: sklearn.linear_model
|
|
|
|
|
|
2012-11-05 22:00:41 +08:00
|
|
|
The advantage of using approximate explicit feature maps compared to the
|
2015-12-03 07:16:40 +08:00
|
|
|
`kernel trick <https://en.wikipedia.org/wiki/Kernel_trick>`_,
|
2011-12-20 21:15:44 +08:00
|
|
|
which makes use of feature maps implicitly, is that explicit mappings
|
2011-12-18 20:48:36 +08:00
|
|
|
can be better suited for online learning and can significantly reduce the cost
|
2011-12-20 23:40:09 +08:00
|
|
|
of learning with very large datasets.
|
|
|
|
|
Standard kernelized SVMs do not scale well to large datasets, but using an
|
|
|
|
|
approximate kernel map it is possible to use much more efficient linear SVMs.
|
2014-07-28 19:30:52 +08:00
|
|
|
In particular, the combination of kernel map approximations with
|
2013-07-23 01:37:26 +08:00
|
|
|
:class:`SGDClassifier` can make non-linear learning on large datasets possible.
|
2011-12-18 20:48:36 +08:00
|
|
|
|
|
|
|
|
Since there has not been much empirical work using approximate embeddings, it
|
2011-12-20 03:11:11 +08:00
|
|
|
is advisable to compare results against exact kernel methods when possible.
|
2011-12-18 20:48:36 +08:00
|
|
|
|
2014-11-26 06:38:35 +08:00
|
|
|
.. seealso::
|
2014-08-24 11:59:30 +08:00
|
|
|
|
|
|
|
|
:ref:`polynomial_regression` for an exact polynomial transformation.
|
2011-12-20 01:08:03 +08:00
|
|
|
|
2012-12-10 00:42:23 +08:00
|
|
|
.. currentmodule:: sklearn.kernel_approximation
|
|
|
|
|
|
2013-01-20 21:23:29 +08:00
|
|
|
.. _nystroem_kernel_approx:
|
|
|
|
|
|
2012-12-10 00:42:23 +08:00
|
|
|
Nystroem Method for Kernel Approximation
|
|
|
|
|
----------------------------------------
|
|
|
|
|
The Nystroem method, as implemented in :class:`Nystroem` is a general method
|
|
|
|
|
for low-rank approximations of kernels. It achieves this by essentially subsampling
|
|
|
|
|
the data on which the kernel is evaluated.
|
|
|
|
|
By default :class:`Nystroem` uses the ``rbf`` kernel, but it can use any
|
|
|
|
|
kernel function or a precomputed kernel matrix.
|
|
|
|
|
The number of samples used - which is also the dimensionality of the features computed -
|
|
|
|
|
is given by the parameter ``n_components``.
|
|
|
|
|
|
2015-06-03 12:24:04 +08:00
|
|
|
.. _rbf_kernel_approx:
|
2012-12-10 00:42:23 +08:00
|
|
|
|
2011-12-18 20:48:36 +08:00
|
|
|
Radial Basis Function Kernel
|
|
|
|
|
----------------------------
|
2011-12-20 17:08:21 +08:00
|
|
|
|
|
|
|
|
The :class:`RBFSampler` constructs an approximate mapping for the radial basis
|
2014-06-19 21:59:04 +08:00
|
|
|
function kernel, also known as *Random Kitchen Sinks* [RR2007]_. This
|
|
|
|
|
transformation can be used to explicitly model a kernel map, prior to applying
|
|
|
|
|
a linear algorithm, for example a linear SVM::
|
2012-10-23 21:44:04 +08:00
|
|
|
|
|
|
|
|
>>> from sklearn.kernel_approximation import RBFSampler
|
|
|
|
|
>>> from sklearn.linear_model import SGDClassifier
|
|
|
|
|
>>> X = [[0, 0], [1, 1], [1, 0], [0, 1]]
|
|
|
|
|
>>> y = [0, 0, 1, 1]
|
|
|
|
|
>>> rbf_feature = RBFSampler(gamma=1, random_state=1)
|
|
|
|
|
>>> X_features = rbf_feature.fit_transform(X)
|
2018-07-05 22:25:34 +08:00
|
|
|
>>> clf = SGDClassifier(max_iter=5)
|
2019-06-01 16:53:45 +08:00
|
|
|
>>> clf.fit(X_features, y)
|
|
|
|
|
SGDClassifier(max_iter=5)
|
2012-10-23 21:44:04 +08:00
|
|
|
>>> clf.score(X_features, y)
|
|
|
|
|
1.0
|
2011-12-18 20:48:36 +08:00
|
|
|
|
|
|
|
|
The mapping relies on a Monte Carlo approximation to the
|
|
|
|
|
kernel values. The ``fit`` function performs the Monte Carlo sampling, whereas
|
|
|
|
|
the ``transform`` method performs the mapping of the data. Because of the
|
|
|
|
|
inherent randomness of the process, results may vary between different calls to
|
2011-12-18 20:54:10 +08:00
|
|
|
the ``fit`` function.
|
2011-12-18 20:48:36 +08:00
|
|
|
|
|
|
|
|
The ``fit`` function takes two arguments:
|
2014-07-06 19:33:20 +08:00
|
|
|
``n_components``, which is the target dimensionality of the feature transform,
|
|
|
|
|
and ``gamma``, the parameter of the RBF-kernel. A higher ``n_components`` will
|
2011-12-18 20:48:36 +08:00
|
|
|
result in a better approximation of the kernel and will yield results more
|
|
|
|
|
similar to those produced by a kernel SVM. Note that "fitting" the feature
|
|
|
|
|
function does not actually depend on the data given to the ``fit`` function.
|
|
|
|
|
Only the dimensionality of the data is used.
|
2011-12-20 01:08:03 +08:00
|
|
|
Details on the method can be found in [RR2007]_.
|
2011-12-18 20:48:36 +08:00
|
|
|
|
2013-06-27 21:09:16 +08:00
|
|
|
For a given value of ``n_components`` :class:`RBFSampler` is often less accurate
|
2012-12-10 00:42:23 +08:00
|
|
|
as :class:`Nystroem`. :class:`RBFSampler` is cheaper to compute, though, making
|
|
|
|
|
use of larger feature spaces more efficient.
|
|
|
|
|
|
2020-04-27 23:23:15 +08:00
|
|
|
.. figure:: ../auto_examples/miscellaneous/images/sphx_glr_plot_kernel_approximation_002.png
|
|
|
|
|
:target: ../auto_examples/miscellaneous/plot_kernel_approximation.html
|
2011-12-20 19:12:21 +08:00
|
|
|
:scale: 50%
|
|
|
|
|
:align: center
|
2011-12-20 03:11:11 +08:00
|
|
|
|
2011-12-20 21:15:44 +08:00
|
|
|
Comparing an exact RBF kernel (left) with the approximation (right)
|
|
|
|
|
|
|
|
|
|
.. topic:: Examples:
|
|
|
|
|
|
2020-04-27 23:23:15 +08:00
|
|
|
* :ref:`sphx_glr_auto_examples_miscellaneous_plot_kernel_approximation.py`
|
2011-12-20 21:15:44 +08:00
|
|
|
|
2015-06-03 12:24:04 +08:00
|
|
|
.. _additive_chi_kernel_approx:
|
2011-12-20 17:08:21 +08:00
|
|
|
|
2011-12-18 20:48:36 +08:00
|
|
|
Additive Chi Squared Kernel
|
2011-12-19 20:41:34 +08:00
|
|
|
---------------------------
|
2011-12-20 17:08:21 +08:00
|
|
|
|
2012-11-26 02:19:04 +08:00
|
|
|
The additive chi squared kernel is a kernel on histograms, often used in computer vision.
|
2011-12-18 20:48:36 +08:00
|
|
|
|
2012-11-26 02:19:04 +08:00
|
|
|
The additive chi squared kernel as used here is given by
|
2011-12-18 20:48:36 +08:00
|
|
|
|
|
|
|
|
.. math::
|
|
|
|
|
|
|
|
|
|
k(x, y) = \sum_i \frac{2x_iy_i}{x_i+y_i}
|
|
|
|
|
|
2012-11-26 02:19:04 +08:00
|
|
|
This is not exactly the same as :func:`sklearn.metrics.additive_chi2_kernel`.
|
2012-11-27 05:15:39 +08:00
|
|
|
The authors of [VZ2010]_ prefer the version above as it is always positive
|
2012-11-26 02:19:04 +08:00
|
|
|
definite.
|
2011-12-18 20:48:36 +08:00
|
|
|
Since the kernel is additive, it is possible to treat all components
|
2011-12-18 20:54:10 +08:00
|
|
|
:math:`x_i` separately for embedding. This makes it possible to sample
|
2011-12-18 20:48:36 +08:00
|
|
|
the Fourier transform in regular intervals, instead of approximating
|
|
|
|
|
using Monte Carlo sampling.
|
|
|
|
|
|
2012-04-27 18:22:33 +08:00
|
|
|
The class :class:`AdditiveChi2Sampler` implements this component wise
|
2014-07-06 19:33:20 +08:00
|
|
|
deterministic sampling. Each component is sampled :math:`n` times, yielding
|
|
|
|
|
:math:`2n+1` dimensions per input dimension (the multiple of two stems
|
2011-12-18 20:48:36 +08:00
|
|
|
from the real and complex part of the Fourier transform).
|
2014-07-06 19:33:20 +08:00
|
|
|
In the literature, :math:`n` is usually chosen to be 1 or 2, transforming
|
|
|
|
|
the dataset to size ``n_samples * 5 * n_features`` (in the case of :math:`n=2`).
|
2011-12-18 20:48:36 +08:00
|
|
|
|
|
|
|
|
The approximate feature map provided by :class:`AdditiveChi2Sampler` can be combined
|
2011-12-20 17:08:21 +08:00
|
|
|
with the approximate feature map provided by :class:`RBFSampler` to yield an approximate
|
2011-12-18 20:48:36 +08:00
|
|
|
feature map for the exponentiated chi squared kernel.
|
2011-12-20 01:08:03 +08:00
|
|
|
See the [VZ2010]_ for details and [VVZ2010]_ for combination with the :class:`RBFSampler`.
|
2011-12-18 20:48:36 +08:00
|
|
|
|
2015-06-03 12:24:04 +08:00
|
|
|
.. _skewed_chi_kernel_approx:
|
2011-12-20 17:08:21 +08:00
|
|
|
|
2011-12-18 20:48:36 +08:00
|
|
|
Skewed Chi Squared Kernel
|
|
|
|
|
-------------------------
|
2011-12-20 17:08:21 +08:00
|
|
|
|
2011-12-18 20:48:36 +08:00
|
|
|
The skewed chi squared kernel is given by:
|
|
|
|
|
|
|
|
|
|
.. math::
|
|
|
|
|
|
|
|
|
|
k(x,y) = \prod_i \frac{2\sqrt{x_i+c}\sqrt{y_i+c}}{x_i + y_i + 2c}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
It has properties that are similar to the exponentiated chi squared kernel
|
|
|
|
|
often used in computer vision, but allows for a simple Monte Carlo
|
2012-11-05 22:00:41 +08:00
|
|
|
approximation of the feature map.
|
2011-12-18 20:48:36 +08:00
|
|
|
|
|
|
|
|
The usage of the :class:`SkewedChi2Sampler` is the same as the usage described
|
|
|
|
|
above for the :class:`RBFSampler`. The only difference is in the free
|
2014-07-06 19:33:20 +08:00
|
|
|
parameter, that is called :math:`c`.
|
2011-12-20 01:08:03 +08:00
|
|
|
For a motivation for this mapping and the mathematical details see [LS2010]_.
|
|
|
|
|
|
2020-08-18 14:44:20 +08:00
|
|
|
.. _polynomial_kernel_approx:
|
|
|
|
|
|
|
|
|
|
Polynomial Kernel Approximation via Tensor Sketch
|
|
|
|
|
-------------------------------------------------
|
|
|
|
|
|
|
|
|
|
The :ref:`polynomial kernel <polynomial_kernel>` is a popular type of kernel
|
|
|
|
|
function given by:
|
|
|
|
|
|
|
|
|
|
.. math::
|
|
|
|
|
|
|
|
|
|
k(x, y) = (\gamma x^\top y +c_0)^d
|
|
|
|
|
|
|
|
|
|
where:
|
|
|
|
|
|
|
|
|
|
* ``x``, ``y`` are the input vectors
|
|
|
|
|
* ``d`` is the kernel degree
|
|
|
|
|
|
|
|
|
|
Intuitively, the feature space of the polynomial kernel of degree `d`
|
|
|
|
|
consists of all possible degree-`d` products among input features, which enables
|
|
|
|
|
learning algorithms using this kernel to account for interactions between features.
|
|
|
|
|
|
|
|
|
|
The TensorSketch [PP2013]_ method, as implemented in :class:`PolynomialCountSketch`, is a
|
|
|
|
|
scalable, input data independent method for polynomial kernel approximation.
|
|
|
|
|
It is based on the concept of Count sketch [WIKICS]_ [CCF2002]_ , a dimensionality
|
|
|
|
|
reduction technique similar to feature hashing, which instead uses several
|
|
|
|
|
independent hash functions. TensorSketch obtains a Count Sketch of the outer product
|
|
|
|
|
of two vectors (or a vector with itself), which can be used as an approximation of the
|
|
|
|
|
polynomial kernel feature space. In particular, instead of explicitly computing
|
|
|
|
|
the outer product, TensorSketch computes the Count Sketch of the vectors and then
|
|
|
|
|
uses polynomial multiplication via the Fast Fourier Transform to compute the
|
|
|
|
|
Count Sketch of their outer product.
|
|
|
|
|
|
|
|
|
|
Conveniently, the training phase of TensorSketch simply consists of initializing
|
|
|
|
|
some random variables. It is thus independent of the input data, i.e. it only
|
|
|
|
|
depends on the number of input features, but not the data values.
|
|
|
|
|
In addition, this method can transform samples in
|
|
|
|
|
:math:`\mathcal{O}(n_{\text{samples}}(n_{\text{features}} + n_{\text{components}} \log(n_{\text{components}})))`
|
|
|
|
|
time, where :math:`n_{\text{components}}` is the desired output dimension,
|
|
|
|
|
determined by ``n_components``.
|
|
|
|
|
|
|
|
|
|
.. topic:: Examples:
|
|
|
|
|
|
2020-11-06 20:16:43 +08:00
|
|
|
* :ref:`sphx_glr_auto_examples_kernel_approximation_plot_scalable_poly_kernels.py`
|
2020-08-18 14:44:20 +08:00
|
|
|
|
|
|
|
|
.. _tensor_sketch_kernel_approx:
|
2011-12-20 17:08:21 +08:00
|
|
|
|
2011-12-18 20:48:36 +08:00
|
|
|
Mathematical Details
|
|
|
|
|
--------------------
|
2011-12-20 17:08:21 +08:00
|
|
|
|
2011-12-18 20:48:36 +08:00
|
|
|
Kernel methods like support vector machines or kernelized
|
|
|
|
|
PCA rely on a property of reproducing kernel Hilbert spaces.
|
2014-07-06 19:33:20 +08:00
|
|
|
For any positive definite kernel function :math:`k` (a so called Mercer kernel),
|
2012-02-11 01:37:16 +08:00
|
|
|
it is guaranteed that there exists a mapping :math:`\phi`
|
|
|
|
|
into a Hilbert space :math:`\mathcal{H}`, such that
|
2011-12-18 20:48:36 +08:00
|
|
|
|
|
|
|
|
.. math::
|
|
|
|
|
|
2014-05-29 02:42:04 +08:00
|
|
|
k(x,y) = \langle \phi(x), \phi(y) \rangle
|
2011-12-18 20:48:36 +08:00
|
|
|
|
2014-05-29 02:42:04 +08:00
|
|
|
Where :math:`\langle \cdot, \cdot \rangle` denotes the inner product in the
|
2011-12-18 20:48:36 +08:00
|
|
|
Hilbert space.
|
|
|
|
|
|
|
|
|
|
If an algorithm, such as a linear support vector machine or PCA,
|
2011-12-18 20:54:10 +08:00
|
|
|
relies only on the scalar product of data points :math:`x_i`, one may use
|
2011-12-18 20:48:36 +08:00
|
|
|
the value of :math:`k(x_i, x_j)`, which corresponds to applying the algorithm
|
|
|
|
|
to the mapped data points :math:`\phi(x_i)`.
|
2014-07-06 19:33:20 +08:00
|
|
|
The advantage of using :math:`k` is that the mapping :math:`\phi` never has
|
2011-12-18 20:48:36 +08:00
|
|
|
to be calculated explicitly, allowing for arbitrary large
|
|
|
|
|
features (even infinite).
|
|
|
|
|
|
2011-12-20 17:08:21 +08:00
|
|
|
One drawback of kernel methods is, that it might be necessary
|
2011-12-18 20:48:36 +08:00
|
|
|
to store many kernel values :math:`k(x_i, x_j)` during optimization.
|
|
|
|
|
If a kernelized classifier is applied to new data :math:`y_j`,
|
|
|
|
|
:math:`k(x_i, y_j)` needs to be computed to make predictions,
|
2011-12-18 20:54:10 +08:00
|
|
|
possibly for many different :math:`x_i` in the training set.
|
2011-12-18 20:48:36 +08:00
|
|
|
|
|
|
|
|
The classes in this submodule allow to approximate the embedding
|
|
|
|
|
:math:`\phi`, thereby working explicitly with the representations
|
2011-12-20 17:08:21 +08:00
|
|
|
:math:`\phi(x_i)`, which obviates the need to apply the kernel
|
2011-12-18 20:48:36 +08:00
|
|
|
or store training examples.
|
|
|
|
|
|
|
|
|
|
|
2011-12-20 01:08:03 +08:00
|
|
|
.. topic:: References:
|
|
|
|
|
|
2011-12-20 03:11:11 +08:00
|
|
|
.. [RR2007] `"Random features for large-scale kernel machines"
|
2022-02-05 23:58:30 +08:00
|
|
|
<https://papers.nips.cc/paper/2007/hash/013a006f03dbc5392effeb8f18fda755-Abstract.html>`_
|
2011-12-20 03:11:11 +08:00
|
|
|
Rahimi, A. and Recht, B. - Advances in neural information processing 2007,
|
|
|
|
|
.. [LS2010] `"Random Fourier approximations for skewed multiplicative histogram kernels"
|
2022-02-05 23:58:30 +08:00
|
|
|
<https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.227.1802&rep=rep1&type=pdf>`_
|
2011-12-20 03:11:11 +08:00
|
|
|
Random Fourier approximations for skewed multiplicative histogram kernels
|
|
|
|
|
- Lecture Notes for Computer Sciencd (DAGM)
|
|
|
|
|
.. [VZ2010] `"Efficient additive kernels via explicit feature maps"
|
2015-12-03 07:16:40 +08:00
|
|
|
<https://www.robots.ox.ac.uk/~vgg/publications/2011/Vedaldi11/vedaldi11.pdf>`_
|
2011-12-20 03:11:11 +08:00
|
|
|
Vedaldi, A. and Zisserman, A. - Computer Vision and Pattern Recognition 2010
|
|
|
|
|
.. [VVZ2010] `"Generalized RBF feature maps for Efficient Detection"
|
2015-12-03 07:16:40 +08:00
|
|
|
<https://www.robots.ox.ac.uk/~vgg/publications/2010/Sreekanth10/sreekanth10.pdf>`_
|
2011-12-20 03:11:11 +08:00
|
|
|
Vempati, S. and Vedaldi, A. and Zisserman, A. and Jawahar, CV - 2010
|
2021-10-21 01:53:25 +08:00
|
|
|
.. [PP2013] :doi:`"Fast and scalable polynomial kernels via explicit feature maps"
|
|
|
|
|
<10.1145/2487575.2487591>`
|
2020-08-18 14:44:20 +08:00
|
|
|
Pham, N., & Pagh, R. - 2013
|
|
|
|
|
.. [CCF2002] `"Finding frequent items in data streams"
|
|
|
|
|
<http://www.cs.princeton.edu/courses/archive/spring04/cos598B/bib/CharikarCF.pdf>`_
|
|
|
|
|
Charikar, M., Chen, K., & Farach-Colton - 2002
|
|
|
|
|
.. [WIKICS] `"Wikipedia: Count sketch"
|
|
|
|
|
<https://en.wikipedia.org/wiki/Count_sketch>`_
|