我有一个numpy数组大小(9126,12)和两个参考簇点(2,12),我试图为数组计算距离,以便标记它们。我在实践中理解这是如何发生的,但由于发送不同大小的数组,所以无法实现
我知道我可以使用numpy.linalg,但这是家庭作业的一部分,因此不允许这样做
ValueError: operands could not be broadcast together with shapes (9126,12) (2,12)
def euclid_dist(v1, v2):
return np.sqrt(((v1-v2)**2).sum(axis = 1))
def check_euclid_dist(data, reference_vectors):
npdata = data.to_numpy()
dst = euclid_dist(npdata, reference_vectors)
# Get the indices of minimum element in numpy array
result = np.where(dst == np.amin(dst))
print(result)
return result
您可以通过在两个数组中插入额外维度并让Numpy相互广播来计算每个向量到每个参考点的距离:
然后,您可以使用
np.argmin
找到最近的集群:相关问题 更多 >
编程相关推荐