I have a given list of values and a collection of lists (lists A
, B
, and C
) with similar values. I'm trying to find a way to return the list that most closely matches the given
list. I'd like to use a least squares fit as the distance metric.
given = [0, 1, 2, 3, 4, 5]
A = [0.1, 0.9, 2, 3.3, 3.6, 5.1]
B = [-0.1, 0.9, 2.1, 3.1, 3.9, 5]
C = [0, 1.1, 2, 2.9, 4, 5.1]
So in this case, it would return C
as the closest match to given
.
I thought I could incorporate something like:
match = [min([val[idx] for val in [A,B,C]], key=lambda x: abs(x-given[idx])) for idx in range(len(given))]
But that only returns the closest value for each list element. I'm not sure how to then identify list C as the closest point-by-point match.
Also, if the lists are different lengths, I really don't know what to do if I'm not comparing them index by index. For example:
given = [0, 1, 2, 3, 4, 5]
A = [0.1, 0.9, 2, 3.3, 3.6, 2, 5.1, 3, 6.8, 7.1, 8.2, 9]
B = [-0.1, 0.9, 2.1, 3.1, 3.9]
C = [-1.7, -1, 0, 1.1, 2, 2.9, 4, 5.1, 6, 7.1, 8]
would still return C
as the closest match.
I'm also using Numpy but haven't found anything useful. Any help would be greatly appreciated!