py5coding / py5generator

Meta-programming project that creates the py5 library code.
https://py5coding.org/
GNU General Public License v3.0
52 stars 13 forks source link

`random_choice()` returns a numpy array, is this by design? #199

Closed villares closed 1 year ago

villares commented 1 year ago

I was not expecting this...

from random import choice
c = choice((1, 2, 4, 5))
print(c, type(c))

c = random_choice((1, 2, 4, 5))
print(c, type(c))

c = random_choice(('a', 'b', 'c', 'd'))
print(c, type(c))

result:

5 <class 'int'>
[4] <class 'numpy.ndarray'>
['c'] <class 'numpy.ndarray'>
hx2A commented 1 year ago

The random_choice() method was upgraded in version 0.8.1a1 to support the size parameter and a replace parameter:

https://ixora.io/blog/new-release-081a1/

I seem to remember this change was prompted by a discussion with you but I can't find it on github. Perhaps it was through Twitter DMs? In any case, if the size param is 2 or more, the result needs to be a numpy array (or at least a collection of some kind). If size param is 1, returning anything other than a numpy array would mean that the type of the returned result depends on the size param, which is a bit odd. This is also consistent with what numpy.random.choice() does (which is what py5 is using here).

But on the other hand, I can see your point, it is also odd to return a numpy array with one item in it, and that is most likely not what a user would expect or want when using this method.

Which way do you think is better? It wouldn't be difficult to check the result and if it is just one item, return the one item and not leave it in a numpy array.

hx2A commented 1 year ago

It was a Twitter DM, from back in July. You wanted to take random samples of different sizes and I upgraded the random_choice() method to accommodate that. By design, it is consistent with what numpy does. I should check to see what py5's random_choice() did before that release. Before then it would only return 1 item and I bet it did not return that 1 item in an array like it does now.

How about changing random_choice() to be how it was before and making a new method random_sample() that let you pick the sample size? The random_choice() method would only return 1 thing without an array and random_sample() would always return an array, of size 1 or more, depending on the size param.

So basically I am suggesting we rename the current random_choice() method random_sample() and then make a new random_choice() method that works the way it did before, returning just one item and that's it.

villares commented 1 year ago

Perfect! As I was reading, I was going to suggest a random_sample() method! Thank you!

hx2A commented 1 year ago

Perfect! As I was reading, I was going to suggest a random_sample() method! Thank you!

Technically you already did, back in July! But then I interfered with that idea and combined it with the existing random_choice() method to make something that was not as nice.

hx2A commented 1 year ago

This is done. I opened an issue to py5book so we remember to update summary.md to reflect the changes.