我刚开始学习一些 numpy,因为高性能计算所有可能组合(n 个列表)的最小二乘差:
现在我被计算困住了,可以使用一些帮助。
我有一个如下所示的 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
我会很感激任何提示。