我需要你的帮助来解决以下问题:
c++/opencv 中是否有一个函数等效于以下代码:
np.random.choice(len(vec), samples, p=probabilities[:,0], replace=True)
提前谢谢。
好吧,让我们看看: numpy.random.choice(a, size=None, replace=True, p=None)
(请参阅我的评论,我猜您混淆了函数的一些参数。
对于输入a
,您使用的是样本数组。作为您想要len(vec)
的输出大小,您希望使用替换采样并具有自定义的非均匀分布。
首先使用随机分布生成索引数组,然后使用索引数组生成选定元素数组可能就足够了。
C++ 有助于生成非均匀分布数,std::discrete_distribution
例:
#include <random>
#include <vector>
#include <algorithm>
#include <iostream>
int main()
{
auto const samples = { 1, 2, 3, 4, 5, 6 }; // deducts to std::initializer_list<int>
auto const probabilities = { 0.1, 0.2, 0.1, 0.5, 0.0, 1.0 }; // deducts to std::initializer_list<double>
if (samples.size() < probabilities.size()) {
std::cerr << "If there are more probabilities then samples, you will get out-of-bounds indices = UB!n";
return -1;
}
// generate non-uniform distribution (default result_type is int)
std::discrete_distribution const distribution{probabilities};
// note, for std::vector or std::array of probabilities, use
// std::discrete_distribution distribution(cbegin(probabilities), cend(probabilities));
int const outputSize = 10;
std::vector<decltype(distribution)::result_type> indices;
indices.reserve(outputSize); // reserve to prevent reallocation
// use a generator lambda to draw random indices based on distribution
std::generate_n(back_inserter(indices), outputSize,
[distribution = std::move(distribution), // could also capture by reference (&) or construct in the capture list
generator = std::default_random_engine{} //pseudo random. Fixed seed! Always same output.
]() mutable { // mutable required for generator
return distribution(generator);
});
std::cout << "Indices: ";
for(auto const index : indices) std::cout << index << " ";
std::cout << 'n';
// just a trick to get the underlying type of samples. Works for std::initializer list, std::vector and std::array
std::vector<decltype(samples)::value_type> output;
output.reserve(outputSize); // reserve to prevent reallocation
std::transform(cbegin(indices), cend(indices),
back_inserter(output),
[&samples](auto const index) {
return *std::next(cbegin(samples), index);
// note, for std::vector or std::array of samples, you can use
// return samples[index];
});
std::cout << "Output samples: ";
for(auto const sample : output) std::cout << sample << " ";
std::cout << 'n';
}
godbolt.org
编辑:链接似乎表明std::default_random_engine
执行采样和替换。
您希望从离散随机分布中采样
该页面上的示例具有相当的演示性:
// discrete_distribution
#include <iostream>
#include <random>
int main()
{
const int nrolls = 10000; // number of experiments
const int nstars = 100; // maximum number of stars to distribute
std::default_random_engine generator;
std::discrete_distribution<int> distribution {2,2,1,1,2,2,1,1,2,2};
int p[10]={};
for (int i=0; i<nrolls; ++i) {
int number = distribution(generator);
++p[number];
}
std::cout << "a discrete_distribution:" << std::endl;
for (int i=0; i<10; ++i)
std::cout << i << ": " << std::string(p[i]*nstars/nrolls,'*') << std::endl;
return 0;
}
我认为没有一个函数可以免费为您提供此功能。你可能必须自己写。
关于如何编写这样一个函数的一些提示:
- 假设您有一个存储概率
vector<float>
。首先在此向量上使用std::partial_sum
以获取元素的累积概率。 - 然后,对于每个样本,生成一个介于 0 和 1 之间的随机浮点数。让我们称之为
random_value
.遍历累积概率向量,直到找到大于random_value
的值。此时的索引是示例索引。在samples
向量中获取此索引处的值,将其存储在某个地方并重复。