mlrl.common.arrays module

Author: Michael Rapp (michael.rapp.ml@gmail.com)

Provides utility functions for handling arrays.

class mlrl.common.arrays.SparseFormat(*values)

Bases: Enum

Specifies all valid textual representations of sparse matrix formats.

COO = 'coo'
CSC = 'csc'
CSR = 'csr'
DOK = 'dok'
LIL = 'lil'
mlrl.common.arrays.enforce_2d(array: ndarray) ndarray

Converts a given np.ndarray into a two-dimensional array if it is one-dimensional.

Parameters:

array – A np.ndarray to be converted

Returns:

A np.ndarray with at least two dimensions

mlrl.common.arrays.enforce_dense(array, order: str, dtype, sparse_value=0) ndarray

Converts a given array into a np.ndarray, if necessary, and enforces a specific memory layout and data type to be used.

Parameters:
  • array – A np.ndarray, scipy.sparse.spmatrix or scipy.sparse.sparray to be converted

  • order – The memory layout to be used. Must be C or F

  • dtype – The data type to be used

  • sparse_value – The value that should be used for sparse elements in the given array

Returns:

A np.ndarray that uses the given memory layout and data type

mlrl.common.arrays.is_coo(array) bool

Returns whether a given scipy.sparse.spmatrix or scipy.sparse.sparray uses the COO format or not.

Parameters:

array – A scipy.sparse.spmatrix or scipy.sparse.sparray to be checked

Returns:

True, if the given array uses the COO format, False otherwise

mlrl.common.arrays.is_csc(array) bool

Returns whether a given scipy.sparse.spmatrix or scipy.sparse.sparray uses the CSC format or not.

Parameters:

array – A scipy.sparse.spmatrix or scipy.sparse.sparray to be checked

Returns:

True, if the given array uses the CSC format, False otherwise

mlrl.common.arrays.is_csr(array) bool

Returns whether a given scipy.sparse.spmatrix or scipy.sparse.sparray uses the CSR format or not.

Parameters:

array – A scipy.sparse.spmatrix or scipy.sparse.sparray to be checked

Returns:

True, if the given array uses the CSR format, False otherwise

mlrl.common.arrays.is_dok(array) bool

Returns whether a given scipy.sparse.spmatrix or scipy.sparse.sparray uses the DOK format or not.

Parameters:

array – A scipy.sparse.spmatrix or scipy.sparse.sparray to be checked

Returns:

True, if the given array uses the DOK format, False otherwise

mlrl.common.arrays.is_lil(array) bool

Returns whether a given scipy.sparse.spmatrix or scipy.sparse.sparray uses the LIL format or not.

Parameters:

array – A scipy.sparse.spmatrix or scipy.sparse.sparray to be checked

Returns:

True, if the given array uses the LIL format, False otherwise

mlrl.common.arrays.is_sparse(array, supported_formats: Set[SparseFormat] | None = None) bool

Returns whether a given array is a scipy.sparse.spmatrix or scipy.sparse.sparray or not.

Parameters:
  • array – A np.ndarray, scipy.sparse.spmatrix or scipy.sparse.sparray to be checked

  • supported_formats – A set of supported SparseFormat`s, the `scipy.sparse.spmatrix or scipy.sparse.sparray may use or None, if the format should not be checked

Returns:

True, if the given array is a scipy.sparse.spmatrix or scipy.sparse.sparray using one of the supported formats, False otherwise

mlrl.common.arrays.is_sparse_and_memory_efficient(array, sparse_format: SparseFormat, dtype, sparse_values: bool = True) bool

Returns whether a given matrix uses sparse format and is expected to occupy less memory than a dense matrix.

Parameters:
  • array – A np.ndarray, scipy.sparse.spmatrix or scipy.sparse.sparray to be checked

  • sparse_format – The SparseFormat to be used. Must be SparseFormat.CSC or SparseFormat.CSR

  • dtype – The type of the values that should be stored in the matrix

  • sparse_values – True, if the values must explicitly be stored when using a sparse format, False otherwise

Returns:

True, if the given matrix uses a sparse format an is expected to occupy less memory than a dense matrix, False otherwise