本文整理汇总了Python中sklearn.metrics.pairwise.pairwise_kernels方法的典型用法代码示例。如果您正苦于以下问题:Python pairwise.pairwise_kernels方法的具体用法?Python pairwise.pairwise_kernels怎么用?Python pairwise.pairwise_kernels使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.metrics.pairwise
的用法示例。
在下文中一共展示了pairwise.pairwise_kernels方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _intertext_score
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import pairwise_kernels [as 别名]
def _intertext_score(full_text):
'''returns tuple of scored sentences
in order of appearance
Note: Doing an A/B test to
compare results, reverting to
original algorithm.'''
sentences = sentence_tokenizer(full_text)
norm = _normalize(sentences)
similarity_matrix = pairwise_kernels(norm, metric='cosine')
scores = _textrank(similarity_matrix)
scored_sentences = []
for i, s in enumerate(sentences):
scored_sentences.append((scores[i],i,s))
top_scorers = sorted(scored_sentences,
key=lambda tup: tup[0],
reverse=True)
return top_scorers
示例2: test_pairwise_kernels_callable
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import pairwise_kernels [as 别名]
def test_pairwise_kernels_callable():
# Test the pairwise_kernels helper function
# with a callable function, with given keywords.
rng = np.random.RandomState(0)
X = rng.random_sample((5, 4))
Y = rng.random_sample((2, 4))
metric = callable_rbf_kernel
kwds = {'gamma': 0.1}
K1 = pairwise_kernels(X, Y=Y, metric=metric, **kwds)
K2 = rbf_kernel(X, Y=Y, **kwds)
assert_array_almost_equal(K1, K2)
# callable function, X=Y
K1 = pairwise_kernels(X, Y=X, metric=metric, **kwds)
K2 = rbf_kernel(X, Y=X, **kwds)
assert_array_almost_equal(K1, K2)
示例3: test_pairwise_similarity_sparse_output
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import pairwise_kernels [as 别名]
def test_pairwise_similarity_sparse_output(metric, pairwise_func):
rng = np.random.RandomState(0)
X = rng.random_sample((5, 4))
Y = rng.random_sample((3, 4))
Xcsr = csr_matrix(X)
Ycsr = csr_matrix(Y)
# should be sparse
K1 = pairwise_func(Xcsr, Ycsr, dense_output=False)
assert issparse(K1)
# should be dense, and equal to K1
K2 = pairwise_func(X, Y, dense_output=True)
assert not issparse(K2)
assert_array_almost_equal(K1.todense(), K2)
# show the kernel output equal to the sparse.todense()
K3 = pairwise_kernels(X, Y=Y, metric=metric)
assert_array_almost_equal(K1.todense(), K3)
示例4: test_cosine_similarity
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import pairwise_kernels [as 别名]
def test_cosine_similarity():
# Test the cosine_similarity.
rng = np.random.RandomState(0)
X = rng.random_sample((5, 4))
Y = rng.random_sample((3, 4))
Xcsr = csr_matrix(X)
Ycsr = csr_matrix(Y)
for X_, Y_ in ((X, None), (X, Y),
(Xcsr, None), (Xcsr, Ycsr)):
# Test that the cosine is kernel is equal to a linear kernel when data
# has been previously normalized by L2-norm.
K1 = pairwise_kernels(X_, Y=Y_, metric="cosine")
X_ = normalize(X_)
if Y_ is not None:
Y_ = normalize(Y_)
K2 = pairwise_kernels(X_, Y=Y_, metric="linear")
assert_array_almost_equal(K1, K2)
示例5: test_mmk
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import pairwise_kernels [as 别名]
def test_mmk():
bags = [np.random.normal(size=(np.random.randint(10, 100), 10))
for _ in range(20)]
res = MeanMapKernel(gamma=2.38).fit_transform(bags)
for i in range(20):
for j in range(20):
exp = pairwise_kernels(bags[j], bags[i], metric='rbf', gamma=2.38)
assert_almost_equal(res[i, j], exp.mean(),
err_msg="({} to {})".format(i, j))
res = MeanMapKernel(kernel='linear').fit(bags[:5]).transform(bags[-2:])
for i in range(5):
for j in range(18, 20):
exp = pairwise_kernels(bags[j], bags[i], metric='linear')
assert_almost_equal(res[j - 18, i], exp.mean(),
err_msg="({} to {})".format(i, j))
# fails on wrong dimension
assert_raises(
ValueError,
lambda:MeanMapKernel().fit(bags).transform([np.random.randn(20, 8)]))
################################################################################
示例6: decision_function
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import pairwise_kernels [as 别名]
def decision_function(self, X):
"""Decision function of the SVM.
Parameters
----------
X : array-like, shape (n_samples, n_features)
The input data.
Returns
-------
y : array-like, shape (n_samples,)
The values of decision function.
"""
X = check_array(X)
if self.kernel == "linear":
return self.intercept_ + np.dot(X, self.coef_)
else:
K = pairwise_kernels(X, self.support_vectors_, metric=self.kernel,
**self.kernel_args)
return (self.intercept_ + np.sum(self.dual_coef_[np.newaxis, :] *
self.y * K, axis=1))
示例7: get_pairwise_matrix
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import pairwise_kernels [as 别名]
def get_pairwise_matrix(self, X, Y=None):
"""Calculates the pairwise similarity of atomic environments with
scikit-learn, and the pairwise metric configured in the constructor.
Args:
X(np.ndarray): Feature vector for the atoms in structure A
Y(np.ndarray): Feature vector for the atoms in structure B
Returns:
np.ndarray: NxM matrix of local similarities between structures A
and B, with N and M atoms respectively.
"""
if callable(self.metric):
params = self.kernel_params or {}
else:
params = {"gamma": self.gamma,
"degree": self.degree,
"coef0": self.coef0}
return pairwise_kernels(X, Y, metric=self.metric,
filter_params=True, **params)
示例8: _title_similarity_score
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import pairwise_kernels [as 别名]
def _title_similarity_score(full_text, title):
"""Similarity scores for sentences with
title in descending order"""
sentences = sentence_tokenizer(full_text)
norm = _normalize([title]+sentences)
similarity_matrix = pairwise_kernels(norm, metric='cosine')
return sorted(zip(
similarity_matrix[0,1:],
range(len(similarity_matrix)),
sentences
),
key = lambda tup: tup[0],
reverse=True
)
示例9: test_pairwise_kernels
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import pairwise_kernels [as 别名]
def test_pairwise_kernels(metric):
# Test the pairwise_kernels helper function.
rng = np.random.RandomState(0)
X = rng.random_sample((5, 4))
Y = rng.random_sample((2, 4))
function = PAIRWISE_KERNEL_FUNCTIONS[metric]
# Test with Y=None
K1 = pairwise_kernels(X, metric=metric)
K2 = function(X)
assert_array_almost_equal(K1, K2)
# Test with Y=Y
K1 = pairwise_kernels(X, Y=Y, metric=metric)
K2 = function(X, Y=Y)
assert_array_almost_equal(K1, K2)
# Test with tuples as X and Y
X_tuples = tuple([tuple([v for v in row]) for row in X])
Y_tuples = tuple([tuple([v for v in row]) for row in Y])
K2 = pairwise_kernels(X_tuples, Y_tuples, metric=metric)
assert_array_almost_equal(K1, K2)
# Test with sparse X and Y
X_sparse = csr_matrix(X)
Y_sparse = csr_matrix(Y)
if metric in ["chi2", "additive_chi2"]:
# these don't support sparse matrices yet
assert_raises(ValueError, pairwise_kernels,
X_sparse, Y=Y_sparse, metric=metric)
return
K1 = pairwise_kernels(X_sparse, Y=Y_sparse, metric=metric)
assert_array_almost_equal(K1, K2)
示例10: test_pairwise_kernels_filter_param
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import pairwise_kernels [as 别名]
def test_pairwise_kernels_filter_param():
rng = np.random.RandomState(0)
X = rng.random_sample((5, 4))
Y = rng.random_sample((2, 4))
K = rbf_kernel(X, Y, gamma=0.1)
params = {"gamma": 0.1, "blabla": ":)"}
K2 = pairwise_kernels(X, Y, metric="rbf", filter_params=True, **params)
assert_array_almost_equal(K, K2)
assert_raises(TypeError, pairwise_kernels, X, Y, "rbf", **params)
示例11: _get_kernel
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import pairwise_kernels [as 别名]
def _get_kernel(self, X, Y=None):
kernel_params = self._get_kernel_params()
if self.kernel == "gak":
return cdist_gak(X, Y, n_jobs=self.n_jobs, verbose=self.verbose,
**kernel_params)
else:
X_sklearn = to_sklearn_dataset(X)
if Y is not None:
Y_sklearn = to_sklearn_dataset(Y)
else:
Y_sklearn = Y
return pairwise_kernels(X_sklearn, Y_sklearn, metric=self.kernel,
n_jobs=self.n_jobs, **kernel_params)
示例12: transform
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import pairwise_kernels [as 别名]
def transform(self, X):
'''
Compute kernels from X to :attr:`features_`.
Parameters
----------
X : list of arrays or :class:`skl_groups.features.Features`
The bags to compute "from". Must have same dimension as
:attr:`features_`.
Returns
-------
K : array of shape ``[len(X), len(features_)]``
The kernel evaluations from X to :attr:`features_`.
'''
X = as_features(X, stack=True, bare=True)
Y = self.features_
if X.dim != Y.dim:
raise ValueError("MMK transform got dimension {} but had {} at fit"
.format(X.dim, Y.dim))
pointwise = pairwise_kernels(X.stacked_features, Y.stacked_features,
metric=self.kernel,
filter_params=True,
**self._get_kernel_params())
# TODO: is there a way to do this without a Python loop?
K = np.empty((len(X), len(Y)))
for i in range(len(X)):
for j in range(len(Y)):
K[i, j] = pointwise[X._boundaries[i]:X._boundaries[i+1],
Y._boundaries[j]:Y._boundaries[j+1]].mean()
return K
示例13: _get_kernel
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import pairwise_kernels [as 别名]
def _get_kernel(self, X, Y=None):
if callable(self.kernel_):
params = self.kernel_params_ or {}
else:
params = {'gamma': self.gamma_,
'degree': self.degree_,
'coef0': self.coef0_}
return pairwise_kernels(X, Y, metric=self.kernel_, filter_params=True, **params)
示例14: test_pairwise_precomputed
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import pairwise_kernels [as 别名]
def test_pairwise_precomputed():
for func in [pairwise_distances, pairwise_kernels]:
# Test correct shape
assert_raises_regexp(ValueError, '.* shape .*',
func, np.zeros((5, 3)), metric='precomputed')
# with two args
assert_raises_regexp(ValueError, '.* shape .*',
func, np.zeros((5, 3)), np.zeros((4, 4)),
metric='precomputed')
# even if shape[1] agrees (although thus second arg is spurious)
assert_raises_regexp(ValueError, '.* shape .*',
func, np.zeros((5, 3)), np.zeros((4, 3)),
metric='precomputed')
# Test not copied (if appropriate dtype)
S = np.zeros((5, 5))
S2 = func(S, metric="precomputed")
assert_true(S is S2)
# with two args
S = np.zeros((5, 3))
S2 = func(S, np.zeros((3, 3)), metric="precomputed")
assert_true(S is S2)
# Test always returns float dtype
S = func(np.array([[1]], dtype='int'), metric='precomputed')
assert_equal('f', S.dtype.kind)
# Test converts list to array-like
S = func([[1.]], metric='precomputed')
assert_true(isinstance(S, np.ndarray))
示例15: test_pairwise_parallel
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import pairwise_kernels [as 别名]
def test_pairwise_parallel():
wminkowski_kwds = {'w': np.arange(1, 5).astype('double'), 'p': 1}
metrics = [(pairwise_distances, 'euclidean', {}),
(pairwise_distances, wminkowski, wminkowski_kwds),
(pairwise_distances, 'wminkowski', wminkowski_kwds),
(pairwise_kernels, 'polynomial', {'degree': 1}),
(pairwise_kernels, callable_rbf_kernel, {'gamma': .1}),
]
for func, metric, kwds in metrics:
yield check_pairwise_parallel, func, metric, kwds