What are different types of Sparse matrix methods in Scipy

This recipe explains what are different types of Sparse matrix methods in Scipy

Recipe Objective - What are different types of Sparse matrix methods?

1. data - Returns the non-zero item which is stored in the matrix.

import numpy as np
from scipy.sparse import csr_matrix

arr = np.array([[0, 1, 0], [1, 0, 0], [1, 0, 2]])

print(csr_matrix(arr).data)

2. count_nonzero() - Returns the total nonzeros values.

import numpy as np
from scipy.sparse import csr_matrix

ar = np.array([[0, 3, 0], [1, 0, 1], [1, 0, 2]])

print(csr_matrix(ar).count_nonzero())

3. eliminate_zeros() - Remove the zero entries from the matrix.

import numpy as np
from scipy.sparse import csr_matrix

arr = np.array([[1, 0, 0], [0, 2, 1], [1, 0, 2]])

mtx = csr_matrix(arr)
mtx.eliminate_zeros()

print(mtx)

4. sum_duplicates()- Eliminate the duplicate entries.

import numpy as np
from scipy.sparse import csr_matrix

arr = np.array([[1, 0, 0], [0, 0, 1], [1, 0, 2]])

mtx = csr_matrix(arr)
mtx.sum_duplicates()

print(mtx)

5. tocsc() - Convert from CSR to CSC.

import numpy as np
from scipy.sparse import csr_matrix

arr = np.array([[0, 0, 0], [0, 0, 1], [1, 0, 2]])

new_arr = csr_matrix(arr).tocsc()

print(new_arr)

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

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

Loan Eligibility Prediction in Python using H2O.ai
In this loan prediction project you will build predictive models in Python using H2O.ai to predict if an applicant is able to repay the loan or not.

BigMart Sales Prediction ML Project in Python
The goal of the BigMart Sales Prediction ML project is to build and evaluate different predictive models and determine the sales of each product at a store.

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.

Build Deep Autoencoders Model for Anomaly Detection in Python
In this deep learning project , you will build and deploy a deep autoencoders model using Flask.

Digit Recognition using CNN for MNIST Dataset in Python
In this deep learning project, you will build a convolutional neural network using MNIST dataset for handwritten digit recognition.

Build Customer Propensity to Purchase Model in Python
In this machine learning project, you will learn to build a machine learning model to estimate customer propensity to purchase.

AWS MLOps Project to Deploy a Classification Model [Banking]
In this AWS MLOps project, you will learn how to deploy a classification model using Flask on AWS.

PyCaret Project to Build and Deploy an ML App using Streamlit
In this PyCaret Project, you will build a customer segmentation model with PyCaret and deploy the machine learning application using Streamlit.

Build a Review Classification Model using Gated Recurrent Unit
In this Machine Learning project, you will build a classification model in python to classify the reviews of an app on a scale of 1 to 5 using Gated Recurrent Unit.