【发布时间】:2015-06-28 22:59:46
【问题描述】:
我正在使用一些 scipy.sparse.csr_matrixes。老实说,我手头的一个来自 Scikit-learn 的 TfidfVectorizer:
vectorizer = TfidfVectorizer(min_df=0.0005)
textsMet2 = vectorizer.fit_transform(textsMet)
好的,这是一个矩阵:
textsMet2
<999x1632 sparse matrix of type '<class 'numpy.float64'>'
with 5042 stored elements in Compressed Sparse Row format>
现在我只想获取那些具有任何非零元素的行。所以很明显我会选择简单的索引:
textsMet2[(textsMet2.sum(axis=1)>0),:]
然后得到一个错误:
文件“D:\Apps\Python\lib\site-packages\scipy\sparse\sputils.py”,第 327 行,在 _boolean_index_to_array raise IndexError('无效的索引形状') IndexError: 无效的索引形状
如果我删除索引的最后一部分,我会得到一些奇怪的东西:
textsMet2[(textsMet2.sum(axis=1)>0)]
<1x492 sparse matrix of type '<class 'numpy.float64'>'
with 1 stored elements in Compressed Sparse Row format>
为什么它只显示 1 行矩阵?
再一次,我想得到这个矩阵中所有非零元素的行。有人知道怎么做吗?
【问题讨论】:
标签: python numpy matrix scipy sparse-matrix