import numpy as np
np.random.seed(100) # 多次运行得到相同的结果,设置随机数的种子
x = np.random.random(50)
x
np.min(x) # x的最小值
np.argmin(x) # x的最小值的索引
x[4] # x的第4位的索引值
np.max(x) # x的最大值
np.argmax(x) # x的最大值的索引
x[36] # x的第36位的索引值
ind = np.argwhere(x > 0.5) # x>0.5的索引
ind
x[ind] # x的索引对应的值
x = np.arange(10)
x
np.random.shuffle(x) # 乱序
x
np.sort(x) # 排序
ind = np.argsort(x) # 按索引排序
ind
x[ind] # x的索引对应的值
ind[:3] # 索引的切片,第0到第3,不包括第3
x[ind[:3]] # 按索引的切片取值,第0到第3,不包括第3
x[ind[3:]] # 按索引的切片取值,第3到最后
x[ind[-3:]] # 按索引的切片取值,最后3个
np.partition(x, 4) # 大于4的x排在前面,小于4的排在后面
np.argpartition(x, 4) # 索引对应的值大于4的x排在前面,小于4的排在后面
二维
X = np.random.randint(20, size=(4, 5)) # 20以内的随机数20个,分成4行5列
X
np.sort(X) # 按每行大小排序
np.argsort(X) # 按每行索引对应值大小排序
np.sort(X, axis=0) # 按每列大小排序
np.argsort(X, axis=0) # 按每列索引对应值大小排序
注:代码来自《Python全栈工程师特训班》课程