簡體   English   中英

如何找到大於某個余弦距離值的值對?

[英]How to find pairs of values greater than a certain cosine distance value?

我有一個數組:

[[ 0.32730174 -0.1436172  -0.3355202  -0.2982458 ]
 [ 0.50490916 -0.33826587  0.4315952   0.4850834 ]
 [-0.18594801 -0.06028342 -0.24817085 -0.41029227]
 [-0.22551994  0.47151482 -0.39798814 -0.14978702]
 [-0.3315491   0.05832376 -0.29526958  0.3786153 ]]

我用“pdist”計算了它的余弦距離, cosine_distance=1-pdist(array, metric='cosine')並獲得了距離數組:

[-0.14822659  0.51635946  0.09485546 -0.38855427 -0.82434624 -0.86407176
 -0.25101774  0.49793639 -0.07881047  0.41272145]

現在,我想只獲得余弦距離大於0.4且小於0.49的那些對。 我已經計算出大於0.4的值的數量,通過number_points=len([1 for i in cosine_distance if i >= 0.4]) 但是不能得到那些對。

訣竅在於pdist輸出的描述。

Y:ndarray

返回一個壓縮距離矩陣Y.對於每個和(where),其中m是原始觀察的數量。 度量dist(u = X [i],v = X [j])被計算並存儲在條目ij中。

文檔還引用了方形 ,使距離向量再次成為矩陣。 那么輸出數組的文檔說明是有道理的。 文檔中的ij位置將是squareform操作創建的矩陣的第一個和第二個索引。 然后我們可以得到每個點對的每個距離。

distance_matrix = squareform(cosine_distances_array)
points_to_keep = []

for (i in range(len(points)-1)):
    for (j in range(i+1, len(points))):
        if(distance_matrix[i,j] > 0.4):
            points_to_keep.push((points[i], points[j]))

print points_to_keep

為什么不

number_points=len([1 for i in cosine_distance if i >= 0.4 and i <= 0.49])

如果需要跟蹤范圍內的哪一對,請使用enumerate

number_points = [idx for idx, i in enumerate(cosine_distance) if i >= 0.4 and i <= 0.49]

這將為您提供一個列表,其中包含滿足條件的對的索引。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM