c++ - 如何从具有非均匀概率的列表中选择一个值?

标签 c++ probability

我正在查看 k-means++初始化算法。该算法的以下两步产生非均匀概率:

For each data point x, compute D(x), the distance between x and the nearest center that has already been chosen.

Choose one new data point at random as a new center, using a weighted probability distribution where a point x is chosen with probability proportional to D(x)^2.

如何在 C++ 中使用这种规定的加权概率分布进行选择?

最佳答案

使用 randomC++11 中实现离散分布要容易得多 header 和使用 std::discrete_distribution .这是例子:

#include <iostream>
#include <map>
#include <random>

int main()
{
    std::random_device rd;
    std::mt19937 gen(rd());
    std::discrete_distribution<> d({20,30,40,10});
    std::map<int, int> m;
    for(int n=0; n<10000; ++n) {
        ++m[d(gen)];
    }
    for(auto p : m) {
        std::cout << p.first << " generated " << p.second << " times\n";
    }
}

这是输出示例:

0 generated 2003 times
1 generated 3014 times
2 generated 4021 times
3 generated 962 times

关于c++ - 如何从具有非均匀概率的列表中选择一个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8568203/

相关文章:

c++ - 采用可以接受 std::auto_ptr<Derived> 的 std::auto_ptr<Base> 的函数

algorithm - 如何从一个联合的、离散的、概率分布函数中进行数值采样

r - 后验概率的校准

data-structures - 如何测量布隆过滤器中的误报率

c++ - 防止 Windows 在 native 代码未处理的异常上显示任何对话框

c++ - 如何使用 ncurses 构建复杂的 "graphics"?

c++ - Qt5 在多宿主网络上绑定(bind) TCP 套接字

c++ - 如何为 ZAP 实现处理程序?

algorithm - 汤姆的包比莎莉的包重的可能性有多大?

batch-file - 批处理脚本随机问题、概率问题或错误