Python 2.7 使用Scipy'的Python类型错误;s coo_matrix.todense()

Python 2.7 使用Scipy'的Python类型错误;s coo_matrix.todense(),python-2.7,scipy,typeerror,Python 2.7,Scipy,Typeerror,最近,我在使用scipy将coo_矩阵转换为密集矩阵时遇到了困难。我有一个数据类型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.asm

最近,我在使用
scipy
coo_矩阵
转换为密集矩阵时遇到了困难。我有一个数据类型
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\u列
的格式似乎很好,并且肯定不是
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
>X_train.dtype
浮动16
>>X_火车形状
(6206, 4712)
>>类型(X_列车)
>>str(X_列车)
(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
你有什么想法吗?另外,如果需要其他详细信息,请告知我


我在Windows7上使用的是Python2.7.2,以及NUMPY1.7和SCIPY0.11。谢谢。

此错误也发生在最新的scipy主分支中。例如

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

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


我在scipy github网站上为此创建了一个问题:

感谢您的回答和报告错误。干杯
float16
(和
bool
)是当前不受支持的数据类型。