DATA MUNGING
PYTHON MATRIX
DATA CLEANING 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()
```

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