繁体   English   中英

什么是CSR格式的scipy.sparse矩阵?

[英]What is a scipy.sparse matrix in the CSR format?

我是scikit和scipy的新手,我尝试了以下操作:

# -- coding: utf-8 --
from sklearn.feature_extraction import FeatureHasher
data = [[('this', 'is'), ('is', 'a'), ('a', 'text')],
        [('and', 'one'), ('one', 'more')],]

fh = FeatureHasher(input_type='string')
X = fh.transform(((' '.join(x) for x in sample) for sample in data))
print X

问题是我不理解输出:

  (0, 18882)    1.0
  (0, 908056)   1.0
  (0, 1003453)  1.0
  (1, 433727)   1.0
  (1, 575892)   -1.0

谁能解释我这个输出是什么意思? 我阅读了FeatureHasher()方法的文档 ,但并未对此有所了解。

这是一个大型稀疏矩阵的显示,如scipy.sparse所实现。

  (0, 18882)    1.0
  (0, 908056)   1.0
  (0, 1003453)  1.0
  (1, 433727)   1.0
  (1, 575892)   -1.0

X.shape将给出其尺寸。 X.todense()生成一个规则的numpy矩阵,其中包含很多零值。

这是一个更小的稀疏矩阵的示例:

In [182]: from scipy import sparse
In [183]: X=sparse.csr_matrix([[0,1,2],[1,0,0]])
In [184]: X
Out[184]: 
<2x3 sparse matrix of type '<type 'numpy.int32'>'
    with 3 stored elements in Compressed Sparse Row format>
In [185]: print X
  (0, 1)    1
  (0, 2)    2
  (1, 0)    1
In [186]: X.todense()
Out[186]: 
matrix([[0, 1, 2],
        [1, 0, 0]])
In [187]: X.toarray()
Out[187]: 
array([[0, 1, 2],
       [1, 0, 0]])

print X(row, col) value格式显示此矩阵的非零值。

您的X至少是一个(2,1003454)矩阵,但多数为零。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM