什么是Python的Numpy np.random.choice的Java等价物? [重复]

时间:2021-04-06 12:32:30

This question already has an answer here:

这个问题在这里已有答案:

I was looking for Java's equivalent code or underlying theory for pythons np.random.choice (Numpy as np). I am trying to implement Q-learning in Java. I have a similar implementation that uses Python's np.random.choice method to select the random moves from the probability distribution.

我正在为pythons np.random.choice(Numpy as np)寻找Java的等效代码或基础理论。我正在尝试用Java实现Q-learning。我有一个类似的实现,它使用Python的np.random.choice方法从概率分布中选择随机移动。

Python's code

Input list: ['pooh', 'rabbit', 'piglet', 'Christopher'] and probabilies: [0.5, 0.1, 0.1, 0.3]

输入列表:['pooh','rabbit','piglet','Christopher']和概率:[0.5,0.1,0.1,0.3]

I want to select one of the value from the input list given the associated probability of each input element.

我想在给定每个输入元素的相关概率的情况下从输入列表中选择一个值。

1 个解决方案

#1


0  

Good idea to use Apache Commons math library, EnumeratedInteger distribution, http://commons.apache.org/proper/commons-math/javadocs/api-3.6/org/apache/commons/math3/distribution/EnumeratedIntegerDistribution.html

好主意使用Apache Commons数学库,EnumeratedInteger发行版,http://commons.apache.org/proper/commons-math/javadocs/api-3.6/org/apache/commons/math3/distribution/EnumeratedIntegerDistribution.html

Along the lines (in some pseudocode)

沿线(在某些伪代码中)

values = new String[4]{'pooh', 'rabbit', 'piglet', 'Christopher'};
dist   = new EnumeratedIntegerDistribution(new int[4]{0,1,2,3}, new double[4]{0.5, 0.1, 0.1, 0.3});

int idx = dist.sample();
return values[idx];

#1


0  

Good idea to use Apache Commons math library, EnumeratedInteger distribution, http://commons.apache.org/proper/commons-math/javadocs/api-3.6/org/apache/commons/math3/distribution/EnumeratedIntegerDistribution.html

好主意使用Apache Commons数学库,EnumeratedInteger发行版,http://commons.apache.org/proper/commons-math/javadocs/api-3.6/org/apache/commons/math3/distribution/EnumeratedIntegerDistribution.html

Along the lines (in some pseudocode)

沿线(在某些伪代码中)

values = new String[4]{'pooh', 'rabbit', 'piglet', 'Christopher'};
dist   = new EnumeratedIntegerDistribution(new int[4]{0,1,2,3}, new double[4]{0.5, 0.1, 0.1, 0.3});

int idx = dist.sample();
return values[idx];