Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
villares opened this issue Nov 27, 2022 · 5 comments · Fixed by #259
Closed

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

villares opened this issue Nov 27, 2022 · 5 comments · Fixed by #259

Comments

@villares
Copy link
Collaborator

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
Copy link
Collaborator

hx2A commented Nov 28, 2022

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
Copy link
Collaborator

hx2A commented Nov 28, 2022

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
Copy link
Collaborator Author

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

@hx2A
Copy link
Collaborator

hx2A commented Nov 28, 2022

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
Copy link
Collaborator

hx2A commented Mar 11, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants