w3resource

Create and operate on a large Sparse matrix using SciPy's Sparse module

NumPy: Integration with SciPy Exercise-10 with Solution

Write a NumPy program to create a large sparse matrix and perform sparse matrix operations using SciPy's sparse module.

Sample Solution:

Python Code:

import numpy as np  # Import NumPy library
from scipy.sparse import csr_matrix, csc_matrix, coo_matrix  # Import sparse matrix types
from scipy.sparse import random  # Import random for creating sparse matrices

# Create a large sparse matrix using SciPy's random function
rows = 1000
cols = 1000
density = 0.01  # 1% of the matrix will be non-zero
sparse_matrix = random(rows, cols, density=density, format='csr', dtype=np.float64)

# Perform sparse matrix operations

# 1. Convert to different sparse matrix formats
csr_sparse = sparse_matrix.tocsr()  # Compressed Sparse Row format
csc_sparse = sparse_matrix.tocsc()  # Compressed Sparse Column format
coo_sparse = sparse_matrix.tocoo()  # Coordinate format

# 2. Perform matrix-vector multiplication
vector = np.random.rand(cols)
result_vector = csr_sparse.dot(vector)

# 3. Compute the transpose of the sparse matrix
transpose_sparse = csr_sparse.transpose()

# 4. Sum the elements of the sparse matrix
sum_elements = csr_sparse.sum()

# Print the results
print("Original Sparse Matrix (CSR format):")
print(csr_sparse)

print("\nConverted to CSC format:")
print(csc_sparse)

print("\nConverted to COO format:")
print(coo_sparse)

print("\nResult of Matrix-Vector Multiplication:")
print(result_vector)

print("\nTranspose of the Sparse Matrix:")
print(transpose_sparse)

print("\nSum of All Elements in the Sparse Matrix:")
print(sum_elements)

Output:

Original Sparse Matrix (CSR format):
  (0, 120)	0.21471827882142103
  (0, 123)	0.9714321590722751
  (0, 191)	0.6004892254255279
  (0, 394)	0.7671482169838781
  (0, 700)	0.4422269471655804
   -----------------------------------------
   -----------------------------------------
  (999, 397)	0.7854764556001641
  (999, 466)	0.5550121109953703
  (999, 511)	0.5997696356941709
  (999, 795)	0.225460003687874
  (999, 903)	0.0521806689562736
  (999, 961)	0.3517484093330441

Converted to CSC format:
  (14, 0)	0.45502582264034097
  (19, 0)	0.6080508480019577
  (61, 0)	0.6239838568531525
  ------------------------------------------
  -------------------------------------------
  (788, 999)	0.9429382947151952
  (833, 999)	0.9408655523685188
  (900, 999)	0.939157701497818
  (905, 999)	0.8199442453334406

Converted to COO format:
  (0, 120)	0.21471827882142103
  (0, 123)	0.9714321590722751
  (0, 191)	0.6004892254255279
  (0, 394)	0.7671482169838781
  (0, 700)	0.4422269471655804
  (0, 740)	0.49057244031907976
  ------------------------------------------
  ------------------------------------------
  (999, 466)	0.5550121109953703
  (999, 511)	0.5997696356941709
  (999, 795)	0.225460003687874
  (999, 903)	0.0521806689562736
  (999, 961)	0.3517484093330441

Result of Matrix-Vector Multiplication:
[1.83219136 2.28026586 1.27514428 2.80244535 2.39462724 1.55893569
 1.21429974 3.64629258 1.8729332  2.62204268 1.96195769 3.1483823
 2.15159565 1.61667861 1.86943874 1.4542032  3.41904755 2.6786697
 3.46855777 2.12098727 4.86558616 3.77960467 3.66170561 3.75764315
 1.39623894 2.55277811 1.31363469 1.75971283 3.49846573 3.63125698
 3.66404111 2.10773362 2.16817706 2.90085879 0.57371095 1.75962388
--------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
 1.51183919 2.52489521 1.49451129 3.8189812  1.97376929 3.11842007
 1.76878761 2.35815012 2.37133228 4.16304991 2.55236607 4.03507708
 2.22023204 3.39838637 2.19385471 3.10879186 2.63835139 2.2193123
 2.39662796 2.00121437 1.99674452 2.02487001]

Transpose of the Sparse Matrix:
  (120, 0)	0.21471827882142103
  (123, 0)	0.9714321590722751
  (191, 0)	0.6004892254255279
  (394, 0)	0.7671482169838781
  (700, 0)	0.4422269471655804
   ----------------------------------------
   ----------------------------------------
   (397, 999)	0.7854764556001641
  (466, 999)	0.5550121109953703
  (511, 999)	0.5997696356941709
  (795, 999)	0.225460003687874
  (903, 999)	0.0521806689562736
  (961, 999)	0.3517484093330441

Sum of All Elements in the Sparse Matrix:
4997.750713886418

Explanation:

  • Import Libraries:
    • Import the necessary libraries: NumPy for array creation and manipulation, and SciPy's sparse module for handling sparse matrices.
  • Create Sparse Matrix:
    • Define the dimensions (rows and columns) and density (percentage of non-zero elements) for the sparse matrix.
    • Use scipy.sparse.random() to create a large sparse matrix in Compressed Sparse Row (CSR) format.
  • Convert to Different Sparse Matrix Formats:
    • Convert the CSR sparse matrix to Compressed Sparse Column (CSC) and Coordinate (COO) formats using .tocsc() and .tocoo() methods.
  • Perform Matrix-Vector Multiplication:
    • Create a random vector of appropriate size and perform matrix-vector multiplication using the dot() method.
  • Compute Transpose:
    • Compute the transpose of the CSR sparse matrix using the .transpose() method.
  • Sum of elements:
    • Calculate the sum of all elements in the sparse matrix using the .sum() method.
  • Display the original sparse matrix, its converted formats, the result of matrix-vector multiplication, the transpose of the matrix, and the sum of its elements.

Python-Numpy Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Generate a 3D dataset and perform multidimensional scaling (MDS) using SciPy.
Next: Generate random data and perform clustering using SciPy's Hierarchical clustering.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/numpy/create-and-operate-on-a-large-sparse-matrix-using-scipys-sparse-module.php