繁体   English   中英

在列表的排列中应用排名

[英]Apply rank in permutation of a list

我有两个清单:

l1 = [0, 1, 12, 33, 41, 52, 69, 7.2, 8.9, 9.91]
l2 = [45, 51]

我需要从l1获得所有可能的组合(不重复),大小等于l2的长度。 然后将排名指标应用于l2l1 (对于每个组合)。 最后,我需要获得最接近的指标。 l1lxlx是置换列表)。

到目前为止我尝试过的(到目前为止更像是伪代码):

import numpy as np

def apply_metric(predictions, targets):
    return np.sqrt(((predictions - targets) ** 2).mean())

l1 = [0, 1, 12, 33, 41, 52, 69, 7.2, 8.9, 9.91]
l2 = [45, 51]

for item in l1:
    #do the possible combinations
    temp_result = apply_metric(np.array(l2), np.array(permuted_items))

输出:

best metric = 0 (identical)
best list = [45, 51]

您可以使用itertools permutation来获取您的排列列表,然后应用该指标。

import numpy as np
import itertools as it

def apply_metric(predictions, targets):
    return np.sqrt(((predictions - targets) ** 2).mean())

l1 = [0, 1, 12, 33, 41, 52, 69, 7.2, 8.9, 9.91]
l2 = [45, 51]

temp_dict = {}
for elements in it.permutations(l1, len(l2)):
    temp_result = apply_metric(np.array(l2), np.array(elements))
    temp_dict.update({temp_result : list(elements)})

print(f"Best metric: {min(temp_dict)}")
print(f"Best list: {temp_dict[min(temp_dict)]}")

其中产生:

Best metric: 2.9154759474226504
Best list: [41, 52]

暂无
暂无

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

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