本文整理汇总了Python中sklearn.metrics.pairwise.polynomial_kernel方法的典型用法代码示例。如果您正苦于以下问题:Python pairwise.polynomial_kernel方法的具体用法?Python pairwise.polynomial_kernel怎么用?Python pairwise.polynomial_kernel使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.metrics.pairwise
的用法示例。
在下文中一共展示了pairwise.polynomial_kernel方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __kernel_definition__
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import polynomial_kernel [as 别名]
def __kernel_definition__(self):
"""Select the kernel function
Returns
-------
kernel : a callable relative to selected kernel
"""
if hasattr(self.kernel, '__call__'):
return self.kernel
if self.kernel == 'rbf' or self.kernel == None:
return lambda X,Y : rbf_kernel(X,Y,self.rbf_gamma)
if self.kernel == 'poly':
return lambda X,Y : polynomial_kernel(X, Y, degree=self.degree, gamma=self.rbf_gamma, coef0=self.coef0)
if self.kernel == 'linear':
return lambda X,Y : linear_kernel(X,Y)
if self.kernel == 'precomputed':
return lambda X,Y : X
示例2: test_polynomial_kernel
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import polynomial_kernel [as 别名]
def test_polynomial_kernel(N=1):
np.random.seed(12345)
i = 0
while i < N:
N = np.random.randint(1, 100)
M = np.random.randint(1, 100)
C = np.random.randint(1, 1000)
gamma = np.random.rand()
d = np.random.randint(1, 5)
c0 = np.random.rand()
X = np.random.rand(N, C)
Y = np.random.rand(M, C)
mine = PolynomialKernel(gamma=gamma, d=d, c0=c0)(X, Y)
gold = sk_poly(X, Y, gamma=gamma, degree=d, coef0=c0)
np.testing.assert_almost_equal(mine, gold)
print("PASSED")
i += 1
示例3: homogeneous_kernel
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import polynomial_kernel [as 别名]
def homogeneous_kernel(X, P, degree=2):
"""Convenience alias for homogeneous polynomial kernel between X and P::
K_P(x, p) = <x, p> ^ degree
Parameters
----------
X : ndarray of shape (n_samples_1, n_features)
Y : ndarray of shape (n_samples_2, n_features)
degree : int, default 2
Returns
-------
Gram matrix : array of shape (n_samples_1, n_samples_2)
"""
return polynomial_kernel(X, P, degree=degree, gamma=1, coef0=0)
示例4: test_affinity_mat_poly
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import polynomial_kernel [as 别名]
def test_affinity_mat_poly(data):
v1_data = data['fit_data'][0]
distances = cdist(v1_data, v1_data)
gamma = 1 / (2 * np.median(distances) ** 2)
true_kernel = polynomial_kernel(v1_data, gamma=gamma)
spectral = MultiviewSpectralClustering(random_state=RANDOM_STATE,
affinity='poly')
p_kernel = spectral._affinity_mat(v1_data)
assert(p_kernel.shape[0] == data['n_fit'])
assert(p_kernel.shape[1] == data['n_fit'])
for ind1 in range(p_kernel.shape[0]):
for ind2 in range(p_kernel.shape[1]):
assert np.abs(true_kernel[ind1][ind2]
- p_kernel[ind1][ind2]) < 0.000001
示例5: test_affinity_mat_poly
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import polynomial_kernel [as 别名]
def test_affinity_mat_poly(data):
v1_data = data['fit_data'][0]
distances = cdist(v1_data, v1_data)
gamma = 1 / (2 * np.median(distances) ** 2)
true_kernel = polynomial_kernel(v1_data, gamma=gamma)
spectral = MultiviewCoRegSpectralClustering(random_state=RANDOM_STATE,
affinity='poly')
p_kernel = spectral._affinity_mat(v1_data)
assert(p_kernel.shape[0] == data['n_fit'])
assert(p_kernel.shape[1] == data['n_fit'])
for ind1 in range(p_kernel.shape[0]):
for ind2 in range(p_kernel.shape[1]):
assert np.abs(true_kernel[ind1][ind2]
- p_kernel[ind1][ind2]) < 0.000001
示例6: test_HPK_train
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import polynomial_kernel [as 别名]
def test_HPK_train(self):
Ktr = self.Xtr @ self.Xtr.T
self.assertTrue(matNear(Ktr,pairwise_sk.linear_kernel(self.Xtr)))
self.assertTrue(matNear(
pairwise_mk.homogeneous_polynomial_kernel(self.Xtr, degree=4),
pairwise_sk.polynomial_kernel(self.Xtr, degree=4, gamma=1, coef0=0)))
self.assertTrue(matNear(
pairwise_mk.homogeneous_polynomial_kernel(self.Xtr, degree=5),
pairwise_sk.polynomial_kernel(self.Xtr, degree=5, gamma=1, coef0=0)))
self.assertTrue(matNear(Ktr**3, pairwise_sk.polynomial_kernel(self.Xtr, degree=3, gamma=1, coef0=0)))
self.assertTrue(matNear(
pairwise_mk.homogeneous_polynomial_kernel(self.Xtr, self.Xtr, degree=3),
pairwise_sk.polynomial_kernel(self.Xtr, self.Xtr, degree=3, gamma=1, coef0=0)))
示例7: test_HPK_test
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import polynomial_kernel [as 别名]
def test_HPK_test(self):
Kte = self.Xte @ self.Xtr.T
self.assertTrue(matNear(Kte,
pairwise_mk.homogeneous_polynomial_kernel(self.Xte, self.Xtr, degree=1)))
self.assertTrue(matNear(
pairwise_mk.homogeneous_polynomial_kernel(self.Xte, self.Xtr, degree=4),
pairwise_sk.polynomial_kernel(self.Xte, self.Xtr, degree=4, gamma=1, coef0=0)))
示例8: test_otype
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import polynomial_kernel [as 别名]
def test_otype(self):
self.assertEqual(type(pairwise_mk.linear_kernel(self.Xtr)), torch.Tensor)
self.assertEqual(type(pairwise_mk.homogeneous_polynomial_kernel(self.Xtr)), torch.Tensor)
self.assertEqual(type(pairwise_mk.polynomial_kernel(self.Xtr)), torch.Tensor)
self.assertEqual(type(pairwise_mk.rbf_kernel(self.Xtr)), torch.Tensor)
示例9: test_numpy
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import polynomial_kernel [as 别名]
def test_numpy(self):
Xtr = self.Xtr.numpy()
self.assertTrue(matNear(
pairwise_mk.polynomial_kernel(Xtr, degree=4, gamma=0.1, coef0=2),
pairwise_sk.polynomial_kernel(Xtr, degree=4, gamma=0.1, coef0=2)))
self.assertTrue(matNear(
pairwise_mk.linear_kernel(Xtr),
pairwise_sk.linear_kernel(Xtr)))
示例10: test_nystroem_poly_kernel_params
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import polynomial_kernel [as 别名]
def test_nystroem_poly_kernel_params():
# Non-regression: Nystroem should pass other parameters beside gamma.
rnd = np.random.RandomState(37)
X = rnd.uniform(size=(10, 4))
K = polynomial_kernel(X, degree=3.1, coef0=.1)
nystroem = Nystroem(kernel="polynomial", n_components=X.shape[0],
degree=3.1, coef0=.1)
X_transformed = nystroem.fit_transform(X)
assert_array_almost_equal(np.dot(X_transformed, X_transformed.T), K)
示例11: _ker
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import polynomial_kernel [as 别名]
def _ker(self, x):
if self.basis == 'minimax':
# minimax polynomial kernel (Andrews et al., NIPS2002)
stat = lambda X: np.concatenate([X.max(axis=0), X.min(axis=0)])
sx = np.array([stat(X) for X in x])
sc = np.array([stat(X) for X in self._x_c])
K = polynomial_kernel(sx, sc, degree=self.degree)
return K
示例12: __init__
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import polynomial_kernel [as 别名]
def __init__(self, X, y, **kwargs):
# K: kernel matrix
super(QueryMultiLabelQUIRE, self).__init__(X, y)
self.lmbda = kwargs.pop('lambda', 1.)
self.kernel = kwargs.pop('kernel', 'rbf')
if self.kernel == 'rbf':
self.K = rbf_kernel(X=X, Y=X, gamma=kwargs.pop('gamma', 1.))
elif self.kernel == 'poly':
self.K = polynomial_kernel(X=X,
Y=X,
coef0=kwargs.pop('coef0', 1),
degree=kwargs.pop('degree', 3),
gamma=kwargs.pop('gamma', 1.))
elif self.kernel == 'linear':
self.K = linear_kernel(X=X, Y=X)
elif hasattr(self.kernel, '__call__'):
self.K = self.kernel(X=np.array(X), Y=np.array(X))
else:
raise NotImplementedError
if not isinstance(self.K, np.ndarray):
raise TypeError('K should be an ndarray')
if self.K.shape != (len(X), len(X)):
raise ValueError(
'Kernel should have size (%d, %d)' % (len(X), len(X)))
self._nsamples, self._nclass = self.y.shape
self.L = np.linalg.pinv(self.K + self.lmbda * np.eye(len(X)))
示例13: __init__
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import polynomial_kernel [as 别名]
def __init__(self, X, y, train_idx, **kwargs):
# K: kernel matrix
#
X = np.asarray(X)[train_idx]
y = np.asarray(y)[train_idx]
self._train_idx = np.asarray(train_idx)
self.y = np.array(y)
self.lmbda = kwargs.pop('lambda', 1.)
self.kernel = kwargs.pop('kernel', 'rbf')
if self.kernel == 'rbf':
self.K = rbf_kernel(X=X, Y=X, gamma=kwargs.pop('gamma', 1.))
elif self.kernel == 'poly':
self.K = polynomial_kernel(X=X,
Y=X,
coef0=kwargs.pop('coef0', 1),
degree=kwargs.pop('degree', 3),
gamma=kwargs.pop('gamma', 1.))
elif self.kernel == 'linear':
self.K = linear_kernel(X=X, Y=X)
elif hasattr(self.kernel, '__call__'):
self.K = self.kernel(X=np.array(X), Y=np.array(X))
else:
raise NotImplementedError
if not isinstance(self.K, np.ndarray):
raise TypeError('K should be an ndarray')
if self.K.shape != (len(X), len(X)):
raise ValueError(
'kernel should have size (%d, %d)' % (len(X), len(X)))
self.L = np.linalg.inv(self.K + self.lmbda * np.eye(len(X)))
示例14: trainModel
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import polynomial_kernel [as 别名]
def trainModel(data):
"""
在模型里使用不同的核函数
"""
kernel = [linear_kernel, polynomial_kernel, rbf_kernel, laplacian_kernel]
res = []
for i in kernel:
model = SVC(kernel=i, coef0=1)
model.fit(data[["x1", "x2"]], data["y"])
res.append({"name": i.__name__, "result": model})
return res
示例15: trainModel
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import polynomial_kernel [as 别名]
def trainModel(data):
"""
在模型里使用不同的核函数
"""
kernel = [polynomial_kernel, rbf_kernel]
res = []
for i in kernel:
model = SVC(kernel=i, coef0=1)
model.fit(data[["x1", "x2"]], data["y"])
res.append({"name": i.__name__, "result": model})
return res