【问题标题】:cryptic scipy "could not convert integer scalar" error神秘的scipy“无法转换整数标量”错误
【发布时间】:2015-05-23 23:46:13
【问题描述】:

我正在使用scipy.sparse.csr_matrix 构造一个稀疏向量,如下所示:

csr_matrix((values, (np.zeros(len(indices)), indices)), shape = (1, max_index))

这适用于我的大部分数据,但偶尔我会收到 ValueError: could not convert integer scalar

这重现了问题:

In [145]: inds

Out[145]:
array([ 827969148,  996833913, 1968345558,  898183169, 1811744124,
        2101454109,  133039182,  898183170,  919293479,  133039089])

In [146]: vals

Out[146]:
array([ 1.,  1.,  1.,  1.,  1.,  2.,  1.,  1.,  1.,  1.])

In [147]: max_index

Out[147]:
2337713000

In [143]: csr_matrix((vals, (np.zeros(10), inds)), shape = (1, max_index+1))
...

    996         fn = _sparsetools.csr_sum_duplicates
    997         M,N = self._swap(self.shape)
--> 998         fn(M, N, self.indptr, self.indices, self.data)
    999 
    1000         self.prune()  # nnz may have changed

ValueError: could not convert integer scalar

indsnp.int64 数组,valsnp.float64 数组。

scipysum_duplicates代码的相关部分是here

请注意,这是有效的:

In [235]: csr_matrix(([1,1], ([0,0], [1,2])), shape = (1, 2**34))
Out[235]:

<1x17179869184 sparse matrix of type '<type 'numpy.int64'>'
    with 2 stored elements in Compressed Sparse Row format>

所以问题不在于维度之一是&gt; 2^31

任何想法为什么这些值会导致问题?

【问题讨论】:

  • 请尝试使用 2**312**31-1 与您发布的相同示例。
  • 是的,我发布的示例数据适用于2**31-1,但不适用于 2**31

标签: python numpy scipy sparse-matrix


【解决方案1】:

可能是 max_index > 2**31 吗? 试试这个,只是为了确保:

csr_matrix((vals, (np.zeros(10), inds/2)), shape = (1, max_index/2))

【讨论】:

  • 是的,这也是我的第一个想法——但它适用于使用相同 max_index 的其他类似数据
  • 不,scipy.sparse.csr_matrix 可以与 max_index &gt; 2**31 配合使用——请参阅编辑后的问题。
  • @Rok 我实际上得到了一个不同的异常(使用 Python 2.7 + scipy 0.9.0)。我可以用2**31-1 构造矩阵,但不能用2**31 构造矩阵。你用的是什么 scipy 版本?
  • @matiasg: scipy 0.15.1 使用 continuum anaconda 安装
  • 我安装了 Anaconda。他们现在使用 64 位作为索引,因为我可以用2**63-1 构造一个矩阵,但不能用2**63 构造一个矩阵。那么,这与您的问题无关,但似乎有点烦人。
【解决方案2】:

您提供的最大索引小于您提供的行的最大索引。

这个 sparse.csr_matrix((vals, (np.zeros(10), inds)), shape = (1, np.max(inds)+1)) 适合我。

虽然制作 .todense() 会导致矩阵的大尺寸出现内存错误

【讨论】:

  • 好吧,不——索引数组中的最大值是2101454109,但max_index是2337713001。当维度太小时,会抛出ValueError: column index exceeds matrix dimensions错误。虽然你是对的,使用 inds.max() +1 有效。情节变厚了。
  • 哎呀,我少了一个零。顺便说一句,对我来说,任何大于 2**32-1 的东西都不起作用(你的例子失败了)。它抛出一个奇怪的异常,NotImplementedError:重载函数“coo_tocsr”的参数数量或类型错误。我正在使用 Enthought 学生发行版,scipy 版本:'0.13.3'
  • 我猜你的版本使用的是 32 位整数?
【解决方案3】:

取消注释 sum_duplicates - 函数将导致其他错误。但是这个修复:strange error when creating csr_matrix 也解决了你的问题。您可以将 version_check 扩展到较新版本的 scipy。

import scipy 
import scipy.sparse  
if scipy.__version__ in ("0.14.0", "0.14.1", "0.15.1"): 
    _get_index_dtype = scipy.sparse.sputils.get_index_dtype 
    def _my_get_index_dtype(*a, **kw): 
        kw.pop('check_contents', None) 
        return _get_index_dtype(*a, **kw) 
    scipy.sparse.compressed.get_index_dtype = _my_get_index_dtype 
    scipy.sparse.csr.get_index_dtype = _my_get_index_dtype 
    scipy.sparse.bsr.get_index_dtype = _my_get_index_dtype 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 2017-07-21
    • 1970-01-01
    • 2015-03-13
    • 2017-04-20
    • 2014-06-07
    • 1970-01-01
    相关资源
    最近更新 更多