chi2_kernel#
- sklearn.metrics.pairwise.chi2_kernel(X, Y=None, gamma=1.0)[源代码]#
计算 X 和 Y 之间的指数卡方核函数。
卡方核函数是在 X 和 Y 中每对行之间计算的。X 和 Y 必须是非负的。此核函数最常用于直方图。
卡方核函数由下式给出:
k(x, y) = exp(-gamma Sum [(x - y)^2 / (x + y)])
它可以被解释为每个条目的加权差。
在用户指南中了解更多。
- 参数:
- X形状为 (n_samples_X, n_features) 的类数组
特征数组。
- Y形状为 (n_samples_Y, n_features) 的类数组,默认为 None
可选的第二个特征数组。如果为
None
,则使用Y=X
。- gamma浮点数,默认为 1
chi2 核函数的缩放参数。
- 返回:
- kernel形状为 (n_samples_X, n_samples_Y) 的 ndarray
核矩阵。
另请参阅
additive_chi2_kernel
此核函数的加性版本。
sklearn.kernel_approximation.AdditiveChi2Sampler
此核函数加性版本的傅里叶近似。
参考文献
Zhang, J. and Marszalek, M. and Lazebnik, S. and Schmid, C. Local features and kernels for classification of texture and object categories: A comprehensive study International Journal of Computer Vision 2007 https://hal.archives-ouvertes.fr/hal-00171412/document
示例
>>> 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.368, 0.135], [0.135, 0.368]])