Python ValueError:包含多个元素的数组的真值不明确。使用csr_矩阵时使用a.any()或a.all()
我有4个稀疏矩阵,具有以下维度:Python ValueError:包含多个元素的数组的真值不明确。使用csr_矩阵时使用a.any()或a.all(),python,numpy,scipy,Python,Numpy,Scipy,我有4个稀疏矩阵,具有以下维度: X_train_content_sparse.shape (62313, 100000) X_train_title_sparse.shape (62313, 100000) X_train_author_sparse.shape (62313,31540) X_train_time_features_sparse.shape (62313, 7) 然后我水平地按顺序堆叠数组 X_train_sparse = hstack([X_train_conten
X_train_content_sparse.shape
(62313, 100000)
X_train_title_sparse.shape
(62313, 100000)
X_train_author_sparse.shape
(62313,31540)
X_train_time_features_sparse.shape
(62313, 7)
然后我水平地按顺序堆叠数组
X_train_sparse = hstack([X_train_content_sparse, X_train_title_sparse,
X_train_author_sparse, X_train_time_features_sparse])
之后,我将这个稀疏矩阵数组转换为稀疏矩阵。
我应用csr\u矩阵(X\u train\u sparse)
并收到这样的错误:
ValueError:包含多个元素的数组的真值为
模棱两可的使用a.any()或a.all()
>X\u列车\u稀疏
数组([,,
,
,
],dtype=object)
sparse.hstack
将所有矩阵转换为coo
格式,然后适当地联接它们的行、列、数据
数组,然后生成一个新的稀疏矩阵
sparse.hstack
带格式
参数:
In [88]: sparse.hstack([M,M],format='csr')
Out[88]:
<10x20 sparse matrix of type '<class 'numpy.float64'>'
with 40 stored elements in Compressed Sparse Row format>
[88]中的:sparse.hstack([M,M],format='csr')
出[88]:
我们需要查看完整的代码列表以及完整的回溯。您使用的是np.hstack
还是sparse.hstack
?相关?:我使用scipy.sparse.hstack
进行堆叠,而不是numpy。另外,我已经更新了问题描述。您刚才添加的内容是由np.hstack
生成的,就像我的Out[84]
一样sparse.hstack
生成稀疏矩阵,并采用格式
参数(请参见我的编辑)。你说得对。这实际上是numpy和scipy函数之间奇怪的冲突。我已经以这种方式从scipy.sparse import hstack导入了,但在默认情况下它使用了numpy的hstack
。当我改为scipy.hstack
时,效果很好
In [83]: M
Out[83]:
<10x10 sparse matrix of type '<class 'numpy.float64'>'
with 20 stored elements in Compressed Sparse Row format>
In [84]: np.hstack([M,M])
Out[84]:
array([<10x10 sparse matrix of type '<class 'numpy.float64'>'
with 20 stored elements in Compressed Sparse Row format>,
<10x10 sparse matrix of type '<class 'numpy.float64'>'
with 20 stored elements in Compressed Sparse Row format>], dtype=object)
In [85]: sparse.csr_matrix(_)
...
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all().
In [86]: sparse.hstack([M,M])
Out[86]:
<10x20 sparse matrix of type '<class 'numpy.float64'>'
with 40 stored elements in COOrdinate format>
In [88]: sparse.hstack([M,M],format='csr')
Out[88]:
<10x20 sparse matrix of type '<class 'numpy.float64'>'
with 40 stored elements in Compressed Sparse Row format>