英文:
how to show sparse matrix with increasing data
问题
如何使稀疏矩阵以值部分递增的方式显示?我认为默认情况下,列部分是递增的。
英文:
how can we have the sparse matrix to be displayed in a way that the value part of a sparse matrix is increasing? I think by default, column part is increasing.
答案1
得分: 1
一个直观的方法是使用 sorted(zip()) 方法通过值来“手动”对列/行索引进行排序:
from scipy.sparse import csr_matrix
m = csr_matrix([[0, 0, 7], [2, 0, 0], [0, 5, 0]])
print("As returned by csr:")
print(m)
print("Sorted by value:")
for d, r, c in sorted(zip(m.data, m.tocoo().row, m.tocoo().col)):
print(f" ({r}, {c})\t{d}")
这将得到以下结果:
As returned by csr:
(0, 2) 7
(1, 0) 2
(2, 1) 5
Sorted by value:
(1, 0) 2
(2, 1) 5
(0, 2) 7
英文:
A straight-forward approach would be to "manually" sort the col/row indices by the value, using a sorted(zip()) approach:
from scipy.sparse import csr_matrix
m = csr_matrix([[0, 0, 7], [2, 0, 0], [0, 5, 0]])
print("As returned by csr:")
print(m)
print("Sorted by value:")
for d, r, c in sorted(zip(m.data, m.tocoo().row, m.tocoo().col)):
print(f" ({r}, {c})\t{d}")
which gives the following result:
As returned by csr:
(0, 2) 7
(1, 0) 2
(2, 1) 5
Sorted by value:
(1, 0) 2
(2, 1) 5
(0, 2) 7
答案2
得分: 1
这是关于对data
进行排序并以相同顺序显示坐标的基本方法的变体:
首先,创建一个带有整数值的示例随机矩阵(便于显示)。我使用coo
格式,因为它显示了数据和坐标之间的最简单关系。
M = (sparse.random(10, 10, 0.1, 'coo') * 10).astype(int)
在ipython
中,基本显示是repr
版本,而print
显示str
版本。
现在获取数据的排序顺序:
idx = np.argsort(M.data)
然后创建一个新的矩阵,其中行和列以相同的顺序排列:
M1 = sparse.coo_matrix((M.data[idx], (M.row[idx], M.col[idx])), shape=M.shape)
这是相同的矩阵,但data
数组已排序。
M1.A
将coo
格式转换为csr
格式需要排序(按行和列的词法顺序),因此两个coo
矩阵以相同的方式转换:
M.tocsr().data
csr
格式的规范顺序按行和列排序:
print(M1.tocsr())
英文:
Here's a variation on the basic approach of sorting the data
, and displaying the coordinates in the same order
First make a sample random matrix with integer values (for ease of display). I'm using coo
format because that's the one that shows the simplest relation between data and coordinates.
In [9]: M = (sparse.random(10,10,.1, 'coo')*10).astype(int)
In [10]: M
Out[10]:
<10x10 sparse matrix of type '<class 'numpy.int32'>'
with 10 stored elements in COOrdinate format>
In [11]: print(M)
(1, 3) 1
(3, 4) 7
(5, 4) 1
(7, 4) 8
(5, 6) 9
(6, 6) 7
(5, 8) 4
(6, 8) 1
(4, 9) 5
(9, 9) 1
In ipython
, the basic display is the repr
version, while print
shows the str
version.
Now get the sort order of the data:
In [12]: idx = np.argsort(M.data)
and make a new matrix with row and column in the same order:
In [13]: M1 = sparse.coo_matrix((M.data[idx],(M.row[idx], M.col[idx])),shape=M.shape)
In [14]: M1
Out[14]:
<10x10 sparse matrix of type '<class 'numpy.int32'>'
with 10 stored elements in COOrdinate format>
In [15]: print(M1)
(1, 3) 1
(5, 4) 1
(6, 8) 1
(9, 9) 1
(5, 8) 4
(4, 9) 5
(3, 4) 7
(6, 6) 7
(7, 4) 8
(5, 6) 9
It's the same matrix, but the data
array is sorted
In [16]: M.A
Out[16]:
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 7, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 5],
[0, 0, 0, 0, 1, 0, 9, 0, 4, 0],
[0, 0, 0, 0, 0, 0, 7, 0, 1, 0],
[0, 0, 0, 0, 8, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1]])
In [17]: M1.A
Out[17]:
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 7, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 5],
[0, 0, 0, 0, 1, 0, 9, 0, 4, 0],
[0, 0, 0, 0, 0, 0, 7, 0, 1, 0],
[0, 0, 0, 0, 8, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1]])
In [18]: M1.data
Out[18]: array([1, 1, 1, 1, 4, 5, 7, 7, 8, 9])
Converting the coo to csr format requires sorting (lexical for row and col), so both coo matrices are converted in the same way:
In [19]: M.tocsr().data
Out[19]: array([1, 7, 5, 1, 9, 4, 7, 1, 8, 1], dtype=int32)
In [20]: M1.tocsr().data
Out[20]: array([1, 7, 5, 1, 9, 4, 7, 1, 8, 1], dtype=int32)
The cannonical csr order by rows and columns within those:
In [23]: print(M1.tocsr())
(1, 3) 1
(3, 4) 7
(4, 9) 5
(5, 4) 1
(5, 6) 9
(5, 8) 4
(6, 6) 7
(6, 8) 1
(7, 4) 8
(9, 9) 1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论