# Find 10 most similar data points from 100?

I am trying to solve a problem where I need to group battery cells that are most similar to each other to form a reliable battery pack. It seems like a clustering problem but I only need to find the top 10 most similar cells from 100.

Is there any way i can solve this?

• What sort of data has been given to you? Mar 26 at 12:23

That is often called nearest-neighbor search.

The most common methods require a distance metric. Given the features of battery pack, how close to each other are they?

• Thanks for the hint. Actually, i am in the process of collecting data. Mar 31 at 6:16

As you mention it might be solved via clustering, but given you need the top n to each other you can go as follows:

Assuming you have matrix X of nxm (n- batteries m- features/attributes of each one)

1. Define a distance metric (Euclidean, Mahalanobis, etc)
2. Calculate the distance between a battery j and all the other batteries i - j
3. Sort the top n distances from a battery

In pseudo code it will be something like this:

def kclosest(frame, battery_id, metric = "euclidean", top = 10):
"""
Return the top n closest from battery id
"""
distances = list()
for index, row in frame.iterrows():
d = metric(frame[frame.id == battery_id].values, row)
distances.append(d)
return sorted(distances)[1:top] # The closest point will always be the itself that's why we get from 1 to top