在解决问题之前,请确保您正在使用 Linux 平台上的 64 位构建和 64 位架构。在那里,本机的“long”数据类型大小为 64 位(相对于 Windows,我认为是这样的)。
参考以下表格:
编辑:
也许我之前没有表述清楚——在64位Windows上,经典的本地“long”数据类型大小为32位(请参见this问题)。这可能是你的情况存在问题的原因。也就是说,当你改变平台到Linux时,你的代码可能会正常工作。我不能绝对确定这一点,因为它确实取决于numpy/scipy C源码中使用了哪些本地数据类型(当然,在Windows上有64位数据类型可用,并且通常会通过编译器指令执行平台案例分析,并通过宏选择适当的类型——我无法想象他们会意外使用32位数据类型)。
编辑2:
我可以提供三个支持我的假设的数据样本。
Debian 64位、Python 2.7.3和SciPy 0.10.1二进制文件来自Debian仓库:
Python 2.7.3 (default, Mar 13 2014, 11:03:55)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import scipy; print scipy.__version__; import scipy.sparse as s; s.rand(100000, 100000, 0.0001).shape
0.10.1
(100000, 100000)
Windows 7 64位,32位Python构建版本,32位SciPy 0.10.1构建版本,均来自ActivePython:
ActivePython 2.7.5.6 (ActiveState Software Inc.) based on
Python 2.7.5 (default, Sep 16 2013, 23:16:52) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import scipy; print scipy.__version__; import scipy.sparse as s; s.rand(100000, 100000, 0.0001).shape
0.10.1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\user\AppData\Roaming\Python\Python27\site-packages\scipy\sparse\construct.py", line 426, in rand
raise ValueError(msg % np.iinfo(tp).max)
ValueError: Trying to generate a random sparse matrix such as the product of dimensions is
greater than 2147483647 - this is not supported on this machine
Windows 7 64位操作系统,64位ActivePython版本,64位SciPy 0.15.1版本(来自Gohlke,基于MKL构建):
ActivePython 3.4.1.0 (ActiveState Software Inc.) based on
Python 3.4.1 (default, Aug 7 2014, 13:09:27) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import scipy; scipy.__version__; import scipy.sparse as s; s.rand(100000, 100000, 0.0001).shape
'0.15.1'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python34\lib\site-packages\scipy\sparse\construct.py", line 723, in rand
j = random_state.randint(mn)
File "mtrand.pyx", line 935, in mtrand.RandomState.randint (numpy\random\mtrand\mtrand.c:10327)
OverflowError: Python int too large to convert to C long
N*M
之间选择一个32位整数来选择随机条目来给出您的矩阵,而最大的32位(有符号)整数是2^31-1
(100,000*100,000 = 10,000,000,000 > 2,147,483,647 = 2^31-1
)。使用bmat
分块构建可能是最简单的解决方法。尝试使N*M = 2^31-2
,然后是2^31
,看看是否会出现问题。 - willPython int too large to convert to C long
,并且与 climits 头文件中的限制相符。 - willstdint.h
并查看您的限制是什么。 - will