I have an Array[0]
with len(Array[0]) = 40
I want to delete the last 3 values in this array.
np.delete(Array[0],[36,39])
Works as it should. However,
np.delete(Array[0],[len(Array[0])-4,len(Array[0])-1])
Deletes only the last number (instead of the last 3).
np.delete(Array[0],[-3,-1])
Doesn't do anything and gives me the warning: main :1: FutureWarning: in the future negative indices will not be ignored by numpy.delete
.
What is the correct way to do this?
You could use range(37, 40)
along with axis
keyword argument to delete the last three columns. Here is a contrived example:
In [88]: arr = np.random.random_sample((3, 10))
In [89]: arr.shape
Out[89]: (3, 10)
In [90]: new_arr = np.delete(arr, range(7, 10), axis=1)
In [91]: new_arr.shape
Out[91]: (3, 7)
Please note that numpy.delete()
would return a new sub-array. If you're wary of memory requirements, you should slice as Divakar suggested .
sliced_arr = Array[0][:-3]
But then you've to be careful when you modify the sliced array since that's a view of the original array.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.