Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I specify a numpy dtype when generating random values?

I'm creating a numpy array of random values and adding them to an existing array containing 32-bit floats. I'd like to generate the random values using the same dtype as the target array, so that I don't have to convert the dtypes manually. Currently I do this:

import numpy as np  x = np.zeros((10, 10), dtype='f') x += np.random.randn(*x.shape).astype('f') 

What I'd like to do instead of the last line is something like:

x += np.random.randn(*x.shape, dtype=x.dtype) 

but randn (and actually none of the numpy.random methods) does not accept a dtype argument.

My specific question is, is it possible to specify a dtype for random numbers when I create them, without having to call astype? (My guess is that the random number generator is 64 bits long, so it doesn't really make sense to do this, but I thought I'd ask if it's possible.)

like image 225
lmjohns3 Avatar asked Apr 29 '14 04:04

lmjohns3


People also ask

How do I generate random values in numpy?

Array of Random Integer Values An array of random integers can be generated using the randint() NumPy function. This function takes three arguments, the lower end of the range, the upper end of the range, and the number of integer values to generate or the size of the array.

Can numpy generate sample data?

The random module from numpy offers a wide range ways to generate random numbers sampled from a known distribution with a fixed set of parameters.

How to make random arrays in NumPy?

In NumPy we work with arrays, and you can use the two methods from the above examples to make random arrays. The randint () method takes a size parameter where you can specify the shape of an array.

What is dtype in NumPy?

Data type Object (dtype) in NumPy Python. 1 Type of the data (integer, float, Python object etc.) 2 Size of the data (number of bytes) 3 Byte order of the data (little-endian or big-endian) 4 If the data type is a sub-array, what is its shape and data type.

How to instantiate a random NumPy generator?

The function numpy.random.default_rng will instantiate a Generator with numpy’s default BitGenerator. No Compatibility Guarantee. Generator does not provide a version compatibility guarantee. In particular, as better algorithms evolve the bit stream may change. Parameters.

How to create a data type object in NumPy?

1. Constructing a data type (dtype) object: A data type object is an instance of the NumPy.dtype class and it can be created using NumPy.dtype. obj: Object to be converted to a data-type object.


2 Answers

Q: is it possible to specify a dtype for random numbers when I create them.

A: No it isn't. randn accepts the shape only as randn(d0, d1, ..., dn)

Simply try this:

x = np.random.randn(10, 10).astype('f') 

Or define a new function like

np.random.randn2 = lambda *args, dtype=np.float64: np.random.randn(*args).astype(dtype) x = np.random.randn2(10, 10, dtype='f') 

If you have to use your code on the post, try this code instead

x = np.zeros((10, 10), dtype='f') x[:] = np.random.randn(*x.shape) 

This assigns the results of randn to the memory allocated by np.zeros

like image 119
emeth Avatar answered Oct 02 '22 17:10

emeth


Let me begin by saying that numpy now supports dtypes for random integers. This enhancement can be tracked through Issue #6790 on numpy's github. But as of today, this facility is not available for the gaussian RNG. I needed this same facility so I wrote this patch for numpy, https://gist.github.com/se4u/e44f631b249e0be03c21c6c898059176

The patch only adds support for generating float values and it does not handle other data types, but it might still be helpful to someone.

UPDATE 27 Sep 2020

The numpy.random.Generator provides RNGs that support the dtype keyword for all random variables. E.g. numpy.random.default_rng().standard_normal(size=1, dtype='float32') gives 1 standard gaussian of type float32.

like image 31
Pushpendre Avatar answered Oct 02 '22 16:10

Pushpendre