Hướng dẫn dùng sparse. python

Một ví dụ rất hữu ích và thích hợp là trong sự trợ giúp!

import scipy.sparse as sp
help(sp)

Điều này mang lại:

Example 2
---------

Construct a matrix in COO format:

>>> from scipy import sparse
>>> from numpy import array
>>> I = array([0,3,1,0])
>>> J = array([0,3,1,2])
>>> V = array([4,5,7,9])
>>> A = sparse.coo_matrix((V,(I,J)),shape=(4,4))

Cũng cần lưu ý rằng các hàm tạo khác nhau (một lần nữa từ sự trợ giúp):

    1. csc_matrix: Compressed Sparse Column format
    2. csr_matrix: Compressed Sparse Row format
    3. bsr_matrix: Block Sparse Row format
    4. lil_matrix: List of Lists format
    5. dok_matrix: Dictionary of Keys format
    6. coo_matrix: COOrdinate format (aka IJV, triplet format)
    7. dia_matrix: DIAgonal format

To construct a matrix efficiently, use either lil_matrix (recommended) or
dok_matrix. The lil_matrix class supports basic slicing and fancy
indexing with a similar syntax to NumPy arrays.  

Ví dụ của bạn sẽ đơn giản như sau:

S = sp.csr_matrix(A)

3 hữu ích 2 bình luận chia sẻ