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

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

Skip Gram Model Python Implementation for Word Embeddings
Skip-Gram Model word2vec Example -Learn how to implement the skip gram algorithm in NLP for word embeddings on a set of documents.

Learn How to Build a Linear Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple linear regression model in PyTorch to predict the number of days subscribed.

Build ARCH and GARCH Models in Time Series using Python
In this Project we will build an ARCH and a GARCH model using Python

Census Income Data Set Project-Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based oncensus data.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Build Multi Class Text Classification Models with RNN and LSTM
In this Deep Learning Project, you will use the customer complaints data about consumer financial products to build multi-class text classification models using RNN and LSTM.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Build a Text Classification Model with Attention Mechanism NLP
In this NLP Project, you will learn to build a multi class text classification model with attention mechanism.

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.

Recommender System Machine Learning Project for Beginners-3
Content Based Recommender System Project - Building a Content-Based Product Recommender App with Streamlit