TypeError:类型为'numpy.float64'的对象在分配矩阵元素时没有len()

时间:2019-06-06 00:20:45

标签: python numpy scipy sparse-matrix

我在使用scipy稀疏矩阵的程序包时遇到问题。

当我隔离问题时,我发现仅将元素分配给csr或csc矩阵时就会出现错误。

from scipy.sparse import csr_matrix
x = csr_matrix(np.eye(10))
x[0,3] = int(4)

我得到了错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-36-2e1809373207> in <module>
----> 1 x[0,3] = int(4)

~/anaconda2/envs/macrophage/lib/python3.6/site-packages/scipy/sparse/_index.py in __setitem__(self, key, x)
     67             if x.size != 1:
     68                 raise ValueError('Trying to assign a sequence to an item')
---> 69             self._set_intXint(row, col, x.flat[0])
     70             return
     71 

~/anaconda2/envs/macrophage/lib/python3.6/site-packages/scipy/sparse/compressed.py in _set_intXint(self, row, col, x)
    795     def _set_intXint(self, row, col, x):
    796         i, j = self._swap((row, col))
--> 797         self._set_many(i, j, x)
    798 
    799     def _set_arrayXarray(self, row, col, x):

~/anaconda2/envs/macrophage/lib/python3.6/site-packages/anndata/h5py/h5sparse.py in _set_many(self, i, j, x)
    176     i, j, M, N = self._prepare_indices(i, j)
    177 
--> 178     n_samples = len(x)
    179     offsets = np.empty(n_samples, dtype=self.indices.dtype)
    180     ret = _sparsetools.csr_sample_offsets(M, N, self.indptr, self.indices,

TypeError: object of type 'numpy.float64' has no len()

似乎_set_many()函数期望多个值,而 setitem ()仅期望一个值! 我该如何纠正此错误?

作为参考,我正在使用scipy 1.3.0。

谢谢。

1 个答案:

答案 0 :(得分:0)

仅使用scipy.sparse,您的代码示例即可工作:

In [246]: x=sparse.csr_matrix(np.eye(10))                                                              
In [247]: x[0,3]=int(4)                                                                                
/usr/local/lib/python3.6/dist-packages/scipy/sparse/_index.py:69: SparseEfficiencyWarning: Changing the sparsity structure of a csr_matrix is expensive. lil_matrix is more efficient.
  self._set_intXint(row, col, x.flat[0])
In [248]: x                                                                                            
Out[248]: 
<10x10 sparse matrix of type '<class 'numpy.float64'>'
    with 11 stored elements in Compressed Sparse Row format>
In [249]: x.A                                                                                          
Out[249]: 
array([[1., 0., 0., 4., 0., 0., 0., 0., 0., 0.],
       [0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 1., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 1., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 1., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]])

但是回溯

~/anaconda2/envs/macrophage/lib/python3.6/site-packages/anndata/h5py/h5sparse.py in _set_many(self, i, j, x)

引用另一个具有h5py/h5sparse模块的软件包https://anndata.readthedocs.io/en/stable/。某种程度上改变了标准sparse矩阵的行为。

标准_set_many(在scipy.sparse.compressed.py中)首先将x变成数组np.array(x...),并使用nsample = x.size

总之:

  • 您没有告诉我们有关anndata包裹的信息
  • “错误”似乎在该软件包中,或者您在使用它。