Numpy 查找数组中特定元素并删除
import numpy as np
# 1. Create an array as an example arr = np.arange(10) ''' arr array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) ''' arr[3:6] = 0 ''' arr array([0, 1, 2, 0, 0, 0, 6, 7, 8, 9]) '''
# 2. Find the indexes of the specific number (Here it is 0) ii = np.argwhere(arr == 0) ''' ii array([[0], [3], [4], [5]]) '''
# 3. Delete the corresponding numbers in the array arr = np.delete(arr,ii,0) ''' arr array([1, 2, 6, 7, 8, 9]) '''
numpy.
delete
(arr, obj, axis=None)
Return a new array with sub-arrays along an axis deleted. For a one dimensional array, this returns those entries not returned by arr[obj].
Parameters: |
arr : array_like
obj : slice, int or array of ints
axis : int, optional
|
---|---|
Returns: |
out : ndarray
|
From https://docs.scipy.org/doc/numpy/reference/generated/numpy.delete.html