【发布时间】:2020-04-14 04:18:52
【问题描述】:
我需要计算每个像素和每个质心之间的距离。
参数:
- X(numpy 数组):PxD 第一组数据点(通常是数据点)
- C(numpy 数组):KxD 第二组数据点(通常是聚类质心点)
返回:
- dist:PxK numpy 数组位置 ij 是第一个集合的第 i 个点与第二个集合的第 j 个点之间的距离
def distance(X, C):
dist = numpy.empty((X.shape[0], C.shape[0]))
for i,x in enumerate(X):
for y,c in enumerate(C):
dist[i][y] = euclidean_dist(x,c)
return dist
def euclidean_dist(x, y):
x1, y1, z1 = x
x2, y2, z2 = y
return math.sqrt((x1-x2)**2 + (y1-y2)**2 + (z1-z2)**2)
【问题讨论】:
-
您有错误吗?你的代码有什么问题?不应该是
dist[i,y]吗? -
不,没有错误。我怎样才能更快地使用矩阵 / 数组 / numpy 来处理它。
标签: python arrays numpy optimization