Table Of Contents
Table Of Contents

mxnet.test_utils.rand_sparse_ndarray

mxnet.test_utils.rand_sparse_ndarray(shape, stype, density=None, dtype=None, distribution=None, data_init=None, rsp_indices=None, modifier_func=None, shuffle_csr_indices=False)[source]

Generate a random sparse ndarray. Returns the ndarray, value(np) and indices(np)

Parameters
  • shape (list or tuple) –

  • stype (str) – valid values: “csr” or “row_sparse”

  • density (float, optional) – should be between 0 and 1

  • distribution (str, optional) – valid values: “uniform” or “powerlaw”

  • dtype (numpy.dtype, optional) – default value is None

Returns

Return type

Result of type CSRNDArray or RowSparseNDArray

Examples

Below is an example of the powerlaw distribution with csr as the stype. It calculates the nnz using the shape and density. It fills up the ndarray with exponentially increasing number of elements. If there are enough unused_nnzs, n+1th row will have twice more nnzs compared to nth row. else, remaining unused_nnzs will be used in n+1th row If number of cols is too small and we have already reached column size it will fill up all following columns in all followings rows until we reach the required density.

>>> csr_arr, _ = rand_sparse_ndarray(shape=(5, 16), stype="csr",
                                     density=0.50, distribution="powerlaw")
>>> indptr = csr_arr.indptr.asnumpy()
>>> indices = csr_arr.indices.asnumpy()
>>> data = csr_arr.data.asnumpy()
>>> row2nnz = len(data[indptr[1]:indptr[2]])
>>> row3nnz = len(data[indptr[2]:indptr[3]])
>>> assert(row3nnz == 2*row2nnz)
>>> row4nnz = len(data[indptr[3]:indptr[4]])
>>> assert(row4nnz == 2*row3nnz)