Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete column in 3d numpy array

I have a numpy array that looks like this

[
[[1,2,3], [4,5,6]],
[[3,8,9], [2,9,4]],
[[7,1,3], [1,3,6]]
]

I want it like this after deleting first column

[
[[2,3], [5,6]],
[[8,9], [9,4]],
[[1,3], [3,6]]
]

so currently the dimension is 3*3*3, after removing the first column it should be 3*3*2

like image 436
Test Test Avatar asked Aug 09 '17 03:08

Test Test


2 Answers

You can slice it as so, where 1: signifies that you only want the second and all remaining columns from the inner most array (i.e. you 'delete' its first column).

>>> a[:, :, 1:]
array([[[2, 3],
        [5, 6]],

       [[8, 9],
        [9, 4]],

       [[1, 3],
        [3, 6]]])
like image 177
Alexander Avatar answered Oct 04 '22 19:10

Alexander


Since you are using numpy I'll mention numpy way of doing this. First of all, the dimension you have specified for the question seems wrong. See below

x = np.array([
[[1,2,3], [4,5,6]],
[[3,8,9], [2,9,4]],
[[7,1,3], [1,3,6]]
])

The shape of x is

x.shape
(3, 2, 3)

You can use numpy.delete to remove a column as shown below

a = np.delete(x, 0, 2)
a
array([[[2, 3],
    [5, 6]],

   [[8, 9],
    [9, 4]],

   [[1, 3],
    [3, 6]]])

To find the shape of a

a.shape
(3, 2, 2)
like image 45
Julu Ahamed Avatar answered Oct 04 '22 17:10

Julu Ahamed