Index into NumPy array ignoring NaNs in the indexing array
By : jeff
Date : March 29 2020, 07:55 AM
Hope this helps I have an array of zeros , Mask it - code :
mask = ~np.isnan(out)
arr[out[0,mask[0]].astype(int),np.flatnonzero(mask[0])] = 1
arr[out[1,mask[1]].astype(int),np.flatnonzero(mask[1])] = 1
In [171]: out
Out[171]:
array([[ nan, 2., 4., 1., 1.],
[ nan, 3., 4., 4., 4.]])
In [172]: mask = ~np.isnan(out)
...: arr[out[0,mask[0]].astype(int),np.flatnonzero(mask[0])] = 1
...: arr[out[1,mask[1]].astype(int),np.flatnonzero(mask[1])] = 1
...:
In [173]: arr
Out[173]:
array([[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 1., 1.],
[ 0., 1., 0., 0., 0.],
[ 0., 1., 0., 0., 0.],
[ 0., 0., 1., 1., 1.]])
r = np.arange(arr.shape[1])
arr[out[0,mask[0]].astype(int),r[mask[0]]] = 1
arr[out[1,mask[1]].astype(int),r[mask[1]]] = 1
n = arr.shape[1]
linear_idx = (out*n + np.arange(n))
np.put(arr, linear_idx[~np.isnan(linear_idx)].astype(int), 1)
|
How to do numpy combined slicing and array indexing with unknown array dimension
By : user1254073
Date : March 29 2020, 07:55 AM
Hope this helps You can concatenate the Ellipsis with your tuple p, to obtain the tuple Ep that can be used to slice the array: code :
Ep = (Ellipsis,)+p
sliced_arr = arr[Ep]
|
Indexing multi-dimensional array with tuple of indices from an indexing array - NumPy / Python
By : user2958170
Date : March 29 2020, 07:55 AM
To fix the issue you can do Approach #1 Reshape a to 2D keeping the first axis length as the same. Convert each thus 2D-flattened-block to a tuple and then index into b. This tuple-conversion leads to a packing of each elements along the first axis as an indexer to select an element each off b. Finally a reshaping is needed to get a 2D output. Hence, the implementation would look something like this - code :
b[tuple(a.reshape(6,-1))].reshape(m,n)
b[tuple(a)]
b.ravel()[np.ravel_multi_index(a,b.shape)]
In [89]: np.random.seed(0)
...: m,n = 500,500
...: b = np.random.rand(20,20,20,20,20,20)>0.5
...: a = np.random.randint(0,20,(6,m,n))
In [90]: %timeit b[tuple(a)]
14.6 ms ± 184 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [91]: %timeit b.ravel()[np.ravel_multi_index(a,b.shape)]
7.35 ms ± 136 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
|
Indexing 4D array with an indexing array along last two axes - NumPy / Python
By : user2958478
Date : March 29 2020, 07:55 AM
should help you out A simple vectorized using advanced-indexing would be - code :
I,J = np.arange(batch_size)[:,None],np.arange(num_channels)
images[I, J, pixels[...,0], pixels[...,1]] = 1
I,J = np.ogrid[:batch_size,:num_channels]
|
Indexing numpy array with index array of lower dim yields array of higher dim than both
By : Jadson Lourenco
Date : March 29 2020, 07:55 AM
will be helpful for those in need This is known as advanced indexing. Advanced indexing allows you to select arbitrary elements in the input array based on an N-dimensional index. Let's use another example to make it clearer: code :
a = np.random.randint(1, 5, (5,4,3))
v = np.ones((5, 4), dtype=int)
array([[[2, 1, 1],
[3, 4, 4],
[4, 3, 2],
[2, 2, 2]],
[[4, 4, 1],
[3, 3, 4],
[3, 4, 2],
[1, 3, 1]],
[[3, 1, 3],
[4, 3, 1],
[2, 1, 4],
[1, 2, 2]],
...
print(v)
array([[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]])
a[1]
[[4, 4, 1],
[3, 3, 4],
[3, 4, 2],
[1, 3, 1]]
array([[[[4, 4, 1],
[3, 3, 4],
[3, 4, 2],
[1, 3, 1]],
[[4, 4, 1],
[3, 3, 4],
[3, 4, 2],
[1, 3, 1]],
...
|