简体   繁体   English

列表内列表之间的欧几里德距离

[英]eucledian distance between lists inside a list

I would like to calculate eucledian distance between lists inside a list and if that distance is smaller than threshold than get maximum element of such lists.我想计算列表内列表之间的欧几里德距离,以及该距离是否小于阈值,而不是获取此类列表的最大元素。

My solution gives me distance between each two lists, but I want to comapere every list with every other.我的解决方案给了我每两个列表之间的距离,但我想将每个列表相互比较。 Basically, a two loop solution probably.基本上,可能是两个循环解决方案。

yhat = [[10 , 15, 200 ,220], [20 , 25, 200 ,230], [30 , 15, 200 ,230], [100 , 150, 230 ,300], [110 , 150, 240 ,300] ]

def euclidean(v1, v2):
    return sum((p-q)**2 for p, q in zip(v1, v2)) ** .5

it = iter(yhat)
prev = next(it)
ec =[]
for ind, ele in enumerate(it):
    ec.append(euclidean(ele, prev))
    prev = ele
ec

To summarize, I would like a new list xhat which contains elements:总而言之,我想要一个包含元素的新列表xhat

xhat = [[30 , 35, 200 ,230], [110 , 150, 240 ,300] ]

You can use enumerate and itertools.combinations to make this rather short:您可以使用enumerateitertools.combinations来缩短它:

from itertools import combinations

out = defaultdict(lambda: defaultdict(dict))
for (i, v1), (j, v2) in combinations(enumerate(yhat), 2):
    out.setdefault(i, {})[j] = euclidean(v1, v2)

out
{0: {1: 17.320508075688775, 2: 22.360679774997898, 3: 183.3712082089225, 4: 190.3286631067428}, 
 1: {2: 14.142135623730951, 3: 166.80827317612278, 4: 173.8533865071371}, 
 2: {3: 170.07351351694948, 4: 176.4227876437735}, 
 3: {4: 14.142135623730951}}

where out maps to indeces in your input list to the distance between the vectors at those indeces.其中 out 映射到输入列表中的索引到这些索引处的向量之间的距离。 You could get the max elements of the vectors whose distance is smaller than the threshold like:您可以获得距离小于阈值的向量的最大元素,例如:

for (i, v1), (j, v2) in combinations(enumerate(yhat), 2):
    if euclidean(v1, v2) < threshold:
        out.setdefault(i, {})[j] = (max(v1), max(v2))
out
{0: {1: (220, 230), 2: (220, 230), 3: (220, 300), 4: (220, 300)}, 
 1: {2: (230, 230), 3: (230, 300), 4: (230, 300)}, 
 2: {3: (230, 300), 4: (230, 300)}, 
 3: {4: (300, 300)}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM