DATA MUNGING
DATA CLEANING PYTHON
MACHINE LEARNING RECIPES
PANDAS CHEATSHEET
ALL TAGS
# How to create a sparse Matrix in Python?

This recipe helps you create a sparse Matrix in Python

Many a times we work on matrices in python and making Sparse Matrix manually is quite a hectic process but we know how to use python and using we can do this very well for us.

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.

**Get Closer To Your Dream of Becoming a Data Scientist with 70+ Solved End-to-End ML Projects**

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

So this is the recipe on how we can create a sparse Matrix in Python.

```
import numpy as np
from scipy import sparse
```

We have imported numpy and sparse modules which will be requied.

We have created a matrix of which we will calculate sparse Matrix. ```
matrix = np.array([[9, 8, 7],
[6, 5, 4],
[3, 2, 1]])
print()
print("Original Matrix: \n", matrix)
```

**Explore More Data Science and Machine Learning Projects for Practice. Fast-Track Your Career Transition with ProjectPro**

We have created various sparse matrices by passing the original matix from the required functions.

- Creating Dictionary Of Keys based sparse matrix (DOK).

```
print(sparse.dok_matrix(matrix))
```

- Creating Block Sparse Row matrix (BSR).

```
print(sparse.bsr_matrix(matrix))
```

- Creating Coordinate list matrix (COO)

```
print(sparse.coo_matrix(matrix))
```

- Creating Compressed Sparse Column matrix (CSC)

```
print(sparse.csc_matrix(matrix))
```

- Creating Compressed Sparse Row matrix (CSR)

```
print(sparse.csr_matrix(matrix))
```

- Creating Sparse matrix with DIAgonal storage (DIA)

```
print(sparse.dia_matrix(matrix))
```

- Creating Row-based linked list sparse matrix (LIL)

```
print(sparse.lil_matrix(matrix))
```

Now we are printing the final matrices and the output comes as:

Original Matrix: [[9 8 7] [6 5 4] [3 2 1]] Sparse Matrices: (0, 0) 9 (0, 1) 8 (0, 2) 7 (1, 0) 6 (1, 1) 5 (1, 2) 4 (2, 0) 3 (2, 1) 2 (2, 2) 1 (0, 0) 9 (0, 1) 8 (0, 2) 7 (1, 0) 6 (1, 1) 5 (1, 2) 4 (2, 0) 3 (2, 1) 2 (2, 2) 1 (0, 0) 9 (0, 1) 8 (0, 2) 7 (1, 0) 6 (1, 1) 5 (1, 2) 4 (2, 0) 3 (2, 1) 2 (2, 2) 1 (0, 0) 9 (1, 0) 6 (2, 0) 3 (0, 1) 8 (1, 1) 5 (2, 1) 2 (0, 2) 7 (1, 2) 4 (2, 2) 1 (0, 0) 9 (0, 1) 8 (0, 2) 7 (1, 0) 6 (1, 1) 5 (1, 2) 4 (2, 0) 3 (2, 1) 2 (2, 2) 1 (2, 0) 3 (1, 0) 6 (2, 1) 2 (0, 0) 9 (1, 1) 5 (2, 2) 1 (0, 1) 8 (1, 2) 4 (0, 2) 7 (0, 0) 9 (0, 1) 8 (0, 2) 7 (1, 0) 6 (1, 1) 5 (1, 2) 4 (2, 0) 3 (2, 1) 2 (2, 2) 1

**
Download Materials
**

In this deep learning project, you will find similar images (lookalikes) using deep learning and locality sensitive hashing to find customers who are most likely to click on an ad.

In this deep learning project, you will build a convolutional neural network using MNIST dataset for handwritten digit recognition.

In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

We all at some point in time wished to create our own language as a child! But what if certain words always cooccur with another in a corpus? Thus you can make your own model which will understand which word goes with which one, which words are often coming together etc. This all can be done by building a custom embeddings model which we create in this project

Recommender System Machine Learning Project for Beginners - Learn how to design, implement and train a rule-based recommender system in Python

In this OpenCV project, you will learn to implement advanced computer vision concepts and algorithms in OpenCV library using Python.

Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

In this deep learning project, you will build your own face recognition system in Python using OpenCV and FaceNet by extracting features from an image of a person's face.

In this time series project, you will learn how to build an autoregressive model in Python from Scratch for forecasting time series data.