Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove rows in 3D numpy array

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.

like image 677
JAWE Avatar asked Oct 24 '13 09:10

JAWE


People also ask

How do I delete a specific row in NumPy?

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.

How do I delete multiple rows in NumPy?

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().

How do I remove part of a NumPy array?

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.

How do I remove rows and columns in NumPy?

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.


1 Answers

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]]])
like image 132
falsetru Avatar answered Sep 20 '22 15:09

falsetru