I would like to use python's numpy to change the process according to the specific axis value.
For example, if there is a a[3][3][3]
ndarray like the following,
[[0 12]
[ 3 4 5]
[ 6 7 8]]
[[ 9 10 11]
[12 13 14]
[15 16 17]]
[[18 19 20]
[21 22 23]
[24 25 26]]]
For a[:,:,:1]
, we would like to overwrite 10 or more to 0, for a[:,:,1:2]
, for a[:,:,2:]
, we would like to overwrite 20 or more to 0.
After examining the numpy function, I implemented it as follows and now I can handle it as intended, but I'm thinking about whether I can handle the slice smartly (without slices).
I think there are probably some appropriate functions, but is there a function that is appropriate for such a process?
import numpy as np
defmain():
a=np.range(27).reshape(3,3,3)
print(a)
a0 = a [:, :, :, :1]
a1 = a [:, :, 1:2]
a2 = a [ : , : , 2: ]
a0_update=np.where(a0<10,a0,0)
a1_update=np.where(a1<15,a1,0)
a2_update=np.where(a2<20,a2,0)
a_update=np.dstack((a0_update, a1_update, a2_update))
print(a_update.shape)
print(a_update)
if__name__=="__main__":
main()
numpy.moveaxis—NumPy v1.23 Manual
Returns:result:np.ndarray
Array with moved axes.This array is a viewof the input array.
b=np.moveaxis(a, -1,0)
for i,n in enumerate ([10,15,20]):
b[i][b[i]>=n] = 0
print(a)
#
[[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]]
[[ 9 10 11]
[ 0 13 14]
[ 0 0 17]]
[[ 0 0 0]
[ 0 0 0]
[ 0 0 0]]]
© 2024 OneMinuteCode. All rights reserved.