简体   繁体   English

Python Numpy.ndarray.shape限制

[英]Python Numpy.ndarray.shape limit

I want to create a matrix with Numpy in Python with the following code: 我想用Python在Numpy中创建一个矩阵,代码如下:

import numpy

result=numpy.zeros((20,20,20,30,30,30))

numpy.save('result',result)

I get the following error: 我收到以下错误:

Traceback (most recent call last):  
File "numpy_memoryerror.py",
line 5, in <module>
    result=numpy.zeros((20,20,20,30,30,30))
MemoryError

If I use smaller dimensions like these: 如果我使用这样的较小尺寸:

result=numpy.ones((10,10,10,20,20,20))

then the code works. 然后代码工作。

Can somebody tell me the limit of the shape tuple? 有人能告诉我形状元组的限制吗?

Its not a fundamental limit of the shape tuple, its that you don't have enough memory (RAM) on your system, hence the MemoryError . 它不是形状元组的基本限制,它是你的系统上没有足够的内存(RAM),因此是MemoryError

Again 20*20*20*30*30*30 is 216 million 64-bit (8 byte) floats or a little more than 1.6 GB of RAM. 20 * 20 * 20 * 30 * 30 * 30再次是2.16亿64位(8字节)浮点数或略高于1.6 GB的RAM。 So do you have 1.6 GB of RAM free while running the script at that point? 那么在此时运行脚本时你有1.6 GB的RAM吗? (Don't forget to all the RAM used by python, the OS, other running programs, etc.). (不要忘记python,操作系统,其他正在运行的程序等使用的所有RAM)。 If you are in linux/unix you can see how much free memory by typing free -m from the command prompt. 如果您使用的是linux / unix,则可以通过在命令提示符下键入free -m来查看可用内存量。 In windows you can see free memory by going to the task manager. 在Windows中,您可以通过转到任务管理器查看可用内存。 Furthermore, some OSes limit the amount of memory that a single process (like python) can allocate; 此外,一些操作系统限制单个进程(如python)可以分配的内存量; eg, 32-bit windows only gives 2 GB of address space per process). 例如,32位窗口每个进程仅提供2 GB的地址空间。

Compare that to 20*20*20*10*10*10, which is only ~0.06 GB (or just 27 times less memory). 将其与20 * 20 * 20 * 10 * 10 * 10进行比较,仅为~0.06 GB(或仅减少27倍的内存)。

If you don't need 8-byte floats, you could do 如果你不需要8字节浮点数,你可以这样做

numpy.zeros(20,20,20,30,30,30, dtype='float32')

which would halve your memory footprint by using single-precision (32-bit) floats. 这将通过使用单精度(32位)浮点数减半您的内存占用。 By default numpy uses dtype='float64'. 默认情况下,numpy使用dtype ='float64'。

Roughly speaking a 32-bit float has 8 digits of accuracy, a 64-bit float has 16 digits of accuracy. 粗略地说,32位浮点数具有8位精度,64位浮点数具有16位精度。 Meaning that 1+1e-8 appears as just 1 for 32-bit floats, and 1+1e-16 appears as 1 for 64-bit floats, but 1+1e-15 appears as 1.000000000000001 with 64-bit floats, but not 32-bit floats. 意味着1 + 1e-8在32位浮点数时仅显示1,而1 + 1e-16在64位浮点数时显示为1,但1 + 1e-15显示为1.000000000000001与64位浮点数,但不是32比特浮动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 pylint 为 numpy.ndarray.shape 返回“unsubscriptable-object”? - Why pylint returns `unsubscriptable-object` for numpy.ndarray.shape? 我怎么能总是让 numpy.ndarray.shape 返回一个二值元组? - How can I always have numpy.ndarray.shape return a two valued tuple? Numpy ndarray形状有3个参数 - Numpy ndarray shape with 3 parameters Numpy: 如何 map f: (shape (3) ndarray) --&gt; (float) over an ndarray of shape (...,3) 以获得 ndarray of shape (...)? - Numpy: How to map f: (shape (3) ndarray) --> (float) over an ndarray of shape (…,3) to get ndarray of shape (…)? numpy ndarray意外形状广播错误 - Numpy ndarray unexpected shape broadcast error python3 numpy ndarray没有一致的尺寸。 遇到形状归因的奇怪行为 - python3 numpy ndarray does not have consistant dimentions. Encountered odd behavor with shape atribute 我应该如何注释形状几乎不变的 numpy.ndarray object (Python) - How should I annotate numpy.ndarray object whose shape is almost constant (Python) 如何在 Python 列表列表中查找重复项,哪些元素是 numpy.ndarray 的形状(9、103) - How to find duplicates in a Python list of lists which elements are numpy.ndarray of shape (9, 103) 替换numpy ndarray中的字符(Python) - Replace character in numpy ndarray (Python) Python AttributeError:“ numpy.ndarray” - Python AttributeError: 'numpy.ndarray'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM