python-2.7 - 使用 Scipy 的 coo_matrix.todense() 的 Python TypeError

标签 python-2.7 scipy typeerror

我最近在使用 scipycoo_matrix 转换为密集矩阵时遇到了困难。我有一个 dtype float16 稀疏矩阵并尝试将其转换为密集矩阵。该错误提示给出了 char 类型的数组。但是,我非常确定我正在传递 float16 类型的数组。

错误是:

    self.Xd_train = X_train.todense()
File "C:\Python27\lib\site-packages\scipy\sparse\base.py", line 501, in todense
  return np.asmatrix(self.toarray(order=order, out=out))
File "C:\Python27\lib\site-packages\scipy\sparse\coo.py", line 241, in toarray
  B.ravel('A'), fortran)
File "C:\Python27\lib\site-packages\scipy\sparse\sparsetools\coo.py", line 175, in coo_todense
  return _coo.coo_todense(*args)
TypeError: Array of type 'float' required.  Array of type 'char' given

错误出现在类构造函数中:

self.Xd_train = X_train.todense()

矩阵X_train看起来格式良好,并且绝对不是char类型:

>> X_train.dtype
float16

>> X_train.shape
(6206, 4712)

>> type(X_train)
<class 'scipy.sparse.coo.coo_matrix'>

>> str(X_train)

(0, 63)       2.0
(0, 72)       1.0
(0, 76)       2.0
(0, 100)      1.0
(0, 104)      1.0
(0, 5)        1.0
(0, 10)       2.0
(0, 134)      2.0
(0, 20)       3.0
(0, 263)      1.0
(0, 264)      1.0
(0, 265)      1.0
(0, 27)       1.0
(0, 148)      2.0
(0, 32)       1.0
(0, 275)      1.0
(0, 35)       1.0
(0, 36)       1.0
(0, 279)      1.0
(0, 39)       1.0
(0, 41)       1.0
(0, 42)       1.0
(0, 52)       1.0
(0, 59)       4.0
(1, 72)       1.0
:     :
(6205, 133)   1.0
(6205, 134)   4.0
(6205, 135)   4.0
(6205, 136)   2.0
(6205, 137)   6.0
(6205, 138)   1.0
(6205, 139)   4.0
(6205, 20)    4.0
(6205, 142)   4.0
(6205, 23)    2.0
(6205, 24)    2.0
(6205, 26)    2.0
(6205, 27)    2.0
(6205, 32)    1.0
(6205, 33)    1.0
(6205, 35)    1.0
(6205, 36)    1.0
(6205, 37)    1.0
(6205, 39)    1.0
(6205, 40)    1.0
(6205, 41)    1.0
(6205, 42)    1.0
(6205, 43)    1.0
(6205, 56)    3.0
(6205, 60)    1.0

对于可能出现的问题有什么想法吗?另外,如果需要其他详细信息/信息,请告诉我。

我在 Windows 7 上使用 Python 2.7.2,以及 Numpy 1.7 和 Scipy 0.11。谢谢。

最佳答案

这个错误也出现在最新的 scipy master 分支中。例如。

>>> coo_matrix([[0]], dtype=np.float16).todense()

引发相同的异常。数据类型 np.float16 相对较新,scipy 中(可能还有其他地方)有很多代码尚未使用它进行测试。

如果将稀疏矩阵更改为 np.float32,它应该可以工作。

我在 scipy github 站点上为此创建了一个问题:https://github.com/scipy/scipy/issues/2481

关于python-2.7 - 使用 Scipy 的 coo_matrix.todense() 的 Python TypeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16492224/

相关文章:

python - 使用 Python 和 Hadoop Streaming 查找 Top-K

python - signal.spectrogram 找到给定段的最大频率

javascript - 如何在 GatsbyJS/ReactJS 中重复执行窗口函数?

numpy - 任意轮廓的平滑样条表示,f(length) --> x,y

Python 3 处理错误 TypeError : catching classes that do not inherit from BaseException is not allowed

javascript - React Updater 功能不起作用。常规 setState 不知何故? (无法读取 null 的属性 'name')

python - 如何打印使用类实现的树数据结构?

python - 将 python 3.x 移植到 2.7 时 to_bytes 中的问题

python - 如何对全局 __builtin__ 对象进行 cythonize?

python - 使用 Spearman 相关性和 Sklearn KNN 进行模式匹配