How to change integer type when creating meshgrid with Numpy?

这一生的挚爱 提交于 2021-01-28 10:10:37

问题


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 ranges 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!