librosa.util.roll_sparse

librosa.util.roll_sparse(x, shift, axis=0)[source]

Sparse matrix roll

This operation is equivalent to numpy.roll, but operates on sparse matrices.

Parameters:
x : scipy.sparse.spmatrix or np.ndarray

The sparse matrix input

shift : int

The number of positions to roll the specified axis

axis : (0, 1, -1)

The axis along which to roll.

Returns:
x_rolled : same type as x

The rolled matrix, with the same format as x

See also

numpy.roll

Examples

>>> # Generate a random sparse binary matrix
>>> X = scipy.sparse.lil_matrix(np.random.randint(0, 2, size=(5,5)))
>>> X_roll = roll_sparse(X, 2, axis=0)  # Roll by 2 on the first axis
>>> X_dense_r = roll_sparse(X.toarray(), 2, axis=0)  # Equivalent dense roll
>>> np.allclose(X_roll, X_dense_r.toarray())
True