SparseRandomMatrix#
- class xesn.SparseRandomMatrix(n_rows, n_cols, factor, distribution, normalization='multiply', density=None, sparsity=None, connectedness=None, format='coo', random_seed=None)#
Similar to
RandomMatrix, but used to create a sparse random matrix. Additional controls are the density and sparse array layout.Example
Create a sparse 100x100 random matrix with entries from a Gaussian distribution with its spectral radius set to 0.9
>>> from xesn import SparseRandomMatrix ; from scipy.sparse.linalg import eigs >>> mat = SparseRandomMatrix(100, 100, factor=0.9, distribution="normal", normalization="eig", connectedness=1, random_seed=0) >>> A = mat() >>> A <100x100 sparse matrix of type '<class 'numpy.float64'>' with 100 stored elements in COOrdinate format> >>> A.nnz 100 >>> mat.density 0.01 >>> max(abs(eigs(A, k=1, which="LM", return_eigenvectors=False))) 0.9000000000000011
- Parameters:
n_rows (int) – number of rows in the matrix
n_cols (int) – number of columns in the matrix
factor (float, optional) – factor to rescale the matrix with after it’s been normalized
distribution (str) – distribution to draw elements of the matrix from, either “uniform”, or “gaussian” and “normal” are recognized
normalization (str, optional) – method used to rescale the matrix, see
normalize()density (float) – use ‘density’, ‘sparsity’ or ‘connectedness’ to specify the number of nonzero values in the matrix, where ‘density’ sets the fraction of nonzero values,
sparsity=1 - density, anddensity = connectedness / n_rows, but note that ‘connectedness’ only makes sense for square matricessparsity (float) – use ‘density’, ‘sparsity’ or ‘connectedness’ to specify the number of nonzero values in the matrix, where ‘density’ sets the fraction of nonzero values,
sparsity=1 - density, anddensity = connectedness / n_rows, but note that ‘connectedness’ only makes sense for square matricesconnectedness (float) – use ‘density’, ‘sparsity’ or ‘connectedness’ to specify the number of nonzero values in the matrix, where ‘density’ sets the fraction of nonzero values,
sparsity=1 - density, anddensity = connectedness / n_rows, but note that ‘connectedness’ only makes sense for square matricesrandom_seed (int, optional) – used to control the RNG for matrix generation
Methods
The main routine to use.
Create either a uniform or normal random matrix.
Rescale a random matrix either through simple multiplication, or by first normalizing by the matrix's spectral radius or induced 2 norm.
Attributes
SparseRandomMatrix.densitySparseRandomMatrix.formatSparseRandomMatrix.distributionSparseRandomMatrix.factorSparseRandomMatrix.n_colsSparseRandomMatrix.n_rowsSparseRandomMatrix.normalizationSparseRandomMatrix.random_seedSparseRandomMatrix.random_state