當前位置: 首頁>>代碼示例>>Python>>正文


Python pairwise.check_pairwise_arrays方法代碼示例

本文整理匯總了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) 
開發者ID:PacktPublishing,項目名稱:Mastering-Elasticsearch-7.0,代碼行數:22,代碼來源:test_pairwise.py

示例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) 
開發者ID:PacktPublishing,項目名稱:Mastering-Elasticsearch-7.0,代碼行數:26,代碼來源:test_pairwise.py

示例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) 
開發者ID:alvarobartt,項目名稱:twitter-stock-recommendation,代碼行數:22,代碼來源:test_pairwise.py

示例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) 
開發者ID:IvanoLauriola,項目名稱:MKLpy,代碼行數:11,代碼來源:boolean.py

示例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) 
開發者ID:PacktPublishing,項目名稱:Mastering-Elasticsearch-7.0,代碼行數:9,代碼來源:test_pairwise.py

示例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) 
開發者ID:PacktPublishing,項目名稱:Mastering-Elasticsearch-7.0,代碼行數:16,代碼來源:test_pairwise.py

示例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) 
開發者ID:PacktPublishing,項目名稱:Mastering-Elasticsearch-7.0,代碼行數:10,代碼來源:test_pairwise.py

示例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) 
開發者ID:PacktPublishing,項目名稱:Mastering-Elasticsearch-7.0,代碼行數:12,代碼來源:test_pairwise.py

示例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) 
開發者ID:alvarobartt,項目名稱:twitter-stock-recommendation,代碼行數:9,代碼來源:test_pairwise.py

示例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 
開發者ID:alphacsc,項目名稱:alphacsc,代碼行數:56,代碼來源:custom_distances.py

示例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 
開發者ID:alphacsc,項目名稱:alphacsc,代碼行數:59,代碼來源:custom_distances.py


注:本文中的sklearn.metrics.pairwise.check_pairwise_arrays方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。