PairwiseKernel#

class sklearn.gaussian_process.kernels.PairwiseKernel(gamma=1.0, gamma_bounds=(1e-05, 100000.0), metric='linear', pairwise_kernels_kwargs=None)[source]#

scikit-learn.metrics.pairwise 中核函数的封装。

对 scikit-learn.metrics.pairwise 中核函数功能的轻量级封装。

注意:`eval_gradient` 的评估不是解析式的,而是数值的,并且所有

核函数仅支持各向同性距离。参数 `gamma` 被视为超参数,可以进行优化。其他核函数参数在初始化时直接设置,并保持固定。

0.18 版新增。

参数:
gamma浮点型,默认值=1.0

由 `metric` 指定的成对核函数的 `gamma` 参数。它应为正值。

gamma_bounds一对浮点数,>= 0 或 “fixed”,默认值=(1e-5, 1e5)

`gamma` 的下限和上限。如果设置为“fixed”,`gamma` 在超参数调优期间不能更改。

metric{“linear”, “additive_chi2”, “chi2”, “poly”, “polynomial”, “rbf”, “laplacian”, “sigmoid”, “cosine”} 或可调用对象,默认值=”linear”

在计算特征数组中实例之间的核函数时使用的度量。如果 `metric` 是字符串,则它必须是 `pairwise.PAIRWISE_KERNEL_FUNCTIONS` 中的一个度量。如果 `metric` 是“precomputed”,则假定 `X` 是一个核矩阵。或者,如果 `metric` 是一个可调用函数,它将作用于每一对实例(行),并记录结果值。该可调用函数应以 `X` 中的两个数组作为输入,并返回表示它们之间距离的值。

pairwise_kernels_kwargs字典,默认值=None

此字典中的所有条目(如果有)都将作为关键字参数传递给成对核函数。

示例

>>> from sklearn.datasets import load_iris
>>> from sklearn.gaussian_process import GaussianProcessClassifier
>>> from sklearn.gaussian_process.kernels import PairwiseKernel
>>> X, y = load_iris(return_X_y=True)
>>> kernel = PairwiseKernel(metric='rbf')
>>> gpc = GaussianProcessClassifier(kernel=kernel,
...         random_state=0).fit(X, y)
>>> gpc.score(X, y)
0.9733
>>> gpc.predict_proba(X[:2,:])
array([[0.8880, 0.05663, 0.05532],
       [0.8676, 0.07073, 0.06165]])
__call__(X, Y=None, eval_gradient=False)[source]#

返回核函数 k(X, Y) 及其可选的梯度。

参数:
X形状为 (n_samples_X, n_features) 的 ndarray

返回的核函数 k(X, Y) 的左侧参数

Y形状为 (n_samples_Y, n_features) 的 ndarray,默认值=None

返回的核函数 k(X, Y) 的右侧参数。如果为 None,则改为评估 k(X, X)。

eval_gradient布尔型,默认值=False

确定是否计算核超参数对数的梯度。仅在 `Y` 为 None 时支持。

返回:
K形状为 (n_samples_X, n_samples_Y) 的 ndarray

核函数 k(X, Y)

K_gradient形状为 (n_samples_X, n_samples_X, n_dims) 的 ndarray,可选

核函数 k(X, X) 对核函数超参数的对数的梯度。仅当 eval_gradient 为 True 时返回。

property bounds#

返回 `theta` 的对数变换边界。

返回:
bounds形状为 (n_dims, 2) 的 ndarray

核函数超参数 `theta` 的对数变换边界

clone_with_theta(theta)[source]#

返回一个具有给定超参数 `theta` 的 `self` 克隆。

参数:
theta形状为 (n_dims,) 的 ndarray

超参数

diag(X)[source]#

返回核函数 k(X, X) 的对角线。

此方法的结果与 `np.diag(self(X))` 相同;但是,由于只评估对角线,因此可以更高效地进行评估。

参数:
X形状为 (n_samples_X, n_features) 的 ndarray

返回的核函数 k(X, Y) 的左侧参数

返回:
K_diag形状为 (n_samples_X,) 的 ndarray

核函数 k(X, X) 的对角线

get_params(deep=True)[source]#

获取此核函数的参数。

参数:
deep布尔型,默认值=True

如果为 True,将返回此估计器及其包含的作为估计器的子对象的参数。

返回:
params字典

参数名称映射到其值。

property hyperparameters#

返回所有超参数规范的列表。

is_stationary()[source]#

返回核函数是否是平稳的。

property n_dims#

返回核函数的非固定超参数的数量。

property requires_vector_input#

返回核函数是定义在固定长度特征向量上还是通用对象上。为了向后兼容,默认为 True。

set_params(**params)[source]#

设置此核函数的参数。

此方法适用于简单核函数和嵌套核函数。后者具有 <component>__<parameter> 形式的参数,因此可以更新嵌套对象的每个组件。

返回:
self
property theta#

返回(展平的、对数变换的)非固定超参数。

请注意,`theta` 通常是核函数超参数的对数变换值,因为这种搜索空间表示更适合超参数搜索,因为像长度尺度这样的超参数自然地位于对数尺度上。

返回:
theta形状为 (n_dims,) 的 ndarray

核函数的非固定、对数变换超参数