Python numpy中大型阵列的点积

Python numpy中大型阵列的点积,python,numpy,Python,Numpy,我有一个巨大的数组,我想用一个小数组计算点积。但我得到的是“数组太大了”有解决办法吗 import numpy as np eMatrix = np.random.random_integers(low=0,high=100,size=(20000000,50)) pMatrix = np.random.random_integers(low=0,high=10,size=(50,50)) a = np.dot(eMatrix,pMatrix) Error: /Library/Python

我有一个巨大的数组,我想用一个小数组计算点积。但我得到的是“数组太大了”有解决办法吗

import numpy as np

eMatrix = np.random.random_integers(low=0,high=100,size=(20000000,50))
pMatrix = np.random.random_integers(low=0,high=10,size=(50,50))

a = np.dot(eMatrix,pMatrix)

Error:
/Library/Python/2.7/site-packages/numpy/random/mtrand.so in mtrand.RandomState.random_integers (numpy/random/mtrand/mtrand.c:9385)()

/Library/Python/2.7/site-packages/numpy/random/mtrand.so in mtrand.RandomState.randint (numpy/random/mtrand/mtrand.c:7051)()

ValueError: array is too big.
我认为唯一“简单”的答案是增加内存

它花费了15GB,但我可以在我的macbook上实现

In [1]: import numpy
In [2]: e = numpy.random.random_integers(low=0, high=100, size=(20000000, 50))
In [3]: p = numpy.random.random_integers(low=0, high=10, size=(50, 50))
In [4]: a = numpy.dot(e, p)
In [5]: a[0]
Out[5]:
array([14753, 12720, 15324, 13588, 16667, 16055, 14144, 15239, 15166,
       14293, 16786, 12358, 14880, 13846, 11950, 13836, 13393, 14679,
       15292, 15472, 15734, 12095, 14264, 12242, 12684, 11596, 15987,
       15275, 13572, 14534, 16472, 14818, 13374, 14115, 13171, 11927,
       14226, 13312, 16070, 13524, 16591, 16533, 15466, 15440, 15595,
       13164, 14278, 13692, 12415, 13314])
一个可能的解决方案可能是使用A和稀疏矩阵点运算符

例如,在我的机器上,仅将
e
构造为密集矩阵时使用了8GB的ram。构造类似的稀疏矩阵
eprome

In [1]: from scipy.sparse import rand
In [2]: eprime = rand(20000000, 50)

在内存方面有可忽略的成本。

我相信答案是您没有足够的RAM,而且可能您正在运行32位版本的python。也许可以澄清您正在运行的操作系统。许多操作系统将同时运行32位和64位程序。

如果数组溢出了本机int类型,则在计算数组的总大小时会出现该错误,以获得确切的源代码行

为了实现这一点,不管您的机器是64位的,您几乎可以肯定运行的是32位版本的Python(和NumPy):

除非我将大小增加到魔法
2**31-1
阈值之外

>>> np.random.random_integers(low=0,high=100,size=(2**30, 2))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "mtrand.pyx", line 1420, in mtrand.RandomState.random_integers (numpy\random\mtrand\mtrand.c:12943)
  File "mtrand.pyx", line 938, in mtrand.RandomState.randint (numpy\random\mtrand\mtrand.c:10338)
ValueError: array is too big.

这在eMatrix=上已经发生了,不是吗?您要求的是10^9个整数—每个整数字节数的1 GB倍。因此,至少你应该将它们放入一个数据类型为int8的数组中,而不是默认的int64。但我有一台64位机器,第一次使用16GB的RAMSo 8GB,对于a,至少同样如此,也许还有一些看不见的中间值。我相信,一旦你进行了类似点的计算,你又会有一个密集矩阵。嘿@stderr,正如我前面提到的,我也在有16GB ram的mac上尝试过,但失败了。而且我不想要稀疏矩阵,我的矩阵需要加密如何检查我是否正在运行32位版本的python?如上所述,请参阅此处关于如何确定您是在运行64位还是32位的python可执行文件:感谢您的深入了解!我使用的是numpy版本1.8.2
>>> np.random.random_integers(low=0,high=100,size=(20000000,50))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "mtrand.pyx", line 1420, in mtrand.RandomState.random_integers (numpy\random\mtrand\mtrand.c:12943)
  File "mtrand.pyx", line 938, in mtrand.RandomState.randint (numpy\random\mtrand\mtrand.c:10338)
MemoryError
>>> np.random.random_integers(low=0,high=100,size=(2**30, 2))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "mtrand.pyx", line 1420, in mtrand.RandomState.random_integers (numpy\random\mtrand\mtrand.c:12943)
  File "mtrand.pyx", line 938, in mtrand.RandomState.randint (numpy\random\mtrand\mtrand.c:10338)
ValueError: array is too big.
>>> np.__version__
'1.10.0.dev-9c50f98'