scipy.sparse.

get_index_dtype#

scipy.sparse.get_index_dtype(arrays=(), maxval=None, check_contents=False)[source]#

Based on input (integer) arrays a, determine a suitable index data type that can hold the data in the arrays.

Parameters:
arraystuple of array_like

Input arrays whose types/contents to check

maxvalfloat, optional

Maximum value needed

check_contentsbool, optional

Whether to check the values in the arrays and not just their types. Default: False (check only the types)

Returns:
dtypedtype

Suitable index data type (int32 or int64)

Examples

>>> import numpy as np
>>> from scipy import sparse
>>> # select index dtype based on shape
>>> shape = (3, 3)
>>> idx_dtype = sparse.get_index_dtype(maxval=max(shape))
>>> data = [1.1, 3.0, 1.5]
>>> indices = np.array([0, 1, 0], dtype=idx_dtype)
>>> indptr = np.array([0, 2, 3, 3], dtype=idx_dtype)
>>> A = sparse.csr_array((data, indices, indptr), shape=shape)
>>> A.indptr.dtype
dtype('int32')
>>> # select based on larger of existing arrays and shape
>>> shape = (3, 3)
>>> idx_dtype = sparse.get_index_dtype(A.indptr, maxval=max(shape))
>>> idx_dtype
<class 'numpy.int32'>