How to create a sparse Matrix in Python?

How to create a sparse Matrix in Python?

This recipe helps you create a sparse Matrix in Python

There are two popular kinds of matrices: dense and sparse. Sparse matrices have lots of 'zero' values. In machine learning projects, the learning algorithms require the data to be in-memory. If the data needed for the learning (dataframe) is not in the RAM, then the algorithm does not work. By converting a dense matrix into a sparse matrix it can be made to fit in the RAM.

There are many data structures that can be used to construct a sparse matrix in python. Python Scipy provides the following ways to represent a sparse matrix:
- Block Sparse Row matrix (BSR)
- Coordinate list matrix (COO)
- Compressed Sparse Column matrix (CSC)
- Compressed Sparse Row matrix (CSR)
- Sparse matrix with DIAgonal storage (DIA)
- Dictionary Of Keys based sparse matrix (DOK)
- Row-based linked list sparse matrix (LIL)

The recipe above takes a dense matrix and displays the various formats of sparse matrix that scipy supports.

References: https://docs.scipy.org/doc/scipy/reference/sparse.html

In [1]:
## How to Create A Sparse Matrix
def Kickstarter_Example_2():
    print()
    print(format('How to Create A Sparse Matrix', '*^50'))

    # Load libraries
    import numpy as np
    from scipy import sparse

    # Create a matrix
    matrix = np.array([[1, 2, 3],
                   [4, 5, 6],
                   [7, 8, 9]])
    print()
    print("Original Matrix: \n", matrix)

    # Create sparse matrices
    print()
    print("Sparse Matrices: ")
    print()
    print(sparse.csr_matrix(matrix))
    print()
    print(sparse.bsr_matrix(matrix))
    print()
    print(sparse.coo_matrix(matrix))
    print()
    print(sparse.csc_matrix(matrix))
    print()
    print(sparse.dia_matrix(matrix))
    print()
    print(sparse.dok_matrix(matrix))
    print()
    print(sparse.lil_matrix(matrix))
    print()
Kickstarter_Example_2()
**********How to Create A Sparse Matrix***********

Original Matrix:
 [[1 2 3]
 [4 5 6]
 [7 8 9]]

Sparse Matrices:

  (0, 0)	1
  (0, 1)	2
  (0, 2)	3
  (1, 0)	4
  (1, 1)	5
  (1, 2)	6
  (2, 0)	7
  (2, 1)	8
  (2, 2)	9

  (0, 0)	1
  (0, 1)	2
  (0, 2)	3
  (1, 0)	4
  (1, 1)	5
  (1, 2)	6
  (2, 0)	7
  (2, 1)	8
  (2, 2)	9

  (0, 0)	1
  (0, 1)	2
  (0, 2)	3
  (1, 0)	4
  (1, 1)	5
  (1, 2)	6
  (2, 0)	7
  (2, 1)	8
  (2, 2)	9

  (0, 0)	1
  (1, 0)	4
  (2, 0)	7
  (0, 1)	2
  (1, 1)	5
  (2, 1)	8
  (0, 2)	3
  (1, 2)	6
  (2, 2)	9

  (2, 0)	7
  (1, 0)	4
  (2, 1)	8
  (0, 0)	1
  (1, 1)	5
  (2, 2)	9
  (0, 1)	2
  (1, 2)	6
  (0, 2)	3

  (0, 0)	1
  (0, 1)	2
  (0, 2)	3
  (1, 0)	4
  (1, 1)	5
  (1, 2)	6
  (2, 0)	7
  (2, 1)	8
  (2, 2)	9

  (0, 0)	1
  (0, 1)	2
  (0, 2)	3
  (1, 0)	4
  (1, 1)	5
  (1, 2)	6
  (2, 0)	7
  (2, 1)	8
  (2, 2)	9



Stuck at work?
Can't find the recipe you are looking for. Let us know and we will find an expert to create the recipe for you. Click here
Companies using this Recipe
1 developer from HvH
1 developer from New Delhi DataPoint
1 developer from Vodafone
1 developer from Altimetrik
1 developer from KPMG
1 developer from Renovite Technologies
1 developer from YASH Technologies
1 developer from ANAC
1 developer from LTI
1 developer from Sony