【问题标题】:Python: How does one typically get around "Maximum allowed dimension exceeded" error?Python:通常如何解决“超出最大允许尺寸”错误?
【发布时间】:2012-10-21 15:33:46
【问题描述】:

我正在尝试创建一个 2^n x 2^n numpy 数组,其中包含 非常大 组向量的所有可能的点积排列。我的测试数组“数据”是一个 (129L, 222L) numpy 数组。我的功能似乎(在我的新手看来)非常简单。只是我有太多数据需要处理。程序员通常如何解决这个问题?有什么建议吗?

我的数据

>>> data
array([[  1.36339199e-07,   6.71355407e-09,   2.13336419e-07, ...,
          8.44471296e-10,   6.02566662e-10,   3.38577178e-10],
       [  7.19224620e-08,   5.64739121e-08,   1.49689547e-07, ...,
          3.85361972e-10,   3.17756751e-10,   1.68563023e-10],
       [  1.93443482e-10,   1.11626853e-08,   2.66691759e-09, ...,
          2.20938084e-11,   2.56114420e-11,   1.31865060e-11],
       ..., 
       [  7.12584509e-13,   7.70844451e-13,   1.09718565e-12, ...,
          2.08390730e-13,   3.05264153e-13,   1.62286818e-13],
       [  2.57153616e-13,   6.08747557e-13,   2.00768488e-12, ...,
          6.29901984e-13,   1.19631816e-14,   1.05109078e-13],
       [  1.74618064e-13,   5.03695393e-13,   1.29632351e-14, ...,
          7.60145676e-13,   3.19648911e-14,   8.72102078e-15]])`

我的功能

import numpy as np
from itertools import product, count

def myFunction(data):
    S = np.array([])
    num = 2**len(data)
    y = product(data, repeat = 2)
    for x in count():
        while x <= num:
            z = y.next()
            i, j = z
            s = np.dot(i, j)
            S = np.insert(S, x, s)
            break #for the 'StopIteration' issue
        return np.reshape(S, (num,num))

我的错误

&gt;&gt;&gt; theMatrix = myFunction(data)

Traceback(最近一次通话最后一次):

文件“C:\Python27\lib\site-packages\IPython\core\interactiveshell.py”,第 2721 行,在 run_code exec code_obj in self.user_global_ns, self.user_ns

文件“”,第 1 行,&lt;module&gt; 矩阵 = myFunction(数据)

文件“E:\Folder1\Folder2\src\myFunction.py”,第 16 行,在 myFunction return np.reshape(S, (num,num))

文件“C:\Python27\lib\site-packages\numpy\core\fromnumeric.py”,第 171 行,重塑 return reshape(newshape, order=order)

ValueError: 超出最大允许尺寸

【问题讨论】:

    标签: python numpy multidimensional-array large-data


    【解决方案1】:

    为什么你要传递num,num 来重塑,而不是你正在重塑的实际东西?

    也许您想要return np.reshape(S, (num, num)) 之类的东西?


    至于实际的错误,2^129 是一个相当大的数字——即使是常规的 64 位整数也只能索引到 2^64。您机器的内存可能不能包含 2^129 x 2^129 矩阵。

    你确定你真的想要处理那么多吗?即使使用 GHz 处理器,如果您可以在单个 cpu 周期内对元素进行操作(您可能不能),这仍然是大约 2^100 秒的处理时间。

    【讨论】:

    • 哎呀...好消息。我已经纠正了...没有骰子。 (见编辑后的帖子)
    • 2^129 是一个相当大的数字——即使是常规的 64 位整数也只能索引 2^64。你确定你真的想要处理那么多吗?即使使用 GHz 处理器,如果您可以在单个 cpu 周期内对元素进行操作(您可能不能),这仍然是大约 2^100 秒的处理时间。
    【解决方案2】:

    cartesian product 是 O(n^2) 而不是 O(2^n),(你很幸运)。可能这也是您的“StopIteration”问题的原因

    S = np.array([])
    num = len(data) ** 2  # This is not the same as 2 ** len(data) !!
    y = product(data, repeat=2)
    for x in count():
        while x <= num:
            z = y.next()
            i, j = z
            s = np.dot(i, j)
            S = np.insert(S, x, s)
            break #for the 'StopIteration' issue
        return np.reshape(S, (num, num))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-18
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      • 2017-06-22
      • 2017-12-26
      • 1970-01-01
      相关资源
      最近更新 更多