What is a sparse matrix in theano?

This recipe explains what is sparse matrix in theano.

Recipe Objective - What is a sparse matrix in theano?

The sparse module is similar to the tensor module in terms of functionality. Because sparse matrices do not hold data in a contiguous array, the difference is hidden under the surface.

Theano has two compressed sparse formats: csc and csr, which are based on columns and rows, respectively. They have the same data, indices, indptr, and form properties.
Attributes:
1. The data attribute is a one-dimensional ndarray that contains all of the sparse matrix's non-zero members.
2. The data position in the sparse matrix is stored using the indices and indptr properties.
3. The shape attribute is the same as the dense matrix's shape attribute.

Access YOLO OCR Character Recognition Project with Source Code

For more related projects -

/projects/data-science-projects/deep-learning-projects
/projects/data-science-projects/tensorflow-projects

Compressed Sparse Column (csc) Matrix

In the Compressed Sparse Column (csc) format, indices refer to indexes within the matrix's column vectors, whereas indptr specifies the position of the column in the data and indices attributes.

# Importing libraries
import numpy as np
import scipy.sparse as sci_sp

# Creating 'data' array
data = np.asarray([10, 20, 30])

# Creating 'indices' array
indices = np.asarray([0, 1, 2])

# Creating 'indptr' array
indptr = np.asarray([0, 3, 2, 1])

# Creating csc matrix
matrix = sci_sp.csc_matrix((data, indices, indptr), shape=(3, 3))
matrix.toarray()

Output -
array([[10,  0,  0],
       [20,  0,  0],
       [30,  0,  0]])

Compressed Sparse Row (csr) Matrix

In the Compressed Sparse Row (csr) format, indices refer to indexes within the matrix's row vectors, whereas indptr specifies the position of the row in the data and indices attributes.

# Importing libraries
import numpy as np
import scipy.sparse as sci_sp

# Creating 'data' array
data = np.asarray([10, 20, 30])

# Creating 'indices' array
indices = np.asarray([0, 1, 2])

# Creating 'indptr' array
indptr = np.asarray([0, 3, 2, 1])

# Creating csr matrix
matrix = sci_sp.csr_matrix((data, indices, indptr), shape=(3, 3))
matrix.toarray()

Output -
array([[10, 20, 30],
       [ 0,  0,  0],
       [ 0,  0,  0]])

In this way, we can create a sparse matrix in theano.

What Users are saying..

profile image

Ed Godalle

Director Data Analytics at EY / EY Tech
linkedin profile url

I am the Director of Data Analytics with over 10+ years of IT experience. I have a background in SQL, Python, and Big Data working with Accenture, IBM, and Infosys. I am looking to enhance my skills... Read More

Relevant Projects

Learn to Build a Polynomial Regression Model from Scratch
In this Machine Learning Regression project, you will learn to build a polynomial regression model to predict points scored by the sports team.

OpenCV Project to Master Advanced Computer Vision Concepts
In this OpenCV project, you will learn to implement advanced computer vision concepts and algorithms in OpenCV library using Python.

Personalized Medicine: Redefining Cancer Treatment
In this Personalized Medicine Machine Learning Project you will learn to classify genetic mutations on the basis of medical literature into 9 classes.

Build a Similar Images Finder with Python, Keras, and Tensorflow
Build your own image similarity application using Python to search and find images of products that are similar to any given product. You will implement the K-Nearest Neighbor algorithm to find products with maximum similarity.

Deploy Transformer-BART Model on Paperspace Cloud
In this MLOps Project you will learn how to deploy a Tranaformer BART Model for Abstractive Text Summarization on Paperspace Private Cloud

Credit Card Default Prediction using Machine learning techniques
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

ML Model Deployment on AWS for Customer Churn Prediction
MLOps Project-Deploy Machine Learning Model to Production Python on AWS for Customer Churn Prediction

Learn to Build an End-to-End Machine Learning Pipeline - Part 1
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, addressing a major challenge in the logistics industry.

AWS Project to Build and Deploy LSTM Model with Sagemaker
In this AWS Sagemaker Project, you will learn to build a LSTM model on Sagemaker for sales forecasting while analyzing the impact of weather conditions on Sales.

Learn to Build an End-to-End Machine Learning Pipeline - Part 2
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, incorporating Hopsworks' feature store and Weights and Biases for model experimentation.