本文整理汇总了Python中sklearn.metrics.pairwise.check_pairwise_arrays函数的典型用法代码示例。如果您正苦于以下问题:Python check_pairwise_arrays函数的具体用法?Python check_pairwise_arrays怎么用?Python check_pairwise_arrays使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了check_pairwise_arrays函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _pairwise_callable
def _pairwise_callable(X, Y, metric, **kwds):
"""Handle the callable case for pairwise_{distances,kernels}
"""
try:
X, Y = check_pairwise_arrays(X, Y)
except ValueError:
X, Y = check_pairwise_arrays(X, Y, dtype=object) # try not to convert
if X is Y:
# Only calculate metric for upper triangle
out = np.zeros((X.shape[0], Y.shape[0]), dtype='float')
iterator = itertools.combinations(range(X.shape[0]), 2)
for i, j in iterator:
out[i, j] = metric(X[i], Y[j], **kwds)
# Make symmetric
# NB: out += out.T will produce incorrect results
out = out + out.T
# Calculate diagonal
# NB: nonzero diagonals are allowed for both metrics and kernels
for i in range(X.shape[0]):
x = X[i]
out[i, i] = metric(x, x, **kwds)
else:
# Calculate all cells
out = np.empty((X.shape[0], Y.shape[0]), dtype='float')
iterator = itertools.product(range(X.shape[0]), range(Y.shape[0]))
for i, j in iterator:
out[i, j] = metric(X[i], Y[j], **kwds)
return out
示例2: MaternKernel
def MaternKernel(X, Y=None, gamma = None, p = 0):
"""Compute the generalized normal kernel between X and Y.
The generalized normal kernel is defined as::
K(x, y) = exp(-gamma ||x-y||_1^beta)
for each pair of rows x in X and y in Y.
Parameters
----------
X : array of shape (n_samples_X, n_features)
Y : array of shape (n_samples_Y, n_features)
gamma : float
Returns
-------
kernel_matrix : array of shape (n_samples_X, n_samples_Y)
"""
assert(p == int(p))
X, Y = check_pairwise_arrays(X, Y)
if gamma is None:
gamma = 1.0 / X.shape[1]
r = manhattan_distances(X, Y)
if p == 0:
K = -gamma * r
np.exp(K, K) # exponentiate K in-place
if p == 1:
K = -gamma * r * math.sqrt(3)
np.exp(K, K) # exponentiate K in-place
K *= (1+gamma * r * math.sqrt(3))
if p == 1:
K = -gamma * r * math.sqrt(5)
np.exp(K, K) # exponentiate K in-place
K *= (1+gamma * r * math.sqrt(5) + 5./3. * (r*gamma)**2)
return K
示例3: poisson_kernel
def poisson_kernel(X, Y=None, gamma=None, Sigma_inv = None):
"""
Compute the poisson kernel between X and Y::
K(x, y) = exp(-gamma ||x-mu||^2/mu)
mu = centroid of X (=X if X.shape[0] == 1)
for each pair of rows x in X and y in Y.
Parameters
----------
X : array of shape (n_samples_X, n_features)
Y : array of shape (n_samples_Y, n_features)
gamma : float
Returns
-------
kernel_matrix : array of shape (n_samples_X, n_samples_Y)
"""
X, Y = check_pairwise_arrays(X, Y)
if gamma is None:
gamma = 1.0 / X.shape[1]
if Sigma_inv is None:
raise ValueError('Missing Sigma_inv')
v = X - Y
K = -0.5 * gamma * np.sqrt(v.dot(Sigma_inv).dot(v.T))
np.exp(K, K) # exponentiate K in-place
return K
示例4: trimmedrbf_kernel
def trimmedrbf_kernel(X, Y=None, gamma=None, robust_gamma = None):
"""
Compute the rbf (gaussian) kernel between X and Y::
K(x, y) = exp(-gamma ||x-y||**2)
for each pair of rows x in X and y in Y.
Parameters
----------
X : array of shape (n_samples_X, n_features)
Y : array of shape (n_samples_Y, n_features)
gamma : float
Returns
-------
kernel_matrix : array of shape (n_samples_X, n_samples_Y)
"""
X, Y = check_pairwise_arrays(X, Y)
if gamma is None:
gamma = 1.0 / X.shape[1]
K = euclidean_distances(X, Y, squared=True)
print K
print "SHape kernel" + str(np.where(np.sqrt(K) > robust_gamma)[0].shape)
K[np.where(np.sqrt(K) > robust_gamma)] = robust_gamma**2
K *= -gamma
np.exp(K, K) # exponentiate K in-place
return K
示例5: test_check_dense_matrices
def test_check_dense_matrices():
# Ensure that pairwise array check works for dense matrices.
# Check that if XB is None, XB is returned as reference to XA
XA = np.resize(np.arange(40), (5, 8))
XA_checked, XB_checked = check_pairwise_arrays(XA, None)
assert XA_checked is XB_checked
assert_array_equal(XA, XA_checked)
示例6: histogram_intersection_kernel
def histogram_intersection_kernel(X, Y = None, alpha = None, beta = None):
"""
Source: https://github.com/kuantkid/scikit-learn/commit/16c82d8f2fe763df7bfee9bbcc40016fb84affcf
Author: kuantkid
Date: Nov 20, 2012
Compute the histogram intersection kernel(min kernel)
between X and Y::
K(x, y) = \\sum_i^n min(|x_i|^\x07lpha, |y_i|^\x08eta)
Parameters
----------
X : array of shape (n_samples_1, n_features)
Y : array of shape (n_samples_2, n_features)
gamma : float
Returns
-------
Gram matrix : array of shape (n_samples_1, n_samples_2)
"""
(X, Y,) = pairwise.check_pairwise_arrays(X, Y)
if alpha is not None:
X = np.abs(X) ** alpha
if beta is not None:
Y = np.abs(Y) ** beta
(n_samples_1, n_features,) = X.shape
(n_samples_2, _,) = Y.shape
K = np.zeros(shape=(n_samples_1, n_samples_2), dtype=np.float)
for i in range(n_samples_1):
K[i] = np.sum(np.minimum(X[i], Y), axis=1)
return K
示例7: GeneralizedNormalKernel
def GeneralizedNormalKernel(X, Y=None, gamma = None, beta = 1):
"""Compute the generalized normal kernel between X and Y.
The generalized normal kernel is defined as::
K(x, y) = exp(-gamma ||x-y||_1^beta)
for each pair of rows x in X and y in Y.
Parameters
----------
X : array of shape (n_samples_X, n_features)
Y : array of shape (n_samples_Y, n_features)
gamma : float
Returns
-------
kernel_matrix : array of shape (n_samples_X, n_samples_Y)
"""
X, Y = check_pairwise_arrays(X, Y)
if gamma is None:
gamma = 1.0 / X.shape[1]
if beta == 1:
K = -gamma * manhattan_distances(X, Y)
else:
K = -gamma * manhattan_distances(X, Y) ** beta
np.exp(K, K) # exponentiate K in-place
return K
示例8: test_check_sparse_arrays
def test_check_sparse_arrays():
""" Ensures that checks return valid sparse matrices. """
rng = np.random.RandomState(0)
XA = rng.random_sample((5, 4))
XA_sparse = csr_matrix(XA)
XB = rng.random_sample((5, 4))
XB_sparse = csr_matrix(XB)
XA_checked, XB_checked = check_pairwise_arrays(XA_sparse, XB_sparse)
# compare their difference because testing csr matrices for
# equality with '==' does not work as expected.
assert_true(abs(XA_sparse - XA_checked).nnz == 0)
assert_true(abs(XB_sparse - XB_checked).nnz == 0)
XA_checked, XB_checked = check_pairwise_arrays(XA_sparse, XA_sparse)
assert_true(XA_sparse == XB_checked)
assert_true(abs(XA_sparse - XA_checked).nnz == 0)
示例9: test_check_XB_returned
def test_check_XB_returned():
""" Ensure that if XA and XB are given correctly, they return as equal."""
# Check that if XB is not None, it is returned equal.
# Note that the second dimension of XB is the same as XA.
XA = np.resize(np.arange(40), (5, 8))
XB = np.resize(np.arange(32), (4, 8))
XA_checked, XB_checked = check_pairwise_arrays(XA, XB)
assert_array_equal(XA, XA_checked)
assert_array_equal(XB, XB_checked)
示例10: test_check_sparse_arrays
def test_check_sparse_arrays():
""" Ensures that checks return valid sparse matrices. """
rng = np.random.RandomState(0)
XA = rng.random_sample((5, 4))
XA_sparse = csr_matrix(XA)
XB = rng.random_sample((5, 4))
XB_sparse = csr_matrix(XB)
XA_checked, XB_checked = check_pairwise_arrays(XA_sparse, XB_sparse)
assert_equal(XA_sparse, XA_checked)
assert_equal(XB_sparse, XB_checked)
示例11: test_check_tuple_input
def test_check_tuple_input():
# Ensures that checks return valid tuples.
rng = np.random.RandomState(0)
XA = rng.random_sample((5, 4))
XA_tuples = tuplify(XA)
XB = rng.random_sample((5, 4))
XB_tuples = tuplify(XB)
XA_checked, XB_checked = check_pairwise_arrays(XA_tuples, XB_tuples)
assert_array_equal(XA_tuples, XA_checked)
assert_array_equal(XB_tuples, XB_checked)
示例12: test_check_preserve_type
def test_check_preserve_type():
# Ensures that type float32 is preserved.
XA = np.resize(np.arange(40), (5, 8)).astype(np.float32)
XB = np.resize(np.arange(40), (5, 8)).astype(np.float32)
XA_checked, XB_checked = check_pairwise_arrays(XA, None)
assert_equal(XA_checked.dtype, np.float32)
# both float32
XA_checked, XB_checked = check_pairwise_arrays(XA, XB)
assert_equal(XA_checked.dtype, np.float32)
assert_equal(XB_checked.dtype, np.float32)
# mismatched A
XA_checked, XB_checked = check_pairwise_arrays(XA.astype(np.float), XB)
assert_equal(XA_checked.dtype, np.float)
assert_equal(XB_checked.dtype, np.float)
# mismatched B
XA_checked, XB_checked = check_pairwise_arrays(XA, XB.astype(np.float))
assert_equal(XA_checked.dtype, np.float)
assert_equal(XB_checked.dtype, np.float)
示例13: _cosine_distances_prenorm
def _cosine_distances_prenorm(self, X, Y):
"""
Return cosine distances based on a prenormalized vectors.
It allows for much faster computation of cosine distances.
"""
if not self.prenorm:
raise Exception(
'Vectors must be prenormalized!')
if Y is None:
Y = X
X, Y = smp.check_pairwise_arrays(X, Y)
sims = X.dot(Y.T)
if scipy.sparse.issparse(sims):
sims = sims.todense()
return 1 - sims
示例14: first_periodic_kernel
def first_periodic_kernel(X, Y=None, gamma=None, period=None):
# TODO: Add mathematical form of the kernel in the docstring
"""Compute the first periodic kernel between *X* and *Y*.
Parameters
----------
X : array of shape (n_samples_X, n_features)
Y : array of shape (n_samples_Y, n_features)
gamma : float, default None
If None, default to 1.0 / n_samples_X
period : float, default None
If None, default to 2 * pi.
This parameter should not be default as
wrong estimation lead to poor learning score.
Returns
-------
kernel_matrix : array of shape (n_samples_X, n_samples_Y)
"""
X, Y = check_pairwise_arrays(X, Y)
if gamma is None:
gamma = 0.8
if period is None:
period = 2. * pi
a = -log(gamma) / period
b = 2 * pi / period
c = sqrt(pi / a) * (exp(- b ** 2 / (4 * a)) + 1)
K = euclidean_distances(X, Y, squared=True)
# TODO: Optimize to avoid temporary?
return exp(-a * K) * (1 + cos(b * sqrt(K))) / c
示例15: cosine_similarity
def cosine_similarity(X, Y=None):
"""Compute cosine similarity between samples in X and Y.
Cosine similarity, or the cosine kernel, computes similarity as the
normalized dot product of X and Y:
K(X, Y) = <X, Y> / (||X||*||Y||)
On L2-normalized data, this function is equivalent to linear_kernel.
Parameters
----------
X : array_like, sparse matrix
with shape (n_samples_X, n_features).
Y : array_like, sparse matrix (optional)
with shape (n_samples_Y, n_features).
Returns
-------
kernel matrix : array_like
An array with shape (n_samples_X, n_samples_Y).
"""
# to avoid recursive import
X, Y = check_pairwise_arrays(X, Y)
X_normalized = normalize(X, copy=True)
if X is Y:
Y_normalized = X_normalized
else:
Y_normalized = normalize(Y, copy=True)
K = linear_kernel(X_normalized, Y_normalized)
return K