Open In App

Sparse Matrix in Python using Dictionary

A sparse matrix is a matrix in which most of the elements have zero value and thus efficient ways of storing such matrices are required. Sparse matrices are generally utilized in applied machine learning such as in data containing data-encodings that map categories to count and also in entire subfields of machine learning such as natural language processing (NLP).

Examples:



0 0 0 1 0            
2 0 0 0 3
0 0 0 4 0

Above is sparse matrix with only 4 non-zero elements.

Representing a sparse matrix by a 2D array leads to wastage of lots of memory as zeroes in the matrix are of no use in most cases. So, instead of storing zeroes with non-zero elements, we only store non-zero elements. These efficient ways require only the non-zero values to be stored along with their index so that the original matrix can be retrieved when required. One such efficient way in Python is the use of a dictionary. Dictionary in Python stores data in key-value pairs like maps in Java. Dictionary stores data in an unordered manner.

Approach:

Below is the Implementation.






# creating sparse matrix
arr = [[0, 0, 0, 1, 0],
       [2, 0, 0, 0, 3],
       [0, 0, 0, 4, 0]]
 
# creating empty dictionary
dic = {}
 
# iterating through the matrix
for i in range(len(arr)):
    for j in range(len(arr[i])):
        if arr[i][j] != 0:
 
            # adding non zero elements to
            # the dictionary
            dic[i, j] = arr[i][j]
 
print("Position of non-zero elements in the matrix:")
print(dic)

Output:

The following dictionary shows the positional of non-zero elements in the sparse matrix

{(0, 3): 1, (1, 0): 2, (1, 4): 3, (2, 3): 4}

Article Tags :