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:
- Chặn ma trận hàng thưa thớt
- Một ma trận thưa thớt ở định dạng tọa độ.
- Ma trận cột thưa thớt nén
- Ma trận hàng thưa thớt nén
- Ma trận thưa thớt với lưu trữ đường chéo
- Từ điển của ma trận thưa thớt dựa trên chìa khóa.
- Danh sách danh sách dựa trên hàng Ma trận thưa thớt
- 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
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 0Vớ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:
- 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
- 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.
- 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.
- 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]]
1A = 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]]
2A = 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]]
4A = 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]]
6A = 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]]
7A = 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]]
9A = 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
1Dense 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
2Dense 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
3Dense 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
4Dense 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
5Dense 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
6Dense 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
7A = 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]]
7Dense 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
3S = csr_matrix[A]
print["Sparse 'row' matrix: \n",S]
print["Memory utilised [bytes]: ", sys.getsizeof[S]]
print["Type of the object", type[S]]
0A = 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]]
2A = 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]]
2S = csr_matrix[A]
print["Sparse 'row' matrix: \n",S]
print["Memory utilised [bytes]: ", sys.getsizeof[S]]
print["Type of the object", type[S]]
4Dense 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
5S = csr_matrix[A]
print["Sparse 'row' matrix: \n",S]
print["Memory utilised [bytes]: ", sys.getsizeof[S]]
print["Type of the object", type[S]]
6A = 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]]
2A = 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]]
9A = 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:
1Sparse 'row' matrix:
[0, 0] 1
[1, 2] 2
[1, 5] 1
[2, 3] 2
Memory utilised [bytes]: 56
Type of the object:
2Sparse 'row' matrix:
[0, 0] 1
[1, 2] 2
[1, 5] 1
[2, 3] 2
Memory utilised [bytes]: 56
Type of the object:
3Sparse 'row' matrix:
[0, 0] 1
[1, 2] 2
[1, 5] 1
[2, 3] 2
Memory utilised [bytes]: 56
Type of the object:
4A = 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]]
7A = 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:
7A = 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:
1Sparse 'row' matrix:
[0, 0] 1
[1, 2] 2
[1, 5] 1
[2, 3] 2
Memory utilised [bytes]: 56
Type of the object:
2Sparse 'row' matrix:
[0, 0] 1
[1, 2] 2
[1, 5] 1
[2, 3] 2
Memory utilised [bytes]: 56
Type of the object:
3S = csc_matrix[A]
print["Sparse 'column' matrix: \n",S]
print["Memory utilised [bytes]: ", sys.getsizeof[S]]
print["Type of the object", type[S]]
223____53S = csc_matrix[A]
print["Sparse 'column' matrix: \n",S]
print["Memory utilised [bytes]: ", sys.getsizeof[S]]
print["Type of the object", type[S]]
4Dense 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
2S = 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]]
7Dense 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:
0Sparse 'column' matrix:
[0, 0] 1
[1, 2] 2
[2, 3] 2
[1, 5] 1
Memory utilised [bytes]: 56
Type of the object:
1Sparse 'column' matrix:
[0, 0] 1
[1, 2] 2
[2, 3] 2
[1, 5] 1
Memory utilised [bytes]: 56
Type of the object:
2Dense 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]]
6Sparse 'column' matrix:
[0, 0] 1
[1, 2] 2
[2, 3] 2
[1, 5] 1
Memory utilised [bytes]: 56
Type of the object:
1Sparse 'column' matrix:
[0, 0] 1
[1, 2] 2
[2, 3] 2
[1, 5] 1
Memory utilised [bytes]: 56
Type of the object:
6Sparse 'column' matrix:
[0, 0] 1
[1, 2] 2
[2, 3] 2
[1, 5] 1
Memory utilised [bytes]: 56
Type of the object:
1Sparse 'column' matrix:
[0, 0] 1
[1, 2] 2
[2, 3] 2
[1, 5] 1
Memory utilised [bytes]: 56
Type of the object:
8Sparse 'column' matrix:
[0, 0] 1
[1, 2] 2
[2, 3] 2
[1, 5] 1
Memory utilised [bytes]: 56
Type of the object:
10 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 50
Sparse 'column' matrix:
[0, 0] 1
[1, 2] 2
[2, 3] 2
[1, 5] 1
Memory utilised [bytes]: 56
Type of the object:
10 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 52
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]]
2Dense 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
3Sparse 'row' matrix:
[0, 0] 1
[1, 2] 2
[1, 5] 1
[2, 3] 2
Memory utilised [bytes]: 56
Type of the object:
20 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 56
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 57
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]]
20 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 59
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 1
4convertToSparseMatrix[]
50 1 1
4S = 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 1
4S = csc_matrix[A]
print["Sparse 'column' matrix: \n",S]
print["Memory utilised [bytes]: ", sys.getsizeof[S]]
print["Type of the object", type[S]]
3convertToSparseMatrix[]
0convertToSparseMatrix[]
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 1
4S = 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 1
4A = 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 1
4S = csc_matrix[A]
print["Sparse 'column' matrix: \n",S]
print["Memory utilised [bytes]: ", sys.getsizeof[S]]
print["Type of the object", type[S]]
3convertToSparseMatrix[]
0convertToSparseMatrix[]
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 1
4S = 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 1
4S = 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 1
4A = 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[]
0convertToSparseMatrix[]
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 1
4S = 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 1
4S = 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 1
4S = csc_matrix[A]
print["Sparse 'column' matrix: \n",S]
print["Memory utilised [bytes]: ", sys.getsizeof[S]]
print["Type of the object", type[S]]
3A = 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]]
30A = 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]]
31A = 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]]
32Output:
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