是否有类似coo_matrix的东西,但矢量稀疏? [英] is there something like coo_matrix but for sparse vectors?
问题描述
我正试图从存在一些重叠索引的一系列数组中创建一个稀疏向量.对于矩阵,有一个非常
I'm trying to create a sparse vector from a series of arrays where there are some overlapping indexes. For a matrix there's a very convenient object in scipy that does exactly this:
coo_matrix((data, (i, j)), [shape=(M, N)])
因此,如果数据恰好具有重复的元素(因为它们的i,j索引相同),则将这些元素累加到最终的稀疏矩阵中.我想知道是否可以对稀疏向量做类似的事情,还是只需要使用这个对象并假装它是1列矩阵?
So if data happens to have repeated elements (because their i,j indexes are the same), those are summed up in the final sparse matrix. I was wondering if it would be possible to do something similar but for sparse vectors, or do I have just to use this object and pretend it's a 1-column matrix?
推荐答案
虽然您可以复制1d等效项,但仅使用1行(或1 col)稀疏矩阵可以节省很多工作.我不知道numpy
的任何稀疏矢量包.
While you might be able to reproduce a 1d equivalent, it would save a lot of work to just work with a 1 row (or 1 col) sparse matrix. I am not aware of any sparse vector package for numpy
.
coo
格式完全按照您给定的输入数组进行存储,不进行求和.当求和显示或(或以其他方式)转换为csc
或csr
格式时,即完成求和.而且由于csr
构造函数已编译,因此求和的速度将比您用Python编写的任何代码都要快.
The coo
format stores the input arrays exactly as you given them, without the summing. The summing is done when it is displayed or (otherwise) converted to a csc
or csr
format. And since the csr
constructor is compiled, it will to that summation faster than anything you could code in Python.
构造一个"1d"稀疏coo矩阵
Construct a '1d' sparse coo matrix
In [67]: data=[10,11,12,14,15,16]
In [68]: col=[1,2,1,5,7,5]
In [70]: M=sparse.coo_matrix((data (np.zeros(len(col)),col)),shape=(1,10))
查看其数据表示形式(不求和)
Look at its data representation (no summation)
In [71]: M.data
Out[71]: array([10, 11, 12, 14, 15, 16])
In [72]: M.row
Out[72]: array([0, 0, 0, 0, 0, 0])
In [73]: M.col
Out[73]: array([1, 2, 1, 5, 7, 5])
查看数组表示形式(形状为(1,10)
)
look at the array representation (shape (1,10)
)
In [74]: M.A
Out[74]: array([[ 0, 22, 11, 0, 0, 30, 0, 15, 0, 0]])
和等效的csr.
In [75]: M1=M.tocsr()
In [76]: M1.data
Out[76]: array([22, 11, 30, 15])
In [77]: M1.indices
Out[77]: array([1, 2, 5, 7])
In [78]: M1.indptr
Out[78]: array([0, 4])
In [79]: np.nonzero(M.A)
Out[79]: (array([0, 0, 0, 0]), array([1, 2, 5, 7]))
nonzero
显示相同的模式:
In [80]: M.nonzero()
Out[80]: (array([0, 0, 0, 0, 0, 0]), array([1, 2, 1, 5, 7, 5]))
In [81]: M.tocsr().nonzero()
Out[81]: (array([0, 0, 0, 0]), array([1, 2, 5, 7]))
In [82]: np.nonzero(M.A)
Out[82]: (array([0, 0, 0, 0]), array([1, 2, 5, 7]))
M.toarray().flatten()
将为您提供(10,)
一维数组.
M.toarray().flatten()
will give you the (10,)
1d array.
这篇关于是否有类似coo_matrix的东西,但矢量稀疏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!