I have a numpy array that I am using to complete a nearest neighbor calculation:
def all_distance_compute(matrix, vector):
diff = matrix[0:] - matrix[vector]
distances = np.sqrt(np.sum(diff**2, axis=1))
for i in range(len(distances)):
print i
print distances[i]
return distances
It seems to be working based on the result distances that is returned, however, I don't know how to look at all of the values in distances and return which element in the array is the minimum.
The for loop that I have in my function is purely for diagnostics, but I was thinking I could iterate thru this way and perhaps determine the minimum this way, but I also figured numpy probably has a better means of doing this.
EDIT:
So as I was typing out the question, I figured I would try my suggestion of iterating to find the minimum, and I changed my function to be this:
code
for i in range(len(distances)):
if distances[i] < min and distances[i] > 0:
min = distances[i]
mindex = i
return min, mindex
vector? Could you add a representative sample case?