What is a symbolic sparse matrix in theano?

This recipe explains what is symbolic sparse matrix in theano.

Recipe Objective - What is a symbolic 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.

For more related projects -

https://www.projectpro.io/projects/data-science-projects/deep-learning-projects

https://www.projectpro.io/projects/data-science-projects/tensorflow-projects

Compressed Sparse Column (csc) Matrix

In the Compressed Sparse Column (csc) format, indices refers 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 refers 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 symbolic sparse matrix in theano.

What Users are saying..

profile image

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

Build a Autoregressive and Moving Average Time Series Model
In this time series project, you will learn to build Autoregressive and Moving Average Time Series Models to forecast future readings, optimize performance, and harness the power of predictive analytics for sensor data.

AWS MLOps Project for ARCH and GARCH Time Series Models
Build and deploy ARCH and GARCH time series forecasting models in Python on AWS .

Build Time Series Models for Gaussian Processes in Python
Time Series Project - A hands-on approach to Gaussian Processes for Time Series Modelling in Python

Build a Logistic Regression Model in Python from Scratch
Regression project to implement logistic regression in python from scratch on streaming app data.

Loan Eligibility Prediction Project using Machine learning on GCP
Loan Eligibility Prediction Project - Use SQL and Python to build a predictive model on GCP to determine whether an application requesting loan is eligible or not.

Isolation Forest Model and LOF for Anomaly Detection in Python
Credit Card Fraud Detection Project - Build an Isolation Forest Model and Local Outlier Factor (LOF) in Python to identify fraudulent credit card transactions.

Deep Learning Project for Time Series Forecasting in Python
Deep Learning for Time Series Forecasting in Python -A Hands-On Approach to Build Deep Learning Models (MLP, CNN, LSTM, and a Hybrid Model CNN-LSTM) on Time Series Data.

Detectron2 Object Detection and Segmentation Example Python
Object Detection using Detectron2 - Build a Dectectron2 model to detect the zones and inhibitions in antibiogram images.

Build Piecewise and Spline Regression Models in Python
In this Regression Project, you will learn how to build a piecewise and spline regression model from scratch in Python to predict the points scored by a sports team.

Build a Graph Based Recommendation System in Python-Part 2
In this Graph Based Recommender System Project, you will build a recommender system project for eCommerce platforms and learn to use FAISS for efficient similarity search.