I have a numpy array of dimension (48, 366, 3) and I want to remove the last column from the array to make it (48, 365, 3). What is the best way to do that? (All the entries are integers. I'm using Python v2.6)
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 a column in 2D Numpy Array by column number 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.
Removing Python Array Elements We can delete one or more items from an array using Python's del statement. We can use the remove() method to remove the given item, and pop() method to remove an item at the given index.
Many times we have non-numeric values in NumPy array. These values need to be removed, so that array will be free from all these unnecessary values and look more decent. It is possible to remove all columns containing Nan values using the Bitwise NOT operator and np. isnan() function.
You could try numpy.delete
:
http://docs.scipy.org/doc/numpy/reference/generated/numpy.delete.html
or just get the slice of the array you want and write it to a new array.
For example:
a = np.random.randint(0,2, size=(48,366,3))
b = np.delete(a, np.s_[-1:], axis=1)
print b.shape # <--- (48,365,3)
or equivalently:
b = np.delete(a, -1, axis=1)
or:
b = a[:,:-1,:]
Along the lines:
In []: A= rand(48, 366, 3)
In []: A.shape
Out[]: (48, 366, 3)
In []: A= A[:, :-1, :]
In []: A.shape
Out[]: (48, 365, 3)
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