Python sklearn.metrics.pairwise.check_pairwise_arrays() Examples
The following are 15
code examples of sklearn.metrics.pairwise.check_pairwise_arrays().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
sklearn.metrics.pairwise
, or try the search function
.
Example #1
Source File: test_pairwise.py From Mastering-Elasticsearch-7.0 with MIT License | 6 votes |
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)
Example #2
Source File: test_pairwise.py From Mastering-Elasticsearch-7.0 with MIT License | 6 votes |
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)
Example #3
Source File: test_pairwise.py From twitter-stock-recommendation with MIT License | 6 votes |
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)
Example #4
Source File: test_pairwise.py From twitter-stock-recommendation with MIT License | 6 votes |
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)
Example #5
Source File: boolean.py From MKLpy with GNU General Public License v3.0 | 5 votes |
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)
Example #6
Source File: test_pairwise.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
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)
Example #7
Source File: test_pairwise.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
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)
Example #8
Source File: test_pairwise.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
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)
Example #9
Source File: test_pairwise.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
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)
Example #10
Source File: test_pairwise.py From twitter-stock-recommendation with MIT License | 5 votes |
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)
Example #11
Source File: test_pairwise.py From twitter-stock-recommendation with MIT License | 5 votes |
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)
Example #12
Source File: test_pairwise.py From twitter-stock-recommendation with MIT License | 5 votes |
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)
Example #13
Source File: test_pairwise.py From twitter-stock-recommendation with MIT License | 5 votes |
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)
Example #14
Source File: custom_distances.py From alphacsc with BSD 3-Clause "New" or "Revised" License | 4 votes |
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
Example #15
Source File: custom_distances.py From alphacsc with BSD 3-Clause "New" or "Revised" License | 4 votes |
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