【发布时间】:2015-06-13 19:32:33
【问题描述】:
我有一个带有双精度的向量,我想对其进行排名(实际上它是一个向量,其中的对象具有一个名为costs 的双精度成员)。如果只有唯一值或者我忽略非唯一值,那么就没有问题。但是,我想对非唯一值使用平均排名。此外,我在 SO 上发现了一些关于排名的问题,但它们忽略了非唯一值。
例如,假设我们有 (1, 5, 4, 5, 5),那么对应的排名应该是 (1, 4, 2, 4, 4)。当我们忽略非唯一值时,排名为 (1, 3, 2, 4, 5)。
当忽略非唯一值时,我使用了以下内容:
void Population::create_ranks_costs(vector<Solution> &pop)
{
size_t const n = pop.size();
// Create an index vector
vector<size_t> index(n);
iota(begin(index), end(index), 0);
sort(begin(index), end(index),
[&pop] (size_t idx, size_t idy) {
return pop[idx].costs() < pop[idy].costs();
});
// Store the result in the corresponding solutions
for (size_t idx = 0; idx < n; ++idx)
pop[index[idx]].set_rank_costs(idx + 1);
}
有谁知道如何考虑非唯一值?我更喜欢使用std::algorithm,因为 IMO 这会导致代码干净。
【问题讨论】:
-
average rank for non unique values是什么意思?那不是(非唯一的)价值本身吗? -
@barakmanos,请参阅问题中给出的示例。值 5 是非唯一的,如果我们忽略它,排名是 (1, 3, 2, 4, 5)。然后是三个 5,每个都有不同的等级。在统计学中,通常将这些等级的平均值 avg(3, 4, 5, ) = 4 分配给所有值 5。