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?
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.
To delete a column from a 2D numpy array using np. delete() we need to pass the axis=1 along with numpy array and index of column i.e. It will delete the column at index position 1 from the above created 2D numpy array.
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.
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