问题
I got the following error
MemoryError: Unable to allocate 201. GiB for an array with shape (2999, 2999, 2999) and data type int64
when creating a meshgrid with Numpy using the following code
dimension=3
tot_length=2000
list_no=range(1, tot_length)
arr = np.meshgrid ( *[list_no for _ in range ( dimension )] )
May I know where to change the int64 to int32 or, other possible setting that can allow me to maximize the number of tot_length
which is higher than the value 2000
I have check the documentation, but it does not stated option to change the data type to type32.
回答1:
May I know where to change the int64 to int32
NumPy is guessing int64
because you are giving it a range
object where it expects an array, and range
s are sequences of int
. Use an array if you want it not to guess:
list_no = np.array(range(1, tot_length), dtype=np.int32)
or more simply
list_no = np.arange(1, tot_length, dtype=np.int32)
Of course, you'll still need 100 GiB of memory, which is still quite large.
来源:https://stackoverflow.com/questions/64448748/how-to-change-integer-type-when-creating-meshgrid-with-numpy