I need to remove some rows from a 3D numpy array. For exampe:
a = [[1,2,3]
[4,5,6]
[7,8,9]
[9,8,7]
[6,5,4]
[3,2,1]]
and I want to remove the third row of both pages of the matrix in order to obtain something like:
a = [[1,2,3]
[4,5,6]
[9,8,7]
[6,5,4]]
I have tried with
a = numpy.delete(a, 2, axis=0)
but I can't obtain what I need.
Using the NumPy function np. delete() , you can delete any row and column from the NumPy array ndarray . Specify the axis (dimension) and position (row number, column number, etc.). It is also possible to select multiple rows and columns using a slice or a list.
delete() – The numpy. delete() is a function in Python which returns a new array with the deletion of sub-arrays along with the mentioned axis. By keeping the value of the axis as zero, there are two possible ways to delete multiple rows using numphy. delete().
To remove an element from a NumPy array: Specify the index of the element to remove. Call the numpy. delete() function on the array for the given index.
The delete() method is a built-in method in numpy library and it is used to remove the columns from the given array. The delete() method takes an array and an index position or array of index parameters. It returns an array by deleting the elements at given index or indices.
axis
should 1
.
>>> import numpy
>>> a = [[[1,2,3],
... [4,5,6],
... [7,8,9]],
... [[9,8,7],
... [6,5,4],
... [3,2,1]]]
>>> numpy.delete(a, 2, axis=1)
array([[[1, 2, 3],
[4, 5, 6]],
[[9, 8, 7],
[6, 5, 4]]])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With