python - TypeError:“numpy.float64”类型的对象在分配矩阵元素时没有 len()
问题描述
我在使用 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。
谢谢。
解决方案
只需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)
引用另一个包https://anndata.readthedocs.io/en/stable/,它有一个h5py/h5sparse
模块。在某种程度上,这改变了标准sparse
矩阵的行为。
标准_set_many
(in scipy.sparse.compressed.py
) 首先变成x
一个数组np.array(x...)
,然后使用nsample = x.size
。
总共:
- 你没有告诉我们
anndata
包裹的事 - “错误”似乎在那个包中,或者你在使用它。
推荐阅读
- php - 如何修改 $session 数组变量中的值对象
- django - 安装 boto 时没有名为“storages.backends.s3boto”的模块
- c++ - 如果一起指定多个优化标志,gcc(和 clang)会做什么?
- javascript - Webpack:收到此错误:构建失败(来自 ./node_modules/css-loader/dist/cjs.js):
- java - heroku 错误:无法找到或加载主类
- c# - 有没有办法将 NAudio.Lame 与 Xamarin (Android/iOS) 应用程序一起使用?
- python - 创建带有图像的 .csv
- c++ - 如何在 Arduino 上使用 char 数组实现函数
- django - 序列化后如何将模型字段添加到对象
- r - 在 RStudio 中调用系统命令时出现问题