Hướng dẫn convert matrix to sparse matrix python - chuyển đổi ma trận thành python ma trận thưa thớt

Trong Python, thư viện SCIPY có thể được sử dụng để chuyển đổi ma trận 2 chiều thành ma trận thưa thớt. Gói ma trận thưa thớt Scipy 2-D cho dữ liệu số là scipy.sparseScipy library can be used to convert the 2-D NumPy matrix into a Sparse matrix. SciPy 2-D sparse matrix package for numeric data is scipy.sparse

Gói Scipy.sparse cung cấp các lớp khác nhau để tạo các loại ma trận thưa thớt sau đây từ ma trận 2 chiều:following types of Sparse matrices from the 2-dimensional matrix:

  1. Chặn ma trận hàng thưa thớt
  2. Một ma trận thưa thớt ở định dạng tọa độ.
  3. Ma trận cột thưa thớt nén
  4. Ma trận hàng thưa thớt nén
  5. Ma trận thưa thớt với lưu trữ đường chéo
  6. Từ điển của ma trận thưa thớt dựa trên chìa khóa.
  7. Danh sách danh sách dựa trên hàng Ma trận thưa thớt
  8. Lớp này cung cấp một lớp cơ sở cho tất cả các ma trận thưa thớt.

Các định dạng CSR [Hàng thưa thớt] hoặc CSC [cột thưa thớt] hỗ trợ các hoạt động truy cập và ma trận hiệu quả. [Compressed Sparse Row] or CSC [Compressed Sparse Column] formats support efficient access and matrix operations.

Mã ví dụ để chuyển đổi ma trận numpy thành Ma trận Ma trận Cột thưa [CSC] được nén [CSR] Ma trận bằng cách sử dụng các lớp SCIPY:

import sys                 # Return the size of an object in bytes
import numpy as np         # To create 2 dimentional matrix
from scipy.sparse import csr_matrix, csc_matrix 
# csr_matrix: used to create compressed sparse row matrix from Matrix
# csc_matrix: used to create compressed sparse column matrix from Matrix

tạo ma trận 2 chiều numpy

A = np.array[[[1, 0, 0, 0, 0, 0],\
              [0, 0, 2, 0, 0, 1],\
              [0, 0, 0, 2, 0, 0]]]
print["Dense matrix representation: \n", A]
print["Memory utilised [bytes]: ", sys.getsizeof[A]]
print["Type of the object", type[A]]

In Ma trận & các chi tiết khác:

Dense matrix representation: 
 [[1 0 0 0 0 0]
 [0 0 2 0 0 1]
 [0 0 0 2 0 0]]
Memory utilised [bytes]:  184
Type of the object 

Chuyển đổi ma trận A thành biểu diễn ma trận hàng thưa thớt được nén bằng lớp CSR_MATRIX:Compressed sparse row matrix representation using csr_matrix Class:

S = csr_matrix[A]
print["Sparse 'row' matrix: \n",S]
print["Memory utilised [bytes]: ", sys.getsizeof[S]]
print["Type of the object", type[S]]

Đầu ra của các câu lệnh in:

Sparse 'row' matrix:
[0, 0] 1
[1, 2] 2
[1, 5] 1
[2, 3] 2
Memory utilised [bytes]: 56
Type of the object: 

Chuyển đổi ma trận A thành biểu diễn ma trận cột thưa thớt được nén bằng lớp CSC_MATRIX:Compressed Sparse Column matrix representation using csc_matrix Class:

S = csc_matrix[A]
print["Sparse 'column' matrix: \n",S]
print["Memory utilised [bytes]: ", sys.getsizeof[S]]
print["Type of the object", type[S]]

Đầu ra của các câu lệnh in:

Sparse 'column' matrix:
[0, 0] 1
[1, 2] 2
[2, 3] 2
[1, 5] 1
Memory utilised [bytes]: 56
Type of the object: 

Chuyển đổi ma trận A thành biểu diễn ma trận cột thưa thớt được nén bằng lớp CSC_MATRIX:

Vì có thể thấy kích thước của các ma trận nén là 56 byte và kích thước ma trận ban đầu là 184 byte.

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Example:

    Bàn luận Matrix:
    1 0 0 0
    0 2 0 0
    0 0 3 0
    0 0 0 4
    5 0 0 0

    Với một ma trận với hầu hết các yếu tố của nó là 0, chúng ta cần chuyển đổi ma trận này thành một ma trận thưa thớt trong Python. Sparse Matrix:
    0 0 1
    1 1 2
    2 2 3
    3 3 4
    4 0 5

    Đầu vào: Ma trận: 1 0 0 00 2 0 00 0 3 00 0 0 45 0 0 0 0
    Here the Matrix is represented using a 2D list and the Sparse Matrix is represented in the form Row Column Value

    Đầu ra: Ma trận thưa thớt: 0 0 11 1 22 2 33 3 44 0 5

    Approach:

    1. Giải thích: Ở đây ma trận được biểu diễn bằng danh sách 2D và ma trận thưa thớt được biểu diễn trong giá trị cột hàng mẫu
    2. Lặp lại thông qua ma trận 2D để tìm các phần tử không phải là không.
    3. Nếu một phần tử không phải là 0, hãy tạo một danh sách trống tạm thời.
    4. Nối giá trị hàng, giá trị cột và phần tử không phải là không vào danh sách tạm thời.
    5. Bây giờ hãy nối danh sách tạm thời vào danh sách ma trận thưa thớt sao cho danh sách tạm thời hoạt động như một danh sách phụ của danh sách ma trận thưa thớt.
    6. Sau khi nhận được tất cả các phần tử không phải từ ma trận, hiển thị ma trận thưa thớt.

    Cách tiếp cận trên đã được sử dụng trong hàm convertToSparseMatrix[] trong chương trình dưới đây:

    A = np.array[[[1, 0, 0, 0, 0, 0],\
                  [0, 0, 2, 0, 0, 1],\
                  [0, 0, 0, 2, 0, 0]]]
    print["Dense matrix representation: \n", A]
    print["Memory utilised [bytes]: ", sys.getsizeof[A]]
    print["Type of the object", type[A]]
    
    0
    A = np.array[[[1, 0, 0, 0, 0, 0],\
                  [0, 0, 2, 0, 0, 1],\
                  [0, 0, 0, 2, 0, 0]]]
    print["Dense matrix representation: \n", A]
    print["Memory utilised [bytes]: ", sys.getsizeof[A]]
    print["Type of the object", type[A]]
    
    1

    A = np.array[[[1, 0, 0, 0, 0, 0],\
                  [0, 0, 2, 0, 0, 1],\
                  [0, 0, 0, 2, 0, 0]]]
    print["Dense matrix representation: \n", A]
    print["Memory utilised [bytes]: ", sys.getsizeof[A]]
    print["Type of the object", type[A]]
    
    2
    A = np.array[[[1, 0, 0, 0, 0, 0],\
                  [0, 0, 2, 0, 0, 1],\
                  [0, 0, 0, 2, 0, 0]]]
    print["Dense matrix representation: \n", A]
    print["Memory utilised [bytes]: ", sys.getsizeof[A]]
    print["Type of the object", type[A]]
    
    3
    A = np.array[[[1, 0, 0, 0, 0, 0],\
                  [0, 0, 2, 0, 0, 1],\
                  [0, 0, 0, 2, 0, 0]]]
    print["Dense matrix representation: \n", A]
    print["Memory utilised [bytes]: ", sys.getsizeof[A]]
    print["Type of the object", type[A]]
    
    4
    A = np.array[[[1, 0, 0, 0, 0, 0],\
                  [0, 0, 2, 0, 0, 1],\
                  [0, 0, 0, 2, 0, 0]]]
    print["Dense matrix representation: \n", A]
    print["Memory utilised [bytes]: ", sys.getsizeof[A]]
    print["Type of the object", type[A]]
    
    5
    A = np.array[[[1, 0, 0, 0, 0, 0],\
                  [0, 0, 2, 0, 0, 1],\
                  [0, 0, 0, 2, 0, 0]]]
    print["Dense matrix representation: \n", A]
    print["Memory utilised [bytes]: ", sys.getsizeof[A]]
    print["Type of the object", type[A]]
    
    6

    A = np.array[[[1, 0, 0, 0, 0, 0],\
                  [0, 0, 2, 0, 0, 1],\
                  [0, 0, 0, 2, 0, 0]]]
    print["Dense matrix representation: \n", A]
    print["Memory utilised [bytes]: ", sys.getsizeof[A]]
    print["Type of the object", type[A]]
    
    7
    A = np.array[[[1, 0, 0, 0, 0, 0],\
                  [0, 0, 2, 0, 0, 1],\
                  [0, 0, 0, 2, 0, 0]]]
    print["Dense matrix representation: \n", A]
    print["Memory utilised [bytes]: ", sys.getsizeof[A]]
    print["Type of the object", type[A]]
    
    3
    A = np.array[[[1, 0, 0, 0, 0, 0],\
                  [0, 0, 2, 0, 0, 1],\
                  [0, 0, 0, 2, 0, 0]]]
    print["Dense matrix representation: \n", A]
    print["Memory utilised [bytes]: ", sys.getsizeof[A]]
    print["Type of the object", type[A]]
    
    9
    A = np.array[[[1, 0, 0, 0, 0, 0],\
                  [0, 0, 2, 0, 0, 1],\
                  [0, 0, 0, 2, 0, 0]]]
    print["Dense matrix representation: \n", A]
    print["Memory utilised [bytes]: ", sys.getsizeof[A]]
    print["Type of the object", type[A]]
    
    5
    Dense matrix representation: 
     [[1 0 0 0 0 0]
     [0 0 2 0 0 1]
     [0 0 0 2 0 0]]
    Memory utilised [bytes]:  184
    Type of the object 
    
    1

    Dense matrix representation: 
     [[1 0 0 0 0 0]
     [0 0 2 0 0 1]
     [0 0 0 2 0 0]]
    Memory utilised [bytes]:  184
    Type of the object 
    
    2
    Dense matrix representation: 
     [[1 0 0 0 0 0]
     [0 0 2 0 0 1]
     [0 0 0 2 0 0]]
    Memory utilised [bytes]:  184
    Type of the object 
    
    3
    Dense matrix representation: 
     [[1 0 0 0 0 0]
     [0 0 2 0 0 1]
     [0 0 0 2 0 0]]
    Memory utilised [bytes]:  184
    Type of the object 
    
    4
    Dense matrix representation: 
     [[1 0 0 0 0 0]
     [0 0 2 0 0 1]
     [0 0 0 2 0 0]]
    Memory utilised [bytes]:  184
    Type of the object 
    
    5
    Dense matrix representation: 
     [[1 0 0 0 0 0]
     [0 0 2 0 0 1]
     [0 0 0 2 0 0]]
    Memory utilised [bytes]:  184
    Type of the object 
    
    6
    Dense matrix representation: 
     [[1 0 0 0 0 0]
     [0 0 2 0 0 1]
     [0 0 0 2 0 0]]
    Memory utilised [bytes]:  184
    Type of the object 
    
    7

    A = np.array[[[1, 0, 0, 0, 0, 0],\
                  [0, 0, 2, 0, 0, 1],\
                  [0, 0, 0, 2, 0, 0]]]
    print["Dense matrix representation: \n", A]
    print["Memory utilised [bytes]: ", sys.getsizeof[A]]
    print["Type of the object", type[A]]
    
    7
    Dense matrix representation: 
     [[1 0 0 0 0 0]
     [0 0 2 0 0 1]
     [0 0 0 2 0 0]]
    Memory utilised [bytes]:  184
    Type of the object 
    
    3
    S = csr_matrix[A]
    print["Sparse 'row' matrix: \n",S]
    print["Memory utilised [bytes]: ", sys.getsizeof[S]]
    print["Type of the object", type[S]]
    
    0

    A = np.array[[[1, 0, 0, 0, 0, 0],\
                  [0, 0, 2, 0, 0, 1],\
                  [0, 0, 0, 2, 0, 0]]]
    print["Dense matrix representation: \n", A]
    print["Memory utilised [bytes]: ", sys.getsizeof[A]]
    print["Type of the object", type[A]]
    
    0
    S = csr_matrix[A]
    print["Sparse 'row' matrix: \n",S]
    print["Memory utilised [bytes]: ", sys.getsizeof[S]]
    print["Type of the object", type[S]]
    
    2

    A = np.array[[[1, 0, 0, 0, 0, 0],\
                  [0, 0, 2, 0, 0, 1],\
                  [0, 0, 0, 2, 0, 0]]]
    print["Dense matrix representation: \n", A]
    print["Memory utilised [bytes]: ", sys.getsizeof[A]]
    print["Type of the object", type[A]]
    
    2
    S = csr_matrix[A]
    print["Sparse 'row' matrix: \n",S]
    print["Memory utilised [bytes]: ", sys.getsizeof[S]]
    print["Type of the object", type[S]]
    
    4
    Dense matrix representation: 
     [[1 0 0 0 0 0]
     [0 0 2 0 0 1]
     [0 0 0 2 0 0]]
    Memory utilised [bytes]:  184
    Type of the object 
    
    5
    S = csr_matrix[A]
    print["Sparse 'row' matrix: \n",S]
    print["Memory utilised [bytes]: ", sys.getsizeof[S]]
    print["Type of the object", type[S]]
    
    6

    A = np.array[[[1, 0, 0, 0, 0, 0],\
                  [0, 0, 2, 0, 0, 1],\
                  [0, 0, 0, 2, 0, 0]]]
    print["Dense matrix representation: \n", A]
    print["Memory utilised [bytes]: ", sys.getsizeof[A]]
    print["Type of the object", type[A]]
    
    2
    A = np.array[[[1, 0, 0, 0, 0, 0],\
                  [0, 0, 2, 0, 0, 1],\
                  [0, 0, 0, 2, 0, 0]]]
    print["Dense matrix representation: \n", A]
    print["Memory utilised [bytes]: ", sys.getsizeof[A]]
    print["Type of the object", type[A]]
    
    3
    S = csr_matrix[A]
    print["Sparse 'row' matrix: \n",S]
    print["Memory utilised [bytes]: ", sys.getsizeof[S]]
    print["Type of the object", type[S]]
    
    9
    A = np.array[[[1, 0, 0, 0, 0, 0],\
                  [0, 0, 2, 0, 0, 1],\
                  [0, 0, 0, 2, 0, 0]]]
    print["Dense matrix representation: \n", A]
    print["Memory utilised [bytes]: ", sys.getsizeof[A]]
    print["Type of the object", type[A]]
    
    5
    Sparse 'row' matrix:
    [0, 0] 1
    [1, 2] 2
    [1, 5] 1
    [2, 3] 2
    Memory utilised [bytes]: 56
    Type of the object: 
    
    1
    Sparse 'row' matrix:
    [0, 0] 1
    [1, 2] 2
    [1, 5] 1
    [2, 3] 2
    Memory utilised [bytes]: 56
    Type of the object: 
    
    2
    Sparse 'row' matrix:
    [0, 0] 1
    [1, 2] 2
    [1, 5] 1
    [2, 3] 2
    Memory utilised [bytes]: 56
    Type of the object: 
    
    3
    Sparse 'row' matrix:
    [0, 0] 1
    [1, 2] 2
    [1, 5] 1
    [2, 3] 2
    Memory utilised [bytes]: 56
    Type of the object: 
    
    4

    A = np.array[[[1, 0, 0, 0, 0, 0],\
                  [0, 0, 2, 0, 0, 1],\
                  [0, 0, 0, 2, 0, 0]]]
    print["Dense matrix representation: \n", A]
    print["Memory utilised [bytes]: ", sys.getsizeof[A]]
    print["Type of the object", type[A]]
    
    7
    A = np.array[[[1, 0, 0, 0, 0, 0],\
                  [0, 0, 2, 0, 0, 1],\
                  [0, 0, 0, 2, 0, 0]]]
    print["Dense matrix representation: \n", A]
    print["Memory utilised [bytes]: ", sys.getsizeof[A]]
    print["Type of the object", type[A]]
    
    3
    Sparse 'row' matrix:
    [0, 0] 1
    [1, 2] 2
    [1, 5] 1
    [2, 3] 2
    Memory utilised [bytes]: 56
    Type of the object: 
    
    7
    A = np.array[[[1, 0, 0, 0, 0, 0],\
                  [0, 0, 2, 0, 0, 1],\
                  [0, 0, 0, 2, 0, 0]]]
    print["Dense matrix representation: \n", A]
    print["Memory utilised [bytes]: ", sys.getsizeof[A]]
    print["Type of the object", type[A]]
    
    5
    Sparse 'row' matrix:
    [0, 0] 1
    [1, 2] 2
    [1, 5] 1
    [2, 3] 2
    Memory utilised [bytes]: 56
    Type of the object: 
    
    1
    Sparse 'row' matrix:
    [0, 0] 1
    [1, 2] 2
    [1, 5] 1
    [2, 3] 2
    Memory utilised [bytes]: 56
    Type of the object: 
    
    2
    Sparse 'row' matrix:
    [0, 0] 1
    [1, 2] 2
    [1, 5] 1
    [2, 3] 2
    Memory utilised [bytes]: 56
    Type of the object: 
    
    3
    S = csc_matrix[A]
    print["Sparse 'column' matrix: \n",S]
    print["Memory utilised [bytes]: ", sys.getsizeof[S]]
    print["Type of the object", type[S]]
    
    223____53
    S = csc_matrix[A]
    print["Sparse 'column' matrix: \n",S]
    print["Memory utilised [bytes]: ", sys.getsizeof[S]]
    print["Type of the object", type[S]]
    
    4

    Dense matrix representation: 
     [[1 0 0 0 0 0]
     [0 0 2 0 0 1]
     [0 0 0 2 0 0]]
    Memory utilised [bytes]:  184
    Type of the object 
    
    2
    S = csc_matrix[A]
    print["Sparse 'column' matrix: \n",S]
    print["Memory utilised [bytes]: ", sys.getsizeof[S]]
    print["Type of the object", type[S]]
    
    6
    S = csc_matrix[A]
    print["Sparse 'column' matrix: \n",S]
    print["Memory utilised [bytes]: ", sys.getsizeof[S]]
    print["Type of the object", type[S]]
    
    7
    Dense matrix representation: 
     [[1 0 0 0 0 0]
     [0 0 2 0 0 1]
     [0 0 0 2 0 0]]
    Memory utilised [bytes]:  184
    Type of the object 
    
    5
    S = csc_matrix[A]
    print["Sparse 'column' matrix: \n",S]
    print["Memory utilised [bytes]: ", sys.getsizeof[S]]
    print["Type of the object", type[S]]
    
    3
    Sparse 'column' matrix:
    [0, 0] 1
    [1, 2] 2
    [2, 3] 2
    [1, 5] 1
    Memory utilised [bytes]: 56
    Type of the object: 
    
    0

    Sparse 'column' matrix:
    [0, 0] 1
    [1, 2] 2
    [2, 3] 2
    [1, 5] 1
    Memory utilised [bytes]: 56
    Type of the object: 
    
    1
    Sparse 'column' matrix:
    [0, 0] 1
    [1, 2] 2
    [2, 3] 2
    [1, 5] 1
    Memory utilised [bytes]: 56
    Type of the object: 
    
    2
    Dense matrix representation: 
     [[1 0 0 0 0 0]
     [0 0 2 0 0 1]
     [0 0 0 2 0 0]]
    Memory utilised [bytes]:  184
    Type of the object 
    
    5
    S = csr_matrix[A]
    print["Sparse 'row' matrix: \n",S]
    print["Memory utilised [bytes]: ", sys.getsizeof[S]]
    print["Type of the object", type[S]]
    
    6

    Sparse 'column' matrix:
    [0, 0] 1
    [1, 2] 2
    [2, 3] 2
    [1, 5] 1
    Memory utilised [bytes]: 56
    Type of the object: 
    
    1
    Sparse 'column' matrix:
    [0, 0] 1
    [1, 2] 2
    [2, 3] 2
    [1, 5] 1
    Memory utilised [bytes]: 56
    Type of the object: 
    
    6

    Sparse 'column' matrix:
    [0, 0] 1
    [1, 2] 2
    [2, 3] 2
    [1, 5] 1
    Memory utilised [bytes]: 56
    Type of the object: 
    
    1
    Sparse 'column' matrix:
    [0, 0] 1
    [1, 2] 2
    [2, 3] 2
    [1, 5] 1
    Memory utilised [bytes]: 56
    Type of the object: 
    
    8

    Sparse 'column' matrix:
    [0, 0] 1
    [1, 2] 2
    [2, 3] 2
    [1, 5] 1
    Memory utilised [bytes]: 56
    Type of the object: 
    
    1
    0 1 0 0 
    0 0 2 0 
    0 3 0 0 
    0 0 5 0 
    0 0 0 4 
    
    Sparse Matrix: 
    0 0 1 
    1 1 2 
    2 2 3 
    3 3 4 
    4 0 5 
    
    0

    Sparse 'column' matrix:
    [0, 0] 1
    [1, 2] 2
    [2, 3] 2
    [1, 5] 1
    Memory utilised [bytes]: 56
    Type of the object: 
    
    1
    0 1 0 0 
    0 0 2 0 
    0 3 0 0 
    0 0 5 0 
    0 0 0 4 
    
    Sparse Matrix: 
    0 0 1 
    1 1 2 
    2 2 3 
    3 3 4 
    4 0 5 
    
    2

    A = np.array[[[1, 0, 0, 0, 0, 0],\
                  [0, 0, 2, 0, 0, 1],\
                  [0, 0, 0, 2, 0, 0]]]
    print["Dense matrix representation: \n", A]
    print["Memory utilised [bytes]: ", sys.getsizeof[A]]
    print["Type of the object", type[A]]
    
    2
    Dense matrix representation: 
     [[1 0 0 0 0 0]
     [0 0 2 0 0 1]
     [0 0 0 2 0 0]]
    Memory utilised [bytes]:  184
    Type of the object 
    
    3
    Sparse 'row' matrix:
    [0, 0] 1
    [1, 2] 2
    [1, 5] 1
    [2, 3] 2
    Memory utilised [bytes]: 56
    Type of the object: 
    
    2
    0 1 0 0 
    0 0 2 0 
    0 3 0 0 
    0 0 5 0 
    0 0 0 4 
    
    Sparse Matrix: 
    0 0 1 
    1 1 2 
    2 2 3 
    3 3 4 
    4 0 5 
    
    6
    0 1 0 0 
    0 0 2 0 
    0 3 0 0 
    0 0 5 0 
    0 0 0 4 
    
    Sparse Matrix: 
    0 0 1 
    1 1 2 
    2 2 3 
    3 3 4 
    4 0 5 
    
    7

    A = np.array[[[1, 0, 0, 0, 0, 0],\
                  [0, 0, 2, 0, 0, 1],\
                  [0, 0, 0, 2, 0, 0]]]
    print["Dense matrix representation: \n", A]
    print["Memory utilised [bytes]: ", sys.getsizeof[A]]
    print["Type of the object", type[A]]
    
    2
    0 1 0 0 
    0 0 2 0 
    0 3 0 0 
    0 0 5 0 
    0 0 0 4 
    
    Sparse Matrix: 
    0 0 1 
    1 1 2 
    2 2 3 
    3 3 4 
    4 0 5 
    
    9

    Các

    convertToSparseMatrix[]1convertToSparseMatrix[]2

    S = csc_matrix[A]
    print["Sparse 'column' matrix: \n",S]
    print["Memory utilised [bytes]: ", sys.getsizeof[S]]
    print["Type of the object", type[S]]
    
    30 1 14convertToSparseMatrix[]50 1 14
    S = csc_matrix[A]
    print["Sparse 'column' matrix: \n",S]
    print["Memory utilised [bytes]: ", sys.getsizeof[S]]
    print["Type of the object", type[S]]
    
    30 1 14
    S = csc_matrix[A]
    print["Sparse 'column' matrix: \n",S]
    print["Memory utilised [bytes]: ", sys.getsizeof[S]]
    print["Type of the object", type[S]]
    
    3convertToSparseMatrix[]0

    convertToSparseMatrix[]1convertToSparseMatrix[]2

    S = csc_matrix[A]
    print["Sparse 'column' matrix: \n",S]
    print["Memory utilised [bytes]: ", sys.getsizeof[S]]
    print["Type of the object", type[S]]
    
    30 1 14
    S = csc_matrix[A]
    print["Sparse 'column' matrix: \n",S]
    print["Memory utilised [bytes]: ", sys.getsizeof[S]]
    print["Type of the object", type[S]]
    
    30 1 14
    A = np.array[[[1, 0, 0, 0, 0, 0],\
                  [0, 0, 2, 0, 0, 1],\
                  [0, 0, 0, 2, 0, 0]]]
    print["Dense matrix representation: \n", A]
    print["Memory utilised [bytes]: ", sys.getsizeof[A]]
    print["Type of the object", type[A]]
    
    070 1 14
    S = csc_matrix[A]
    print["Sparse 'column' matrix: \n",S]
    print["Memory utilised [bytes]: ", sys.getsizeof[S]]
    print["Type of the object", type[S]]
    
    3convertToSparseMatrix[]0

    convertToSparseMatrix[]1convertToSparseMatrix[]2

    S = csc_matrix[A]
    print["Sparse 'column' matrix: \n",S]
    print["Memory utilised [bytes]: ", sys.getsizeof[S]]
    print["Type of the object", type[S]]
    
    30 1 14
    S = csc_matrix[A]
    print["Sparse 'column' matrix: \n",S]
    print["Memory utilised [bytes]: ", sys.getsizeof[S]]
    print["Type of the object", type[S]]
    
    30 1 14
    S = csc_matrix[A]
    print["Sparse 'column' matrix: \n",S]
    print["Memory utilised [bytes]: ", sys.getsizeof[S]]
    print["Type of the object", type[S]]
    
    30 1 14
    A = np.array[[[1, 0, 0, 0, 0, 0],\
                  [0, 0, 2, 0, 0, 1],\
                  [0, 0, 0, 2, 0, 0]]]
    print["Dense matrix representation: \n", A]
    print["Memory utilised [bytes]: ", sys.getsizeof[A]]
    print["Type of the object", type[A]]
    
    19convertToSparseMatrix[]0

    convertToSparseMatrix[]1convertToSparseMatrix[]2

    A = np.array[[[1, 0, 0, 0, 0, 0],\
                  [0, 0, 2, 0, 0, 1],\
                  [0, 0, 0, 2, 0, 0]]]
    print["Dense matrix representation: \n", A]
    print["Memory utilised [bytes]: ", sys.getsizeof[A]]
    print["Type of the object", type[A]]
    
    230 1 14
    S = csc_matrix[A]
    print["Sparse 'column' matrix: \n",S]
    print["Memory utilised [bytes]: ", sys.getsizeof[S]]
    print["Type of the object", type[S]]
    
    30 1 14
    S = csc_matrix[A]
    print["Sparse 'column' matrix: \n",S]
    print["Memory utilised [bytes]: ", sys.getsizeof[S]]
    print["Type of the object", type[S]]
    
    30 1 14
    S = csc_matrix[A]
    print["Sparse 'column' matrix: \n",S]
    print["Memory utilised [bytes]: ", sys.getsizeof[S]]
    print["Type of the object", type[S]]
    
    3
    A = np.array[[[1, 0, 0, 0, 0, 0],\
                  [0, 0, 2, 0, 0, 1],\
                  [0, 0, 0, 2, 0, 0]]]
    print["Dense matrix representation: \n", A]
    print["Memory utilised [bytes]: ", sys.getsizeof[A]]
    print["Type of the object", type[A]]
    
    30

    A = np.array[[[1, 0, 0, 0, 0, 0],\
                  [0, 0, 2, 0, 0, 1],\
                  [0, 0, 0, 2, 0, 0]]]
    print["Dense matrix representation: \n", A]
    print["Memory utilised [bytes]: ", sys.getsizeof[A]]
    print["Type of the object", type[A]]
    
    31

    A = np.array[[[1, 0, 0, 0, 0, 0],\
                  [0, 0, 2, 0, 0, 1],\
                  [0, 0, 0, 2, 0, 0]]]
    print["Dense matrix representation: \n", A]
    print["Memory utilised [bytes]: ", sys.getsizeof[A]]
    print["Type of the object", type[A]]
    
    32

    Output:

    0 1 0 0 
    0 0 2 0 
    0 3 0 0 
    0 0 5 0 
    0 0 0 4 
    
    Sparse Matrix: 
    0 0 1 
    1 1 2 
    2 2 3 
    3 3 4 
    4 0 5 
    

    Làm thế nào để bạn chuyển đổi một ma trận thành một ma trận thưa thớt trong Python?

    Tạo một danh sách trống sẽ đại diện cho danh sách ma trận thưa thớt. Lặp lại thông qua ma trận 2D để tìm các phần tử không phải là không. Nếu một phần tử không phải là 0, hãy tạo một danh sách trống tạm thời. Nối giá trị hàng, giá trị cột và phần tử không phải là không vào danh sách tạm thời.

    Làm thế nào để bạn chuyển đổi một ma trận thành ma trận thưa thớt?

    Sự mô tả.S = thưa thớt [a] Chuyển đổi một ma trận đầy đủ thành dạng thưa thớt bằng cách vắt ra bất kỳ phần tử 0 nào.Nếu một ma trận chứa nhiều số không, việc chuyển đổi ma trận thành lưu trữ thưa thớt sẽ lưu bộ nhớ.S = thưa thớt [m, n] tạo ra một ma trận m -by -n tất cả không.S = sparse[ A ] converts a full matrix into sparse form by squeezing out any zero elements. If a matrix contains many zeros, converting the matrix to sparse storage saves memory. S = sparse[ m,n ] generates an m -by- n all zero sparse matrix.

    Làm thế nào để bạn hiển thị ma trận thưa thớt trong Python?

    Ma trận thưa thớt trong Python..
    Nhập Numpy dưới dạng NP ..
    từ Scipy.Nhập CSR_Matrix thưa thớt ..
    # Tạo biểu diễn 2 chiều của ma trận ..
    A = np.Mảng [[[1, 0, 0, 0, 0, 0], [0, 0, 2, 0, 0, 1], \.
    [0, 0, 0, 2, 0, 0]]].
    in ["Biểu diễn ma trận dày đặc: \ n", a].

    Làm cách nào để lưu một ma trận thưa thớt trong Python?

    Lưu ma trận thưa thớt vào tệp bằng định dạng .npz.Tên tệp [chuỗi] hoặc một tệp mở [đối tượng giống như tệp] nơi dữ liệu sẽ được lưu.using . npz format. Either the file name [string] or an open file [file-like object] where the data will be saved.

    Bài Viết Liên Quan

    Chủ Đề