check_scipy_sparse#
- pybear.base.check_scipy_sparse(X, allowed)#
Check whether a passed data container is a scipy sparse matrix / array.
If it is, check the type against the allowed types specified by the user in allowed. If X is not a scipy sparse container, skip all checks and return None. If X is an allowed scipy sparse container, return None. If X is a disallowed scipy container, do not recast the passed container to a valid scipy sparse container but raise a TypeError.
- Parameters:
- Xarray_like of shape (n_samples, n_features) or (n_samples, )
The data to be checked whether it is an allowed scipy sparse matrix / array. This parameter is not checked for being a valid data container. It only undergoes checks if it is a scipy sparse container.
- allowedLiteral[False] | None | Sequence[str]
If None or False, disallow any scipy sparse containers. Otherwise, a vector-like sequence of literals indicating the types of scipy sparse matrices / arrays that are allowed. Valid literals are ‘csr’, ‘csc’, ‘coo’, ‘dia’, ‘lil’, ‘dok’, and ‘bsr’. If a disallowed scipy sparse type is passed it is not recast to a valid type, but a TypeError is raised.
- Returns:
- None
- Raises:
- TypeError:
If allowed is None or False and X is a scipy sparse container.
If X is a scipy sparse container but not one of the allowed containers.
Examples
>>> from pybear.base import check_scipy_sparse >>> import numpy as np >>> import scipy.sparse as ss >>> X_np = np.random.uniform(0, 1, (5, 3)) >>> X_csc = ss.csc_array(X_np) >>> print(check_scipy_sparse(X_csc, ['csc', 'csr', 'coo'])) None >>> try: ... check_scipy_sparse(X_csc, False) ... except Exception as e: ... print(repr(e)[:53]) TypeError("X is <class 'scipy.sparse._csc.csc_array'>