I just started learning some numpy because of High performance calculation of least squares difference from all possible combinations (n lists):
Right now I’m stucked with the calculations and could use some help.
I have a numpy array object that looks like this:
>>> 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]])
I would like to get the number of the array with the least square difference among all his members, a numpythonic version of:
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
I would appreciate any hints.
Initialize your
NxMarray:Calculate the sum of squares between all elements of each of
NM-dimensional vectors and store the results in a list:Find the index of the vector with the smallest sum-of-squares between all its elements:
Or, to avoid the intermediate list: