sklearn.datasets.make_sparse_spd_matrix(n_dim=None, *, alpha=0.95, norm_diag=False, smallest_coef=0.1, largest_coef=0.9, sparse_format=None, random_state=None, dim='deprecated')[source]#

Generate a sparse symmetric definite positive matrix.

Read more in the User Guide.

n_dimint, default=1

The size of the random matrix to generate.

Changed in version 1.4: Renamed from dim to n_dim.

alphafloat, default=0.95

The probability that a coefficient is zero (see notes). Larger values enforce more sparsity. The value should be in the range 0 and 1.

norm_diagbool, default=False

Whether to normalize the output matrix to make the leading diagonal elements all 1.

smallest_coeffloat, default=0.1

The value of the smallest coefficient between 0 and 1.

largest_coeffloat, default=0.9

The value of the largest coefficient between 0 and 1.

sparse_formatstr, default=None

String representing the output sparse format, such as ‘csc’, ‘csr’, etc. If None, return a dense numpy ndarray.

Added in version 1.4.

random_stateint, RandomState instance or None, default=None

Determines random number generation for dataset creation. Pass an int for reproducible output across multiple function calls. See Glossary.

dimint, default=1

The size of the random matrix to generate.

Deprecated since version 1.4: dim is deprecated and will be removed in 1.6.

precndarray or sparse matrix of shape (dim, dim)

The generated matrix. If sparse_format=None, this would be an ndarray. Otherwise, this will be a sparse matrix of the specified format.

See also


Generate a random symmetric, positive-definite matrix.


The sparsity is actually imposed on the cholesky factor of the matrix. Thus alpha does not translate directly into the filling fraction of the matrix itself.


>>> from sklearn.datasets import make_sparse_spd_matrix
>>> make_sparse_spd_matrix(n_dim=4, norm_diag=False, random_state=42)
array([[1., 0., 0., 0.],
       [0., 1., 0., 0.],
       [0., 0., 1., 0.],
       [0., 0., 0., 1.]])