当前位置: 首页>>代码示例>>Python>>正文


Python pairwise.polynomial_kernel方法代码示例

本文整理汇总了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 
开发者ID:IvanoLauriola,项目名称:MKLpy,代码行数:19,代码来源:komd.py

示例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 
开发者ID:ddbourgin,项目名称:numpy-ml,代码行数:22,代码来源:test_utils.py

示例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) 
开发者ID:scikit-learn-contrib,项目名称:polylearn,代码行数:20,代码来源:kernels.py

示例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 
开发者ID:neurodata,项目名称:mvlearn,代码行数:20,代码来源:test_spectral.py

示例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 
开发者ID:neurodata,项目名称:mvlearn,代码行数:20,代码来源:test_coreg.py

示例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))) 
开发者ID:IvanoLauriola,项目名称:MKLpy,代码行数:15,代码来源:unit_tests.py

示例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))) 
开发者ID:IvanoLauriola,项目名称:MKLpy,代码行数:9,代码来源:unit_tests.py

示例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) 
开发者ID:IvanoLauriola,项目名称:MKLpy,代码行数:7,代码来源:unit_tests.py

示例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))) 
开发者ID:IvanoLauriola,项目名称:MKLpy,代码行数:10,代码来源:unit_tests.py

示例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) 
开发者ID:PacktPublishing,项目名称:Mastering-Elasticsearch-7.0,代码行数:12,代码来源:test_kernel_approximation.py

示例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 
开发者ID:t-sakai-kure,项目名称:pywsl,代码行数:11,代码来源:pumil_mr.py

示例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))) 
开发者ID:NUAA-AL,项目名称:ALiPy,代码行数:29,代码来源:multi_label.py

示例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))) 
开发者ID:NUAA-AL,项目名称:ALiPy,代码行数:33,代码来源:query_labels.py

示例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 
开发者ID:GenTang,项目名称:intro_ds,代码行数:13,代码来源:kernel.py

示例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 
开发者ID:GenTang,项目名称:intro_ds,代码行数:13,代码来源:scale_variant.py


注:本文中的sklearn.metrics.pairwise.polynomial_kernel方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。