本文整理汇总了Python中sklearn.metrics.pairwise.check_pairwise_arrays方法的典型用法代码示例。如果您正苦于以下问题:Python pairwise.check_pairwise_arrays方法的具体用法?Python pairwise.check_pairwise_arrays怎么用?Python pairwise.check_pairwise_arrays使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.metrics.pairwise
的用法示例。
在下文中一共展示了pairwise.check_pairwise_arrays方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_check_sparse_arrays
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import check_pairwise_arrays [as 别名]
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 issparse(XA_checked)
assert_equal(abs(XA_sparse - XA_checked).sum(), 0)
assert issparse(XB_checked)
assert_equal(abs(XB_sparse - XB_checked).sum(), 0)
XA_checked, XA_2_checked = check_pairwise_arrays(XA_sparse, XA_sparse)
assert issparse(XA_checked)
assert_equal(abs(XA_sparse - XA_checked).sum(), 0)
assert issparse(XA_2_checked)
assert_equal(abs(XA_2_checked - XA_checked).sum(), 0)
示例2: test_check_preserve_type
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import check_pairwise_arrays [as 别名]
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)
示例3: test_check_sparse_arrays
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import check_pairwise_arrays [as 别名]
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(issparse(XA_checked))
assert_equal(abs(XA_sparse - XA_checked).sum(), 0)
assert_true(issparse(XB_checked))
assert_equal(abs(XB_sparse - XB_checked).sum(), 0)
XA_checked, XA_2_checked = check_pairwise_arrays(XA_sparse, XA_sparse)
assert_true(issparse(XA_checked))
assert_equal(abs(XA_sparse - XA_checked).sum(), 0)
assert_true(issparse(XA_2_checked))
assert_equal(abs(XA_2_checked - XA_checked).sum(), 0)
示例4: monotone_dnf_kernel
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import check_pairwise_arrays [as 别名]
def monotone_dnf_kernel(X,Z=None,d=2,c=2):
X, Z = check_pairwise_arrays(X, Z)
n = X.shape[1]
n_c = binom(n,c)
XX = np.dot(X.sum(axis=1).reshape(X.shape[0],1), np.ones((1,Z.shape[0])))
ZZ = np.dot(T.sum(axis=1).reshape(Z.shape[0],1), np.ones((1,X.shape[0])))
XXc = binom(XX,c)
ZZc = binom(ZZ,c)
return binom(n_c,d) - binom(n_c - XXc, d) - binom(n_c - ZZc.T, d) + binom(my_mdk(X,Z,c),d)
示例5: test_check_dense_matrices
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import check_pairwise_arrays [as 别名]
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: test_check_XB_returned
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import check_pairwise_arrays [as 别名]
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)
XB = np.resize(np.arange(40), (5, 8))
XA_checked, XB_checked = check_paired_arrays(XA, XB)
assert_array_equal(XA, XA_checked)
assert_array_equal(XB, XB_checked)
示例7: test_check_different_dimensions
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import check_pairwise_arrays [as 别名]
def test_check_different_dimensions():
# Ensure an error is raised if the dimensions are different.
XA = np.resize(np.arange(45), (5, 9))
XB = np.resize(np.arange(32), (4, 8))
assert_raises(ValueError, check_pairwise_arrays, XA, XB)
XB = np.resize(np.arange(4 * 9), (4, 9))
assert_raises(ValueError, check_paired_arrays, XA, XB)
示例8: test_check_invalid_dimensions
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import check_pairwise_arrays [as 别名]
def test_check_invalid_dimensions():
# Ensure an error is raised on 1D input arrays.
# The modified tests are not 1D. In the old test, the array was internally
# converted to 2D anyways
XA = np.arange(45).reshape(9, 5)
XB = np.arange(32).reshape(4, 8)
assert_raises(ValueError, check_pairwise_arrays, XA, XB)
XA = np.arange(45).reshape(9, 5)
XB = np.arange(32).reshape(4, 8)
assert_raises(ValueError, check_pairwise_arrays, XA, XB)
示例9: test_check_dense_matrices
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import check_pairwise_arrays [as 别名]
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_true(XA_checked is XB_checked)
assert_array_equal(XA, XA_checked)
示例10: roll_invariant_euclidean_distances
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import check_pairwise_arrays [as 别名]
def roll_invariant_euclidean_distances(X, Y=None, squared=False):
"""
Considering the rows of X (and Y=X) as vectors, compute the
distance matrix between each pair of vectors.
The distance is the minimum of the euclidean distance over all rolls:
dist(x, y) = min_\tau(||x(t) - y(t - \tau)||^2)
Parameters
----------
X : array, shape (n_samples_1, n_features)
Y : array, shape (n_samples_2, n_features)
squared : boolean
Not used. Only for API compatibility.
Returns
-------
distances : array, shape (n_samples_1, n_samples_2)
"""
X = np.atleast_2d(X)
if Y is not None:
Y = np.atleast_2d(Y)
X, Y = check_pairwise_arrays(X, Y)
n_samples_1, n_features = X.shape
n_samples_2, n_features = Y.shape
X_norm = np.power(np.linalg.norm(X, axis=1), 2)
Y_norm = np.power(np.linalg.norm(Y, axis=1), 2)
# n_pads = 0
# n_fft = next_fast_len(n_features + n_pads)
n_fft = n_features # not fast but otherwise the distance is wrong
X_hat = rfft(X, n_fft, axis=1)
Y_hat = rfft(Y, n_fft, axis=1).conj()
# # broadcasting can have a huge memory cost
# XY_hat = X_hat[:, None, :] * Y_hat[None, :, :]
# XY = irfft(XY_hat, n_fft, axis=2).max(axis=2)
# distances = X_norm[:, None] + Y_norm[None, :] - 2 * XY
distances = np.zeros((n_samples_1, n_samples_2))
if n_samples_2 > 1:
print('RIED on %s samples, this might be slow' % (distances.shape, ))
for ii in range(n_samples_1):
for jj in range(n_samples_2):
XY = irfft(X_hat[ii] * Y_hat[jj], n_fft).max()
distances[ii, jj] = X_norm[ii] + Y_norm[jj] - 2 * XY
distances += 1e-12
return distances
示例11: translation_invariant_euclidean_distances
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import check_pairwise_arrays [as 别名]
def translation_invariant_euclidean_distances(X, Y=None, squared=False,
symmetric=False):
"""
Considering the rows of X (and Y=X) as vectors, compute the
distance matrix between each pair of vectors.
The distance is the minimum of the euclidean distance over a set of
translations:
dist(x, y) = min_{i, j}(||x(i:i+T) - y(j:j+T)||^2)
where T = n_features / 2, and 1 <= i, j <= n_features / 2
Parameters
----------
X : array, shape (n_samples_1, n_features)
Y : array, shape (n_samples_2, n_features)
squared : boolean
Not used. Only for API compatibility.
symmetric : boolean
If False, the distance is not symmetric anymore, since we keep indice
j fixed at `n_features / 4`.
Returns
-------
distances : array, shape (n_samples_1, n_samples_2)
"""
X = np.atleast_2d(X)
if Y is not None:
Y = np.atleast_2d(Y)
X, Y = check_pairwise_arrays(X, Y)
n_samples_1, n_features = X.shape
n_samples_2, n_features = Y.shape
distances = np.zeros((n_samples_1, n_samples_2))
# if n_samples_2 > 1:
# print('TIED on %s samples, this might be slow' % (distances.shape, ))
for nn in range(n_samples_1):
for mm in range(n_samples_2):
XY = (X[nn, :, None] - Y[mm, None, :]) ** 2
if symmetric:
jj_range = np.arange(n_features // 2)
else:
jj_range = [n_features // 4]
dist = np.zeros((n_features // 2, len(jj_range)))
for ii in range(n_features // 2):
for jj, kk in enumerate(jj_range):
xy = XY[ii:ii + n_features // 2, kk:kk + n_features // 2]
dist[ii, jj] = xy.trace(axis1=0, axis2=1)
distances[nn, mm] = dist.min()
return distances