![](/img/trans.png)
[英]How to get the sum of total row where values greater than 20 and drop those less than a certain value
[英]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.