Skip to content

dtype keyword for np.random.randint and np.random.rand #6790

Closed

Description

@sebix

As brought up at the mailinglist, this code ates up much memory:

spectrumArray = np.random.randint(0,255, (2**20,2**12)).astype(np.uint8)

It has also crossed my mind that np.random.randint and np.random.rand could use an extra 'dtype' keyword. It didn't look easy to implement though.

See this thread on the mailinglist: [Numpy-discussion] array of random numbers fails to construct

Activity

  1. njsmith commented on Dec 8, 2015

    @njsmith
    Member

    This came up on the mailing list as a feature request from someone who
    wanted to generate many gigabytes of random uint8s, so couldn't afford the
    memory overhead of generating uint64s and then calling astype.
    On Dec 8, 2015 01:26, "Sebastian" [email protected] wrote:

    —
    Reply to this email directly or view it on GitHub
    #6790.

  2. gfyoung commented on Dec 20, 2015

    @gfyoung
    Contributor

    How is the dtype parameter processed into C data types (e.g. long or long long) when creating an array or matrix object? I have been knee-deep in randint code, and as a first thought, is there anything wrong with initializing rv and array depending on the requested dtype?

  3. jaimefrio commented on Dec 20, 2015

    @jaimefrio
    Member

    Take a look here. When numpy C code wants to cover all the possible integer types, it typically has versions for:

    • npy_byte
    • npy_short
    • npy_int
    • npy_long
    • npy_longlong

    plus their unsigned counterparts.

    The simplest solution would be to leave the generation of the random numbers alone, i.e. make no changes to rk_interval, and simply cast the result to the requested type, one item at a time, rather than the whole array at once. This will not be optimal in some senses, but it will keep a lot of behavior consistent with the current one, which may not be a strict requirement, but is certainly not a bad thing.

    I'm guessing the Cython way of doing this would be to move the loop copying the random numbers into the output array here, into a separate function defined with fused types. If this doesn't make sense, ask and I'll try to explain...

  4. gfyoung commented on Dec 20, 2015

    @gfyoung
    Contributor

    Thanks for the link to the numpy C data types. That's going to be quite useful.

    Fused types, interesting...I've never used them before, but the DOC seems to be cautioning us against using them for the time being. Thus, it seems best to go the less Cythonic route for now. At least the functionality would be in place, and if necessary, another PR could be put up to refactor the code once fused types are more stable.

  5. jaimefrio commented on Dec 20, 2015

    @jaimefrio
    Member

    Scipy uses fused types in several places. We are different projects, but close cousins to say the least: unless someone else makes a compelling argument against their use in NumPy, I think you can assume that it is safe to use them.

    Unless you have a better alternative, I think it's either an if-elif monster with 10 branches, copying the bytes one at a time, or fused types. The first is ugly, the second will be slower, but perhaps not noticeably so, depending on how slow

  6. jaimefrio commented on Dec 20, 2015

    @jaimefrio
    Member

    <hit send to soon, continuing>the actual random number generation is.

    What do you have in mind.

  7. gfyoung commented on Dec 20, 2015

    @gfyoung
    Contributor

    Ah! Okay, that's good to know. I was initially going by the DOC, which warned against using them. However, if scipy has been able to use them without harm, then they certainly merit more consideration as a possible implementation.

  8. gfyoung commented on Dec 20, 2015

    @gfyoung
    Contributor

    Also, it occurs to me that one might also want to expand numpy.random.RandState.tomaxint to account for dtype. But I'm getting ahead of myself here. Let me see how randint and rand go first.

  9. gfyoung commented on Jan 3, 2016

    @gfyoung
    Contributor

    Just curious, does anyone know of a significant use case for specifying dtype for rand? Otherwise, I think this issue is good to close as well.

  10. charris commented on Jan 3, 2016

    @charris
    Member

    Someone might want floats instead of doubles, that would about double the speed.

  11. charris commented on Jan 3, 2016

    @charris
    Member

    That's probably an easier project than randint. Might raise the topic on the list for discussion.

  12. gfyoung commented on Jan 4, 2016

    @gfyoung
    Contributor

    Sent out email here. I'll try putting together a PR for this once I start getting feedback.

  13. 13 remaining items

  14. charris commented on Jan 4, 2017

    @charris
    Member

    Look at how it is done for integers. C++ is a no go.

  15. NumesSanguis commented on Feb 8, 2019

    @NumesSanguis

    Is someone still working on this feature? Since I'm dealing with audio and deep learning, I wanted to add white noise to a signal. For DL, float64 is overkill, so it would be nice if we could generate a uniform distribution as float32 from the start with:

    noise = np.random.uniform(low=-1.0, high=1.0, size=signal_len, dtype=np.float32)
    
  16. bashtage commented on Apr 11, 2019

    @bashtage
    Contributor

    xref #13163 . This is fixed in the randomgen branch.

  17. mattip commented on May 29, 2019

    @mattip
    Member

    Closing, the new API supports dtype. Please reopen if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions