• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Python / Sparse Matrix in Python / Create Random Sparse Matrix / How To Create Random Sparse Matrix of Specific Density?

How To Create Random Sparse Matrix of Specific Density?

February 21, 2019 by cmdlinetips

How To Create Random Sparse Matrix in Python?
How To Create Random Sparse Matrix in Python?
Sometimes, you may want to create sparse random matrices with specific shape and sparsity for testing out a new method or algorithm. Scipy’s sparse module has very useful functionalities that are of great use to create sparse matrices of specific density and from specific probability distributions.

Let us see some simple examples of create random sparse matrices using Scipy.sparse modules.

Let us first load the necessary Python modules to get started.

# import sparse 
import scipy.sparse as sparse
# import stats
import scipy.stats as stats
# import numpy
import numpy as np

The random in Scipy’s sparse module is useful for creating random sparse matrix. To generate a sparse matrix of specific size, random function takes the number of rows and columns as arguments. In addition, we can specify the sparisty we would like with the argument “density”. In the example below, we are creating a random sparse matrix of size 5×5 with sparsity/density=0.25. Density value of one means the created matrix is a full matrix and density value of zero means the created matrix has no non-zero items. Similary, density=0.5 means the matrix has 50% non-zero elements.

# set random seed to repeat
np.random.seed(42)
# create sparse matrix with density 0.25
A = sparse.random(5, 5, density=0.25)
# Convert the sparse matrix to a full matrix
A.toarray()
array([[0.        , 0.        , 0.86617615, 0.        , 0.70807258],
       [0.        , 0.15601864, 0.        , 0.        , 0.        ],
       [0.        , 0.60111501, 0.        , 0.        , 0.        ],
       [0.        , 0.        , 0.        , 0.        , 0.        ],
       [0.        , 0.        , 0.05808361, 0.15599452, 0.        ]])

sparse.random function in Scipy creates the sparse random matrix in COO format, by default. We can also simply print the random matrix without converting to a dense matrix.

# print sparse matrix
print(A)
  (1, 1)	0.15601864
  (4, 3)	0.15599452
  (4, 2)	0.058083612
  (0, 2)	0.8661761
  (2, 1)	0.601115
  (0, 4)	0.7080726

Also, by default, sparse.random function generates random numbers from uniform distribution between 0 and 1. We can specify the probability distributions manually. Let us create random sparse matrix with just ones and zeros. For that, we specify the argument “data_rvs” to np.ones.

A = sparse.random(5, 5, density=0.25, data_rvs=np.ones)
print(A.toarray())
[[1. 0. 0. 0. 1.]
 [1. 0. 1. 0. 1.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 1.]]

Create Sparse Matrix with random numbers from normal distribution

Let us create sparse matrix with density-0.25 from normal probability distribution with mean=3 and sd=1. We can use Scipy.stats’ norm function to create frozen random variables and use that as arugument to our sparse.random function for sparse matrix.

# specify probability distribution
rvs = stats.norm(loc=3, scale=1).rvs
# create sparse random matrix with specific probability distribution/random numbers.
S = sparse.random(5, 5, density=0.25, data_rvs=rvs)
print(S.toarray())
[[0.         0.         0.         3.35555132 0.        ]
 [0.         0.         2.70660085 0.         0.        ]
 [0.         0.         0.         0.         2.97016143]
 [0.         0.         0.         3.83246186 0.        ]
 [0.         2.97554482 3.41701111 0.         0.        ]]

Create Sparse Matrix with random numbers from Poisson distribution

Just like the above example for normal distribution, we can specify probability distribution. Here we generate random numbers from Poisson distribution with mean=10 specified by stats.poisson function.

np.random.seed(42)
# generate random numbers from Poisson distribution
rvs = stats.poisson(15, loc=10).rvs
# generate sparse poisson random matrix
S = sparse.random(5, 5, density=0.25, data_rvs=rvs)
print(S.toarray())
[[ 0.  0. 21.  0. 24.]
 [ 0. 20.  0.  0.  0.]
 [ 0. 23.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.]
 [ 0.  0. 29. 26.  0.]]

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default Thumbnail3 Ways To Create Sparse Matrix in COO Format with SciPy Default ThumbnailHow To Visualize Sparse Matrix in Python? Default ThumbnailHow To Write Pandas GroupBy Function using Sparse Matrix? Default ThumbnailHow To Slice Rows and Columns of Sparse Matrix in Python?

Filed Under: Create Random Sparse Matrix Tagged With: Create Random Sparse Matrix, Sparse Random Matrix in Scipy

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version