Skip to content Skip to sidebar Skip to footer

Multiple Indices For Numpy Array: Indexerror: Failed To Coerce Slice Entry Of Type Numpy.ndarray To Integer

Is there a way to do multiple indexing in a numpy array as described below? arr=np.array([55, 2, 3, 4, 5, 6, 7, 8, 9]) arr[np.arange(0,2):np.arange(5,7)] output: IndexError: too m

Solution 1:

Here's an approach using strides -

start_index = np.arange(0,2)
L = 5# Interval lengthn = arr.strides[0]
strided = np.lib.stride_tricks.as_strided
out = strided(arr[start_index[0]:],shape=(len(start_index),L),strides=(n,n))

Sample run -

In [976]: arr
Out[976]: array([55, 52, 13, 64, 25, 76, 47, 18, 69, 88])

In [977]: start_index
Out[977]: array([2, 3, 4])

In [978]: L =5In [979]: outOut[979]: 
array([[13, 64, 25, 76, 47],
       [64, 25, 76, 47, 18],
       [25, 76, 47, 18, 69]])

Post a Comment for "Multiple Indices For Numpy Array: Indexerror: Failed To Coerce Slice Entry Of Type Numpy.ndarray To Integer"