numpy的聚合运算及其索引和排序
一:常用的聚合运算有
(1)求和sum
(2)最大值max
(3)最小值min
(4)平均值average
(5)中位数median
(6)prod,对所有的元素进行乘积
(7)percentile:百分比
(8)var:方差
(9)std:标准差
二:代码举例说明
import numpy as np L = np.random.random(1000000) print("L = ",L) print("sum = ",np.sum(L)) print("max = ",np.max(L)) print("min = ",np.min(L)) print("average = ",np.average(L)) print("median = ",np.median(L)) print("prod = ",np.prod(L)) print("percentile = ",np.percentile(L,50)) print("var = ",np.var(L)) print("std = ",np.std(L)) ''' L = [0.83190736 0.74081446 0.48812662 ... 0.27353513 0.90351821 0.99799109] sum = 499602.0936992654 max = 0.9999994792249605 min = 1.1307318972253455e-06 average = 0.4996020936992654 median = 0.49924280325258924 prod = 0.0 percentile = 0.49924280325258924 var = 0.0832567557942998 std = 0.2885424679216212 '''
三:索引
import numpy as np x = np.random.normal(0,1,size=1000000) print(x) print(np.min(x)) index_min = np.argmin(x) print(x[index_min]) print(np.max(x)) index_max = np.argmax(x) print(x[index_max])
四:排序及其索引
import numpy as np arr = np.arange(10) np.random.shuffle(arr) print(arr) print(np.sort(arr)) #从小到大排序,但是不改变原先的向量 print(np.argsort(arr))#打印的是从小到大的元素所在的下标
五:partition
import numpy as np arr = np.arange(10) np.random.shuffle(arr) print(arr)#[8 1 5 9 6 3 7 4 0 2] print(np.partition(arr,4))#[1 3 2 0 4 5 6 7 9 8]
类似于快速排序,4就是我们所选的基数,4之前的都比4小,4之后的都比4大。