Yêu cầu thg 5 28, 2021 12:06 CH 193 0 2
  • 193 0 2
0

Ai giúp em giải thích công thức 1 - pairwise_distance với ạ

Chia sẻ
  • 193 0 2

Em đang mới học Python trong phần học máy, công thức này em chưa hiểu tại sao lại có Số bị trừ là 1. Em đã tìm đọc tìm hiểu trong thư viện sklearn rồi nhưng vẫn chưa hiểu tại sao ạ Anh chị giúp em với ạ

2 CÂU TRẢ LỜI


Đã trả lời thg 5 28, 2021 12:43 CH
Đã được chấp nhận
+2

Theo như mình vừa search qua thì link trên của bạn là link này: https://www.it-swarm-vi.com/vi/python/cach-nhanh-nhat-trong-python-de-tinh-do-tuong-tu-cosin-cho-du-lieu-ma-tran-thua-thot-la-gi/1041279579/

Còn link gốc của nó là cái này: https://stackoverflow.com/questions/17627219/whats-the-fastest-way-in-python-to-calculate-cosine-similarity-given-sparse-mat

Bản chất ở đây là tính cosine similiraty của ma trận thưa. Mình xin phép trả lời câu hỏi như sau:

Đoạn dist_out = 1-pairwise_distances(A, metric="cosine") là đoạn code bị ngáo đấy bạn =)). Mục đích là tính cosine similary thì chỉ cần dist_out = cosine_similarity(A) là đủ rồi, vì ngay trong chính code của hàm cosine_distances của sklearn cũng giải thích là pairwise distance = 1 - cosine similary rồi. Bạn có thể đọc chi tiết code ở đây: https://github.com/scikit-learn/scikit-learn/blob/3a64fecd1f1d30a17998b254f94613adee48a930/sklearn/metrics/pairwise.py#L829

def cosine_distances(X, Y=None):
    """Compute cosine distance between samples in X and Y.
    Cosine distance is defined as 1.0 minus the cosine similarity.
    Read more in the :ref:`User Guide <metrics>`.
    Parameters
    ----------
    X : {array-like, sparse matrix} of shape (n_samples_X, n_features)
        Matrix `X`.
    Y : {array-like, sparse matrix} of shape (n_samples_Y, n_features), \
            default=None
        Matrix `Y`.
    Returns
    -------
    distance matrix : ndarray of shape (n_samples_X, n_samples_Y)
    See Also
    --------
    cosine_similarity
    scipy.spatial.distance.cosine : Dense matrices only.
    """
    # 1.0 - cosine_similarity(X, Y) without copy
    S = cosine_similarity(X, Y)
    S *= -1
    S += 1
    np.clip(S, 0, 2, out=S)
    if X is Y or Y is None:
        # Ensure that distances between vectors and themselves are set to 0.0.
        # This may not be the case due to floating point rounding errors.
        S[np.diag_indices_from(S)] = 0.0
    return S
Chia sẻ
thg 5 30, 2021 4:09 CH

Mình cảm ơn bạn nhiều nhé :> hehe

Đã trả lời thg 5 28, 2021 12:10 CH
0

acccccc.png Ví dụ như này ạ. Tại sao lại có Số BỊ TRỪ là 1 ở đây ạ

Chia sẻ
Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí