20

Imagine we have a 5x4 matrix. We need to remove only the first dimension. How can we do it with numpy?

array([[  0.,   1.,   2.,   3.],
       [  4.,   5.,   6.,   7.],
       [  8.,   9.,  10.,  11.],
       [ 12.,  13.,  14.,  15.],
       [ 16.,  17.,  18.,  19.]], dtype=float32)

I tried:

arr = np.arange(20, dtype=np.float32)
matrix = arr.reshape(5, 4)
new_arr = numpy.delete(matrix, matrix[:,0])
trimmed_matrix = new_arr.reshape(5, 3)

It looks a bit clunky. Am I doing it correctly? If yes, is there a cleaner way to remove the dimension without reshaping?

3
  • You want to end up with a (5, 3) array? Then you want to delete a column (or in general, an 'entry' from a dimension). Removing a dimension would be changing to a (5,) or a (4,) array. Commented Nov 30, 2015 at 20:48
  • 3
    It seems you want to remove the first column from a 2D array. This can be done like this: arr[:,1:]. Commented Nov 30, 2015 at 20:48
  • 1
    np.delete works by index, not value. It is not list remove. Commented Nov 30, 2015 at 21:38

3 Answers 3

57

If you want to remove a column from a 2D Numpy array you can specify the columns like this

to keep all rows and to get rid of column 0 (or start at column 1 through the end)

a[:,1:]

another way you can specify the columns you want to keep ( and change the order if you wish) This keeps all rows and only uses columns 0,2,3

a[:,[0,2,3]]

The documentation on this can be found here

And if you want something which specifically removes columns you can do something like this:

idxs = list.range(4)
idxs.pop(2) #this removes elements from the list
a[:, idxs]

and @hpaulj brought up numpy.delete()

This would be how to return a view of 'a' with 2 columns removed (0 and 2) along axis=1.

np.delete(a,[0,2],1)

This doesn't actually remove the items from 'a', it's return value is a new numpy array.

Sign up to request clarification or add additional context in comments.

1 Comment

Only [:,1:] returns a view (i.e. uses the same data buffer). The others make a copy.
9

The correct way to use delete is to specify index and dimension, eg. remove the 1st (0) column (dimension 1):

In [215]: np.delete(np.arange(20).reshape(5,4),0,1)
Out[215]: 
array([[ 1,  2,  3],
       [ 5,  6,  7],
       [ 9, 10, 11],
       [13, 14, 15],
       [17, 18, 19]])

other expressions that work:

np.arange(20).reshape(5,4)[:,1:]
np.arange(20).reshape(5,4)[:,[1,2,3]]
np.arange(20).reshape(5,4)[:,np.array([False,True,True,True])]

Comments

4

You don't need the second reshape.

matrix=np.delete(matrix,0,1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.