question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature request: Set replace flag with tuple for nd array

See original GitHub issue

To be able to vectorise random sampling with/without replacement is would be useful to be able to set the replace flag in random.choice to a tuple matching the shape of size. i.e.

# THIS IS NOT A WORKING CODE
from numpy import np

x = [i for i in range(100)]
k = np.random.poission(0.5, 10)
samples = np.random.choice(x, size=(10, 10), replace=(False, True), p=k)

This would allow for a much neater way to produce a 2D array where each row is a sample from the same population taken without replacement.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:11 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
eric-wiesercommented, Apr 7, 2020

I’m not a huge fan of the proposed spelling, but agreed that the feature itself makes sense.

1reaction
bashtagecommented, May 13, 2020

If it really needed to go into choice I would suggest something like:

choice(a, size=None, replace=True, p=None, axis=0, repeat=None)

Parameters
----------
...
repeat : {int, tuple[int], None}
    Independently repeat the random sampling. If size is not None, the output dimension is
    repeat + size where integer repeat and size values have been transformed into tuples
    (e.g., (size,).) [Further clarification needed if a is not 1d.] 

With this API, your call would be

rng.choice(x, size=10, p=k, repeat=10)

and each row would be an independent sample.

You could also do things like

rng.choice(x, size=(2,2), p=k, repeat=(100,100))

which would produce an output that is (100, 100, 2, 2) which is 10,000 independent samples of 2 by 2 choice.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The tuple built-in is not supported in nopython mode #2771
The tuple([iterable]) python built-in function doesn't work in nopython mode. Is there any plan to support this in nopython mode?
Read more >
Documentation - TypeScript 4.0
Variadic Tuple Types. Consider a function in JavaScript called concat that takes two array or tuple types and concatenates them together to make...
Read more >
numpy.ndarray.flags — NumPy v1.24 Manual
Only the WRITEBACKIFCOPY, WRITEABLE, and ALIGNED flags can be changed by the user, via direct assignment to ... The array flags cannot be...
Read more >
Typed Memoryviews — Cython 3.0.0a11 documentation
Memoryviews are similar to the current NumPy array buffer support ( np.ndarray[np.float64_t, ndim=2] ), but they have more features and cleaner syntax.
Read more >
Difference between numpy.array shape (R, 1) and (R,)
A tuple is not determined by the parentheses, they are not part of it, but by the comma. x=4, assigns a tuple, x=(4)...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found