How To Create Random Sparse Matrix of Specific Density?

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.]]