Python 3.x csc_矩阵列的就地排序
我希望能够对scipy稀疏矩阵的列进行排序。scipy文档相当简洁,我看不到太多关于修改矩阵的内容。于是我找到了这个,但是给出的答案返回了一个Python 3.x csc_矩阵列的就地排序,python-3.x,scipy,sparse-matrix,Python 3.x,Scipy,Sparse Matrix,我希望能够对scipy稀疏矩阵的列进行排序。scipy文档相当简洁,我看不到太多关于修改矩阵的内容。于是我找到了这个,但是给出的答案返回了一个列表 我想写的代码是 s = rand(4, 4, density=0.25, format='csc') _,colSize = s.get_shape() for j in range(0,colSize): s.setcol(j, sorted(s.getcol(j), key=attrgetter('data'), reverse=
列表
我想写的代码是
s = rand(4, 4, density=0.25, format='csc')
_,colSize = s.get_shape()
for j in range(0,colSize):
s.setcol(j, sorted(s.getcol(j), key=attrgetter('data'), reverse=True))
除非没有setcol
,而且sorted
不会返回与getcol
相同的类型
作为一个例子,我想得到什么,如果我在输入
<class 'scipy.sparse.csc.csc_matrix'>
[[ 0. 0.33201655 0. 0. ]
[ 0. 0. 0. 0. ]
[ 0. 0.81332962 0. 0.50794041]
[ 0. 0.41478979 0. 0. ]]
(它不必是csc矩阵,我认为这对于列操作会更好)这里有一个短函数,可以按降序对列进行排序:
import numpy as np
def sort_csc_cols(m):
"""
Sort the columns of m in descending order.
m must be a csc_matrix whose nonzero values are all positive.
m is modified in-place.
"""
seq = np.arange(m.shape[0])
for k in range(m.indptr.size - 1):
start, end = m.indptr[k:k + 2]
m.data[start:end][::-1].sort()
m.indices[start:end] = seq[:end - start]
例如,s
是一个csc\u矩阵
:
In [47]: s
Out[47]:
<8x12 sparse matrix of type '<class 'numpy.int64'>'
with 19 stored elements in Compressed Sparse Column format>
In [48]: s.A
Out[48]:
array([[ 0, 2, 0, 0, 7, 0, 0, 48, 0, 0, 0, 0],
[ 0, 0, 82, 0, 0, 38, 67, 17, 9, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 47, 0],
[ 0, 0, 0, 0, 0, 0, 99, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 83, 0, 0, 0, 9],
[ 0, 0, 0, 0, 0, 0, 85, 94, 0, 55, 68, 0],
[ 0, 0, 0, 0, 0, 0, 22, 0, 0, 0, 71, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
In [49]: sort_csc_cols(s)
In [50]: s.A
Out[50]:
array([[ 0, 2, 82, 0, 7, 38, 99, 94, 9, 55, 71, 9],
[ 0, 0, 0, 0, 0, 0, 85, 83, 0, 0, 68, 0],
[ 0, 0, 0, 0, 0, 0, 67, 48, 0, 0, 47, 0],
[ 0, 0, 0, 0, 0, 0, 22, 17, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
[47]中的:s
出[47]:
在[48]:美国
出[48]:
数组([[0,2,0,0,7,0,0,0,48,0,0,0,0,0],
[ 0, 0, 82, 0, 0, 38, 67, 17, 9, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 47, 0],
[ 0, 0, 0, 0, 0, 0, 99, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 83, 0, 0, 0, 9],
[ 0, 0, 0, 0, 0, 0, 85, 94, 0, 55, 68, 0],
[ 0, 0, 0, 0, 0, 0, 22, 0, 0, 0, 71, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
在[49]中:排序
在[50]:美国
出[50]:
数组([[0,2,82,0,7,38,99,94,9,55,71,9],
[ 0, 0, 0, 0, 0, 0, 85, 83, 0, 0, 68, 0],
[ 0, 0, 0, 0, 0, 0, 67, 48, 0, 0, 47, 0],
[ 0, 0, 0, 0, 0, 0, 22, 17, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
探索了一些列重新排序的替代方法。但它不在适当的位置。在您的示例数组中,所有非零值都是正值。你的实际数组就是这样吗?进一步阅读,你试图做一些与我最近的链接答案完全不同的事情。但我会留下这个评论;它可能会提供一些关于稀疏矩阵的有用想法。@Warren这些值是事件计数,因此它们将是大于或等于零的整数。在一个小示例中效果很好。
In [47]: s
Out[47]:
<8x12 sparse matrix of type '<class 'numpy.int64'>'
with 19 stored elements in Compressed Sparse Column format>
In [48]: s.A
Out[48]:
array([[ 0, 2, 0, 0, 7, 0, 0, 48, 0, 0, 0, 0],
[ 0, 0, 82, 0, 0, 38, 67, 17, 9, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 47, 0],
[ 0, 0, 0, 0, 0, 0, 99, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 83, 0, 0, 0, 9],
[ 0, 0, 0, 0, 0, 0, 85, 94, 0, 55, 68, 0],
[ 0, 0, 0, 0, 0, 0, 22, 0, 0, 0, 71, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
In [49]: sort_csc_cols(s)
In [50]: s.A
Out[50]:
array([[ 0, 2, 82, 0, 7, 38, 99, 94, 9, 55, 71, 9],
[ 0, 0, 0, 0, 0, 0, 85, 83, 0, 0, 68, 0],
[ 0, 0, 0, 0, 0, 0, 67, 48, 0, 0, 47, 0],
[ 0, 0, 0, 0, 0, 0, 22, 17, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])