1

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.

Python's code

Input list: ['pooh', 'rabbit', 'piglet', 'Christopher'] and probabilies: [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.

  • I think you would have written the method in the same time you posted the question. If you can not find it, write it. Simple as that. – Aniket Sahrawat Mar 18 '18 at 05:25
  • @rpg711 I am sure this is not a duplicate question. Please refer this link https://docs.scipy.org/doc/numpy-1.12.0/reference/generated/numpy.random.choice.html – Aawesh Man Shrestha Mar 18 '18 at 05:28
  • Same exact thing, numpy.choice does the same thing as the python random.sample(), samples a random set of items from an input array.. unless you want it with replacements in the sample. In either case, you can emulate all of this capability with a list shuffle in java. – TTT Mar 18 '18 at 05:32
  • 2
    There is no direct equivalent of that in the Java SE libraries. If you are asking for a reference to a 3rd-party implementation, that is Off-Topic. If you are asking for someone to write it for you ... that's Too Broad (IMO). If you are asking for how to do something in Java that you have implemented in python using Numpy ... tell us what it is. In the question, not the comments! – Stephen C Mar 18 '18 at 05:33
  • @rpg711 I have my own probability distribution of the input list. Given that probability distribution. I want to pick the random value from the input list. – Aawesh Man Shrestha Mar 18 '18 at 05:34
  • @StephenC I can write java code myself if anyone of you could provide the underlying theory behind the np.random.choice(). – Aawesh Man Shrestha Mar 18 '18 at 05:35
  • Well: 1) that's not a Java question, 2) you can find it out from the Numpy source code, and 3) this is not what your Question actually says! – Stephen C Mar 18 '18 at 05:37
  • @StephenC Looks like I need to edit my question. I am a newbie. – Aawesh Man Shrestha Mar 18 '18 at 05:42
  • Okay... so you can do this in a few lines of code. Map each input list q value to its probability interval within 0-1, partition float values from 0-1 such that an interval [i,j] represents the probability of selecting each item. roll a dice and select items. Doing it without replacement is a little more complicated as you'd have to increase the probability of each item being selected in the correct proportion as you remove items. – TTT Mar 18 '18 at 05:42
  • @rpg711 Do you have any link to the theory behind it? I found it little more complicated. – Aawesh Man Shrestha Mar 18 '18 at 05:57
  • I fail to see how this is not a duplicate of the post @rpg711 linked. You might want to explicitly clarify why the post linked above isn't a duplicate, and, while you're at it, explain in your post in detail what exactly np.random.choice does (at least **all** the parts of it you're interested in), so Java experts who aren't also Python experts will be able to answer your question. Although it seems like you might be looking for method which is **exactly** the same, down to the last parameter, rather than something which has the same basic functionality, which is not how languages work. – Bernhard Barker Mar 18 '18 at 06:49
  • @Dukeling I have updated my question – Aawesh Man Shrestha Mar 18 '18 at 06:54
  • It's basic probability. And if I'm not mistaken, in q learning you're picking just one move out of the q values to fit a probability model... You pretty much just select one item from your list with the given q function probability... – TTT Mar 18 '18 at 06:56
  • @rpg711 yes that's what i am exacty trying to do. – Aawesh Man Shrestha Mar 18 '18 at 07:00
  • @Dukeling that link looks very close to my problem. Well in that case, can i consider probabilities as weight for each element in the input list? – Aawesh Man Shrestha Mar 18 '18 at 07:02
  • @AaweshManShrestha Probability is a form of weight, so yes. – Bernhard Barker Mar 18 '18 at 07:03
  • @Dukeling thank you so much. I will try that and post if it worked for me. I am a newbie. Thanks for bearing with me. – Aawesh Man Shrestha Mar 18 '18 at 07:06

1 Answers1

3

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

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];
Severin Pappadeux
  • 18,636
  • 3
  • 38
  • 64