sklearn.metrics.pairwise.chi2_kernel(X, Y=None, gamma=1.0)[source]#

Compute the exponential chi-squared kernel between X and Y.

The chi-squared kernel is computed between each pair of rows in X and Y. X and Y have to be non-negative. This kernel is most commonly applied to histograms.

The chi-squared kernel is given by:

k(x, y) = exp(-gamma Sum [(x - y)^2 / (x + y)])

It can be interpreted as a weighted difference per entry.

Read more in the User Guide.

Xarray-like of shape (n_samples_X, n_features)

A feature array.

Yarray-like of shape (n_samples_Y, n_features), default=None

An optional second feature array. If None, uses Y=X.

gammafloat, default=1

Scaling parameter of the chi2 kernel.

kernelndarray of shape (n_samples_X, n_samples_Y)

The kernel matrix.

See also


The additive version of this kernel.


A Fourier approximation to the additive version of this kernel.



>>> from sklearn.metrics.pairwise import chi2_kernel
>>> X = [[0, 0, 0], [1, 1, 1]]
>>> Y = [[1, 0, 0], [1, 1, 0]]
>>> chi2_kernel(X, Y)
array([[0.36..., 0.13...],
       [0.13..., 0.36...]])