python:numpy排序搜索计数及集合

排序

numpy.sort

a. axis:排序沿数组的(轴)方向,0表示按行,1表示按列,None表示展开来排序,默认为-1,表示沿后的轴排序。
b. kind:排序的算法,提供了快排’quicksort’、混排’mergesort’、堆排’heapsort’, 默认为‘quicksort’。
c. order:排序的字段名,可指定字段排序,默认为None。

np.random.seed(20200612) 
x = np.random.rand(5, 5) * 10 
x = np.around(x, 2) 
print(x) 
# [[2.32 7.54 9.78 1.73 6.22] 
#  [6.93 5.17 9.28 9.76 8.25] 
#  [0.01 4.23 0.19 1.73 9.27] 
#  [7.99 4.97 0.88 7.32 4.29] 
#  [9.05 0.07 8.95 7.9  6.99]]
y = np.sort(x) 
print(y) 
# [[1.73 2.32 6.22 7.54 9.78] 
#  [5.17 6.93 8.25 9.28 9.76] 
#  [0.01 0.19 1.73 4.23 9.27] 
#  [0.88 4.29 4.97 7.32 7.99] 
#  [0.07 6.99 7.9  8.95 9.05]]
y = np.sort(x, axis=0) 
print(y) 
# [[0.01 0.07 0.19 1.73 4.29] 
#  [2.32 4.23 0.88 1.73 6.22] 
#  [6.93 4.97 8.95 7.32 6.99] 
#  [7.99 5.17 9.28 7.9  8.25] 
#  [9.05 7.54 9.78 9.76 9.27]]
dt = np.dtype([('name', 'S10'), ('age', np.int)]) 
a = np.array([("Mike", 21), ("Nancy", 25), ("Bob", 17), ("Jane", 27)], dtype=dt) 
b = np.sort(a, order='name') 
print(b) 
# [(b'Bob', 17) (b'Jane', 27) (b'Mike', 21) (b'Nancy', 25)]
b = np.sort(a, order='age') 
print(b) 
# [(b'Bob', 17) (b'Mike', 21) (b'Nancy', 25) (b'Jane', 27)]

numpy.argsort

想用元素的索引位置替代排序后的实际结果

np.random.seed(20200612) 
x = np.random.randint(0, 10, 10) 
print(x) # [6 1 8 5 5 4 1 2 9 1]
y = np.argsort(x) 
print(y) # [1 6 9 7 5 3 4 0 2 8]
print(x[y]) # [1 1 1 2 4 5 5 6 8 9]
y = np.argsort(-x) 
print(y) # [8 2 0 3 4 5 7 1 6 9]
print(x[y]) # [9 8 6 5 5 4 2 1 1 1]

搜索

numpy.argmax
np.random.seed(20200612) 
x = np.random.rand(5, 5) * 10 
x = np.around(x, 2) 
print(x) 
# [[2.32 7.54 9.78 1.73 6.22] 
#  [6.93 5.17 9.28 9.76 8.25] 
#  [0.01 4.23 0.19 1.73 9.27] 
#  [7.99 4.97 0.88 7.32 4.29] 
#  [9.05 0.07 8.95 7.9  6.99]]
y = np.argmax(x) print(y)  # 2
y = np.argmax(x, axis=0) print(y) # [4 0 0 1 2]
y = np.argmax(x, axis=1) print(y) # [2 3 4 0 0]
numpy.argmin
numppy.nonzero

其值为非零元素的下标在对应轴上的值

x = np.array([0, 2, 3]) 
print(x)  # [0 2 3] 
print(x.shape)  # (3,) 
print(x.ndim)  # 1
y = np.nonzero(x) 
print(y)  
# (array([1, 2], dtype=int64),) 
print(np.array(y))  
# [[1 2]]
print(np.array(y).shape)  
# (1, 2) 
print(np.array(y).ndim)  
# 2 print(np.transpose(y)) 
# [[1] 
#  [2]] 
print(x[np.nonzero(x)]) #[2, 3]
numpy.where

Return elements chosen from x or y depending on condition.

x = np.arange(10) 
print(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]
numpy.searchsorted

Find indices where elements should be inserted to maintain order

计数

numpy.count_nonzero

返回数组中的非0元素个数。

x = np.count_nonzero(np.eye(4)) 
print(x)  # 4
x = np.count_nonzero([[0, 1, 7, 0, 0], [3, 0, 0, 2, 19]]) 
print(x)  # 5
x = np.count_nonzero([[0, 1, 7, 0, 0], [3, 0, 0, 2, 19]], axis=0) 
print(x)  # [1 1 1 1 1]
x = np.count_nonzero([[0, 1, 7, 0, 0], [3, 0, 0, 2, 19]], axis=1) 
print(x)  # [2 3]

集合

numpy.unique

Find the unique elements of an array.
return_index=True 表示返回新列表元素在旧列表中的位置。
return_inverse=True表示返回旧列表元素在新列表中的位置。
return_counts=True表示返回新列表元素在旧列表中出现的次数。

x = np.unique([1, 1, 3, 2, 3, 3])
print(x)  # [1 2 3]

x = sorted(set([1, 1, 3, 2, 3, 3]))
print(x)  # [1, 2, 3]

x = np.array([[1, 1], [2, 3]])
u = np.unique(x)
print(u)  # [1 2 3]

x = np.array([[1, 0, 0], [1, 0, 0], [2, 3, 4]])
y = np.unique(x, axis=0)
print(y)
# [[1 0 0]
#  [2 3 4]]

x = np.array(['a', 'b', 'b', 'c', 'a'])
u, index = np.unique(x, return_index=True)
print(u)  # ['a' 'b' 'c']
print(index)  # [0 1 3]
print(x[index])  # ['a' 'b' 'c']

x = np.array([1, 2, 6, 4, 2, 3, 2])
u, index = np.unique(x, return_inverse=True)
print(u)  # [1 2 3 4 6]
print(index)  # [0 1 4 3 1 2 1]
print(u[index])  # [1 2 6 4 2 3 2]

u, count = np.unique(x, return_counts=True)
print(u)  # [1 2 3 4 6]
print(count)  # [1 3 1 1 1]
numpy.intersect1d

求两个集合的交集

import numpy as np
from functools import reduce

x = np.intersect1d([1, 3, 4, 3], [3, 1, 2, 1])
print(x)  # [1 3]

x = np.array([1, 1, 2, 3, 4])
y = np.array([2, 1, 4, 6])
xy, x_ind, y_ind = np.intersect1d(x, y, return_indices=True)
print(x_ind)  # [0 2 4]
print(y_ind)  # [1 0 2]
print(xy)  # [1 2 4]
print(x[x_ind])  # [1 2 4]
print(y[y_ind])  # [1 2 4]

x = reduce(np.intersect1d, ([1, 3, 4, 3], [3, 1, 2, 1], [6, 3, 4, 2]))
print(x)  # [3]
numpy.union1d

求两个集合的并集

import numpy as np
from functools import reduce

x = np.union1d([-1, 0, 1], [-2, 0, 2])
print(x)  # [-2 -1  0  1  2]
x = reduce(np.union1d, ([1, 3, 4, 3], [3, 1, 2, 1], [6, 3, 4, 2]))
print(x)  # [1 2 3 4 6]
'''
setxor1d

求两个集合的异或

import numpy as np

a = np.array([1, 2, 3, 2, 4, 1])
b = np.array([3, 4, 5, 6])
x = np.setxor1d(a, b)
print(x)  # [1 2 5 6]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值