神秘的scipy"无法转换整数标量"错误

Rok*_*Rok 5 python numpy scipy sparse-matrix

我正在使用scipy.sparse.csr_matrix如下构造稀疏向量:

csr_matrix((values, (np.zeros(len(indices)), indices)), shape = (1, max_index))
Run Code Online (Sandbox Code Playgroud)

这适用于我的大部分数据,但偶尔我会得到一个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
Run Code Online (Sandbox Code Playgroud)

inds是一个np.int64数组,vals是一个np.float64数组.

scipy sum_duplicates代码的相关部分在这里.

请注意,这有效:

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>
Run Code Online (Sandbox Code Playgroud)

所以问题不在于其中一个维度 > 2^31

有什么想法为什么这些值应该导致问题?

mat*_*asg 1

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

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