由于High performance calculation of least squares difference from all possible combinations (n lists):
,我刚开始学习一些numpy现在我对计算很不满意,可以使用一些帮助。
我有一个numpy数组对象,如下所示:
>>> items
array([[ 246, 1143, 1491, ..., 1167, 325, 1158],
[ 246, 1143, 1491, ..., 1167, 519, 1158],
[ 246, 1143, 1491, ..., 1167, 507, 1158],
...,
[1491, 1143, 246, ..., 1167, 325, 1158],
[1491, 1143, 246, ..., 1167, 519, 1158],
[1491, 1143, 246, ..., 1167, 507, 1158]])
我想得到所有成员之间差异最小的阵列数,这是一个numpythonic版本:
for num,item in enumerate(items): #Calculate for each list of items
for n in range(len(item)):
for i in range(n, len(item)):
dist += (item[n]-item[i])**2 #Key formula
if dist>min_dist: #This is a shortcut
break
else:
continue
break
if min_dist is None or dist < min_dist:
min_dist = dist
best = num #We get the number of the combination we want
我很感激任何提示。
答案 0 :(得分:1)
初始化您的NxM
数组:
>>> import numpy as np
>>> items = np.random.random_sample((10,3))
计算每个N
M
维向量的所有元素之间的平方和,并将结果存储在列表中:
>>> sq = [(np.subtract.outer(item,item) ** 2).sum() for item in items]
找到所有元素之间具有最小平方和的矢量索引:
>>> best_index = np.argmin(sq)
或者,为了避免中间列表:
best = np.inf
best_index = None
for i,item in enumerate(items):
ls = (np.subtract.outer(item,item) ** 2).sum()
if ls < best:
best = ls
best_index = i
答案 1 :(得分:0)
import numpy as np
[(lambda(x):np.square(np.dot(x,-1*x)))(x) for x in items]