Numpy基础_数组排序、搜索和计数

1 数组排序

 numpy.sort(a[, axis=-1, kind='quicksort', order=None])

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

import numpy as np
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]]
numpy.argsort(a[, axis=-1, kind='quicksort', order=None]) 

对数组沿给定轴执行间接排序,并使用指定排序类型返回数据的索引数组。这个索引数组用于构造排序后的数组

import numpy as np
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]
numpy.lexsort(keys[, axis=-1]) Perform an indirect
 stable sort using a sequence of keys.
(使用键序列执行间接稳定排序。)

以索引是 kth 的元素为基准,将元素分成两部分,即大于该元素的放在其后面,小于该元素的放在其前面,这里有点类似于快排

import numpy as np
np.random.seed(100)
x = np.random.randint(1, 30, [4, 3])
print(x)
#[[ 9 25  4]
# [ 8 24 16]
# [17 11 21]
# [ 3 22  3]]
y = np.sort(x, axis=0)
print(y)
#[[ 3 11  3]
# [ 8 22  4]
# [ 9 24 16]
# [17 25 21]]
z = np.partition(x, kth=3, axis=0)
print(z)
#[[ 3 11  3]
# [ 8 22  4]
# [ 9 24 16]
# [17 25 21]]

2 搜索

一些用于在数组内搜索的函数。 提供了用于找到最大值,最小值以及满足给定条件的元素的函数。

numpy.argmax(a[, axis=None, out=None]) 返回最大值的小标
 numpy.argmin(a[, axis=None, out=None]) 返回最小值的下标

a : 输入数组
axis:none、0、1、2…
默认值:数组展平。否则按照指定的axis方向。如axis=0。
out : 输出结果
返回值:下标组成的数组,返回axis轴上最小值的索引构成的矩阵。
其shape与输入数组a去掉axis维度的shape相同。

import numpy as np
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.argmin(x)
print(y)
#10
y = np.argmin(x,axis=0)
print(y)
#[2 4 2 0 3]
 numppy.nonzero(a) 其值为非零元素的下标在对应轴上的值
  1. 只有 a 中非零元素才会有索引值,那些零值元素没有索引值。
  2. 返回一个长度为 a.ndim 的元组(tuple),元组的每个元素都是一个整数数组(array)。
  3. 每一个array均是从一个维度上来描述其索引值。比如,如果 a 是一个二维数组,则tuple包含两个array,第一个array从行维度来描述索引
    值;第二个array从列维度来描述索引值。
  4. 该 np.transpose(np.nonzero(x)) 函数能够描述出每一个非零元素在不同维度的索引值。
  5. 通过 a[nonzero(a)] 得到所有 a 中的非零值
import numpy as np
x = np.array([[3, 0, 0], [0, 4, 0], [5, 6, 0]])
print(x.shape) # (3, 3)
print(x.ndim) # 2
y = np.nonzero(x)
print(y)
# (array([0, 1, 2, 2], dtype=int64), array([0, 1, 0, 1], dtype=int64))
print(np.array(y))
# [[0 1 2 2]
# [0 1 0 1]]
print(np.array(y).shape) # (2, 4)
print(np.array(y).ndim) # 2
y = x[np.nonzero(x)]
print(y) # [3 4 5 6]
y = np.transpose(np.nonzero(x))
print(y)
# [[0 0]
# [1 1]
# [2 0]
# [2 1]]
	numpy.where(condition, [x=None, y=None])
	满足条件 condition输出 x ,不满足输出 y 
import numpy as np
x=np.array([[0, 1, 2],
           [0, 2, 4],
           [0, 3, 6]])
y = np.where(x < 4, x, -1)
print(y)
# [[ 0  1  2]
# [ 0  2 -1]
# [ 0  3 -1]]

只有 condition ,没有 x 和 y ,则输出满足条件 (即非0) 元素的坐标 (等价于 numpy.nonzero )。这里的坐标以tuple的形式给出,通常原数组有多少维,输出的tuple中就包含几个数组,分别对应符合条件元素的各维坐标。

import numpy as np
x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
y = np.where(x > 5)
print(y)
# (array([5, 6, 7], dtype=int64),)
print(x[y])
# [6 7 8]
y = np.nonzero(x > 5)
print(y)
# (array([5, 6, 7], dtype=int64),)
print(x[y])
# [6 7 8]
numpy.searchsorted(a, v[, side='left', sorter=None]) 

a. a:一维输入数组。当 sorter 参数为 None 的时候, a 必须为升序数组;否则, sorter 不能为空,存放 a 中元素的 index ,用于反映 a 数组的升序排列方式。
b. v:插入 a 数组的值,可以为单个元素, list 或者 ndarray
c. side:查询方向,当为 left 时,将返回第一个符合条件的元素下标;当为 right 时,将返回最后一个符合条件的元素下标。
d. sorter:一维数组存放 a 数组元素的 index,index 对应元素为升序。

import numpy as np
x = np.array([0, 1, 3, 5, 9, 11, 18])
y = np.searchsorted(x, 15)
print(y) # 5
y = np.searchsorted(x, 15, side='right')
print(y) # 5
y = np.searchsorted(x, 15, side='left')
print(y) # 5
import numpy as np
x = np.array([0, 1, 5, 9, 11, 18, 26, 33])
y = np.searchsorted(x, [-1, 0, 11, 15, 33, 35])
print(y) # [0 0 4 5 7 8]
y = np.searchsorted(x, [-1, 0, 11, 15, 33, 35], side='right')
print(y) # [0 1 5 5 8 8]

3 计数

numpy.count_nonzero(a, axis=None)返回数组中的非0元素个数
import numpy as np
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]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值