Reputation: 1080
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.
Upvotes: 4
Views: 1833
Reputation: 13485
Initialize your NxM
array:
>>> import numpy as np
>>> items = np.random.random_sample((10,3))
Calculate the sum of squares between all elements of each of N
M
-dimensional vectors and store the results in a list:
>>> sq = [(np.subtract.outer(item,item) ** 2).sum() for item in items]
Find the index of the vector with the smallest sum-of-squares between all its elements:
>>> best_index = np.argmin(sq)
Or, to avoid the intermediate list:
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
Upvotes: 1
Reputation: 17933
import numpy as np
[(lambda(x):np.square(np.dot(x,-1*x)))(x) for x in items]
Upvotes: 0