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(arrobjaxis=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

Input array.

obj : slice, int or array of ints

Indicate which sub-arrays to remove.

axis : int, optional

The axis along which to delete the subarray defined by obj. If axis is None, obj is applied to the flattened array.

Returns:

out : ndarray

A copy of arr with the elements specified by obj removed. Note that delete does not occur in-place. If axis is None, out is a flattened array.

From https://docs.scipy.org/doc/numpy/reference/generated/numpy.delete.html

posted @ 2018-02-02 08:45  YWU  阅读(3739)  评论(0)    收藏  举报