【问题标题】:How to work around the ValueError: array is too big error?如何解决 ValueError: array is too big 错误?
【发布时间】:2014-01-17 14:23:08
【问题描述】:

我有一个 scipy 稀疏矩阵(csr:压缩稀疏行矩阵)。我想使用 Orange 的特征选择方法(Orange.feature.scoring.score_all (InfoGain/MDL))。但是,据我了解,我必须创建一个只接受 numpy 数组作为参数的表。因此,每当我尝试使用 (.toarray()) 将 csr 矩阵转换为数组时,都会出现以下错误(因为矩阵的大小):

Traceback (most recent call last):
  File "C:\Users\NMS\Desktop\PyExp\experiments_acl2013.py", line 249, in <module>
    print(X_train.toarray())
  File "C:\Python27\lib\site-packages\scipy\sparse\compressed.py", line 561, in toarray
    return self.tocoo(copy=False).toarray(order=order, out=out)
  File "C:\Python27\lib\site-packages\scipy\sparse\coo.py", line 238, in toarray
    B = self._process_toarray_args(order, out)
  File "C:\Python27\lib\site-packages\scipy\sparse\base.py", line 635, in _process_toarray_args
    return np.zeros(self.shape, dtype=self.dtype, order=order)
ValueError: array is too big.

是否有另一种方法可以让我通过稀疏矩阵来创建表格? 或者 有没有办法在 Orange 中应用 InfoGain 或 MDL,而无需直接使用我的稀疏矩阵创建表?

将 memmap 传递给 Table 时出现以下错误:

>>> t2 = Table(d2, mm)

Traceback (most recent call last):
   File "<pyshell#125>", line 1, in <module>
    t2 = Table(d2, mm)
   TypeError: invalid arguments

在没有域的情况下传递 memmap 时,我得到以下信息:

>>> mm
memmap([[0, 1, 2, 4],
       [9, 8, 6, 3]])
>>> t2 = Table(mm)

Traceback (most recent call last):
  File "<pyshell#128>", line 1, in <module>
    t2 = Table(mm)
TypeError: invalid arguments for constructor (domain or examples or both expected)

【问题讨论】:

    标签: python arrays numpy scipy orange


    【解决方案1】:

    这里有一个解决方法。对于给定的coo_matrix,称为m(使用m.tocoo() 获得):

    1) 创建一个numpy.memmap 数组用于写入:

    mm = np.memmap('test.memmap', mode='w+', dtype=m.dtype, shape=m.shape)
    

    2) 将数据复制到 memmap 数组,这应该可以工作:

    for i,j,v in zip(m.row, m.col, m.data):
        mm[i,j] = v
    

    3)您可以访问the documentation...中详细介绍的memmap

    【讨论】:

    • 我需要一个 numpy 数组传递给 Table(Domain,Numpy_array) 来创建一个表。
    • 我猜你可以传memmap数组,你试试看吗?
    • 我已将我的 memmap 通过试验添加到上述问题中
    • @user2179347 from the docs 你的命令应该是Table(mm),不是吗?
    • 我已将没有域的 memmap 通过试验添加到上述问题中
    猜你喜欢
    • 2022-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-09
    • 1970-01-01
    • 2021-08-18
    • 2015-07-24
    • 2023-02-03
    相关资源
    最近更新 更多