1.numpy.argpartition()
numpy.argpartition(a, kth, axis=-1, kind=‘introselect’, order=None)
数组元素中从小到大的第k个值将在处于其最终排序位置k
np.partition(a,2)
表示数组a中第2小的元素即元素2位于排序完成数组b的第二个位置上,即索引b[1]处,
然后小于该元素的位于该元素左边,大于该元素的位于右边,左右两边没有特别的排序要求,只要求左边小于该元素,右边大于该元素即可
numpy.argpartition() 返回索引
2.numpy.where()
numpy.where(condition, [x=None, y=None]) Return elements chosen from x or y depending on condition.
numpy.where(condition, [x=None, y=None]) Return elements chosen from x or y depending on condition.
满足条件condition,输出x,不满足输出y。
x = [0 1 2 3 4 5 6 7 8 9]
y = np.where(x < 5, x, 10 * x)
print(y)
#[ 0 1 2 3 4 50 60 70 80 90]
只有condition,没有x和y,则输出满足条件 (即非0) 元素的坐标 (等价于numpy.nonzero)。这里的坐标以tuple的形式给出,通常原数组有多少维,输出的tuple中就包含几个数组,分别对应符合条件元素的各维坐标。
3numpy.searchsorted()
numpy.searchsorted(a, v[, side=‘left’, sorter=None]) Find indices where elements should be inserted to maintain order.
a:一维输入数组。当sorter参数为None的时候,a必须为升序数组;否则,sorter不能为空,存放a中元素的index,用于反映a数组的升序排列方式。
v:插入a数组的值,可以为单个元素,list或者ndarray。
side:查询方向,当为left时,将返回第一个符合条件的元素下标;当为right时,将返回最后一个符合条件的元素下标。
sorter:一维数组存放a数组元素的 index,index 对应元素为升序。
4.
np.unique(A)
对于一维数组或者列表,unique函数去除其中重复的元素,并按元素由大到小返回一个新的无元素重复的元组或者列表
求两个集合的交集:
numpy.intersect1d(ar1, ar2, assume_unique=False, return_indices=False) Find the intersection of two arrays.
求两个集合的并集:
numpy.union1d(ar1, ar2)
求两个集合的差集:
numpy.setdiff1d(ar1, ar2, assume_unique=False) Find the set difference of two arrays.
集合的差,即元素存在于第一个函数不存在于第二个函数中。
求两个集合的异或:
setxor1d(ar1, ar2, assume_unique=False) Find the set exclusive-or of two arrays.
**获取给定数组a中比7大的数有多少** a = np.random.uniform(1, 50, 10) print(a) len(a) - np.searchsorted(a, 7, side='right') **如何删除numpy数组中的缺失值?** a = np.array([1, 2, 3, np.nan, 5, 6, 7, np.nan]) b = np.isnan(a) c = np.where(np.logical_not(b)) print(a[c]) # [1. 2. 3. 5. 6. 7.] **取出每一列比第三大的数字小的数** np.random.seed(100) x = np.random.randint(1, 30, [8, 3]) print(x) z = np.argpartition(x, kth=2, axis=0) y = np.array([[x[z[i, j], j] for j in range(3)] for i in range(2)]) print(y) np.partition(x, kth=2, axis=0)[:2] **从arr中提取所有奇数** arr = np.arange(10) # 方法1 index = np.where(arr % 2 == 1) print(arr[index]) # 方法2 x = arr[arr % 2 == 1] print(x) **将arr中的偶数元素替换为0,而不替换arr** arr = np.arange(10) # 方法1 x = np.where(arr % 2 == 0, 0, arr) print(x) # 方法2 x = np.copy(arr) x[x % 2 == 0] = 0 print(x) **获取给定数组a中前5个最大值的位置** np.random.seed(100) a = np.random.uniform(1, 50, 20) print(a) # [27.62684215 14.64009987 21.80136195 42.39403048 1.23122395 6.95688692 # 33.86670515 41.466785 7.69862289 29.17957314 44.67477576 11.25090398 # 10.08108276 6.31046763 11.76517714 48.95256545 40.77247431 9.42510962 # 40.99501269 14.42961361] # 方法1 b = np.argsort(a) print(b) print(b[-5:]) # [18 7 3 10 15] # 方法2 b = np.sort(a) b = np.where(a >= b[-5]) print(b) # (array([ 3, 7, 10, 15, 18], dtype=int64),) # 方法3 b = np.argpartition(a, kth=-5) print(b[-5:]) # [18 7 3 10 15]



