scipy.sparse.

get_index_dtype#

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

基於輸入(整數)陣列 a,判斷適合的索引資料類型,該類型可以容納陣列中的資料。

參數:
arraysarray_like 的元組

要檢查類型/內容的輸入陣列

maxvalfloat, 選填

需要的最大值

check_contentsbool, 選填

是否檢查陣列中的值,而不僅僅是其類型。預設值:False(僅檢查類型)

回傳值:
dtypedtype

適合的索引資料類型 (int32 或 int64)

範例

>>> 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'>