[英]How to find a vector from a list of vectors that is nearest to all other vectors using Python?
我有一個向量列表作為一個numpy數組。
[[ 1., 0., 0.],
[ 0., 1., 2.] ...]
它們都具有相同的尺寸。 我如何發現在向量空間中哪個向量最接近數組中所有其他向量? 有計算這個的scipy或sklearn函數嗎?
Update
:
“最接近”是指余弦和歐幾里得距離。
Update 2
:
假設我有4個向量(a,b,c,d),向量之間的余弦距離為:
a,b = 0.2
a,c = 0.9
a,d = 0.7
b,c = 0.5
b,d = 0.75
c,d = 0.8
因此,對於每個向量,我得到:
{
'a': [1,0.2,0.9,0.7],
'b': [0.2,1,0.5,0.75],
'c' : [0.9,0.5,1,0.75],
'd' : [0.7,0.75,0.8,1]
}
可以說矢量d是與a,b,c最相似的一個嗎?
您可以像這樣蠻力。 請注意,這是O(n ^ 2),並且對於大n會變慢。
import numpy as np
def cost_function(v1, v2):
"""Returns the square of the distance between vectors v1 and v2."""
diff = np.subtract(v1, v2)
# You may want to take the square root here
return np.dot(diff, diff)
n_vectors = 5
vectors = np.random.rand(n_vectors,3)
min_i = -1
min_cost = 0
for i in range (0, n_vectors):
sum_cost = 0.0
for j in range(0, n_vectors):
sum_cost = sum_cost + cost_function(vectors[i,:],vectors[j,:])
if min_i < 0 or min_cost > sum_cost:
min_i = i
min_cost = sum_cost
print('{} at {}: {:.3f}'.format(i, vectors[i,:], sum_cost))
print('Lowest cost point is {} at {}: {:.3f}'.format(min_i, vectors[min_i,:], min_cost))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.