How to Create a Sparse Matrix in Python

On the off chance that the majority of the components of the lattice have 0 worth, it is known as an inadequate grid. The two significant advantages of utilizing meager grid rather than a basic lattice are:

  • Capacity: There are lesser non-no components than zeros and along these lines lesser memory can be utilized to store just those components.
  • Registering time: Computing time can be saved by coherently planning an information structure crossing just non-zero components.

Example:

0 0 3 0 4            
0 0 5 7 0
0 0 0 0 0
0 2 6 0 0

Meager networks are by and large used in applied AI, for example, in information containing information encodings that map classifications to include and furthermore in whole subfields of AI, for example, regular language handling (NLP).

Addressing a meager network by a 2D exhibit prompts wastage of heaps of memory as zeroes in the grid are of no utilization in a large portion of the cases. Thus, rather than putting away zeroes with non-zero components, we just store non-zero components. This implies putting away non-no components with significantly increases (Row, Column, esteem).

Make a Sparse Matrix in Python

Python’s SciPy gives apparatuses for making scanty frameworks utilizing different information structures, just as instruments for changing a thick lattice over to a meager network. The capacity csr_matrix() is utilized to make an inadequate lattice of compacted meager line design while csc_matrix() is utilized to make a scanty grid of packed inadequate segment design.

# Using csr_matrix()

Syntax:

scipy.sparse.csr_matrix(shape=Nonedtype=None)

Parameters:

shape: Get shape of a matrix

dtype: Data type of the matrix

Example 1:

# Python program to create
# sparse matrix using csr_matrix()
 
# Import required package
import numpy as np
from scipy.sparse import csr_matrix
 
# Creating a 3 * 4 sparse matrix
sparseMatrix = csr_matrix((3, 4), 
                          dtype = np.int8).toarray()
 
# Print the sparse matrix
print(sparseMatrix)

Output:

[[0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]]

Example 2:

# Python program to create
# sparse matrix using csr_matrix()
 
# Import required package
import numpy as np
from scipy.sparse import csr_matrix
 
row = np.array([0, 0, 1, 1, 2, 1])
col = np.array([0, 1, 2, 0, 2, 2])
 
# taking data
data = np.array([1, 4, 5, 8, 9, 6])
 
# creating sparse matrix
sparseMatrix = csr_matrix((data, (row, col)), 
                          shape = (3, 3)).toarray()
 
# print the sparse matrix
print(sparseMatrix)

Output:

[[ 1  4  0]
 [ 8  0 11]
 [ 0  0  9]]

# Using csc_matrix()

Syntax:

scipy.sparse.csc_matrix(shape=Nonedtype=None)

Parameters:

shape: Get shape of a matrix

dtype: Data type of the matrix

Example 1:

# Python program to create
# sparse matrix using csc_matrix()
 
# Import required package
import numpy as np
from scipy.sparse import csc_matrix
 
# Creating a 3 * 4 sparse matrix
sparseMatrix = csc_matrix((3, 4), 
                          dtype = np.int8).toarray()
 
# Print the sparse matrix
print(sparseMatrix)

Output:

[[0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]]

Example 2:

# Python program to create
# sparse matrix using csc_matrix()
 
# Import required package
import numpy as np
from scipy.sparse import csc_matrix
 
row = np.array([0, 0, 1, 1, 2, 1])
col = np.array([0, 1, 2, 0, 2, 2])
 
# taking data
data = np.array([1, 4, 5, 8, 9, 6])
 
# creating sparse matrix
sparseMatrix = csc_matrix((data, (row, col)),
                          shape = (3, 3)).toarray()
 
# print the sparse matrix
print(sparseMatrix)

Output:

[[ 1  4  0]
 [ 8  0 11]
 [ 0  0  9]]

Also Read: What is Scanner Class in Java

Leave a Reply

Your email address will not be published. Required fields are marked *