Numpy学习总结二

    技术2022-07-10  124

    1.数组运算

    加减乘除

    import numpy as np x =np.arange(4) print("x =",x) print("x+5 =",x+5) print("x-5 =",x-5) print("x*2 =",x*2) print("x/2 =",x/2) print("x//2=",x//2) #向下整除运算 x = [0 1 2 3] x+5 = [5 6 7 8] x-5 = [-5 -4 -3 -2] x*2 = [0 2 4 6] x/2 = [0. 0.5 1. 1.5] x//2= [0 0 1 1]

    负数、指数、模运算

    print("-x =", -x) print("x ** 2 =", x**2) print("x % 2 =", x%2) -x = [ 0 -1 -2 -3] x ** 2 = [0 1 4 9] x % 2 = [0 1 0 1]

    运算数算符(array,数字)

    np.add : 加法运算 np.subtract: 减法运算 np.negative : 负数运算 np.multiply : 乘法运算 np.divide: 除法运算 np.floor_divide :向下整除运算(3//2=1) np.power:指数运算 np.mod:模/余数(9%4=1)

    2.绝对值abs(),np.absolute()

    x=np.array([-2,-3,-5,1,5,6]) abs(x) array([2, 3, 5, 1, 5, 6]) np.absolute(x) array([2, 3, 5, 1, 5, 6])

    3.三角函数

    np.pi是π 定义一个角度数组

    theta=np.linspace(0,np.pi,3) print("theta =",theta) print("sin(theta) =",np.sin(theta)) print("cos(theta) =",np.cos(theta)) print("tan(theta) =",np.tan(theta)) theta = [0. 1.57079633 3.14159265] sin(theta) = [0.0000000e+00 1.0000000e+00 1.2246468e-16] cos(theta) = [ 1.000000e+00 6.123234e-17 -1.000000e+00] tan(theta) = [ 0.00000000e+00 1.63312394e+16 -1.22464680e-16]

    逆三角函数

    x=[-1,0,1] print("x =",x) print("arcsin(x) =",np.arcsin(x)) print("arccos(x) =",np.arccos(x)) print("arctan(x) =",np.arctan(x)) x = [-1, 0, 1] arcsin(x) = [-1.57079633 0. 1.57079633] arccos(x) = [3.14159265 1.57079633 0. ] arctan(x) = [-0.78539816 0. 0.78539816]

    4.指数运算

    x=[1,2,3,4] print("x =",x) print("x^2 =",np.power(x,2)) print("e^x =",np.exp(x)) print("2^x =",np.exp2(x)) print("3^x =",np.power(3,x)) x = [1, 2, 3, 4] x^2 = [ 1 4 9 16] e^x = [ 2.71828183 7.3890561 20.08553692 54.59815003] 2^x = [ 2. 4. 8. 16.] 3^x = [ 3 9 27 81]

    5.高级的通用函数特性

    指定输出:通过out( )将计算结果直接写到期望的存储位置

    x = np.arange(5) y = np.empty(5) np.multiply(x,10,out=y) y array([ 0., 10., 20., 30., 40.]) y = np.zeros(10) np.power(2,x,out=y[::2]) y array([ 1., 0., 2., 0., 4., 0., 8., 0., 16., 0.])

    聚合 使用reduce(),求所有元素和

    x=np.arange(1,6) np.add.reduce(x) 15

    使用reduce(),求所有元素的乘积

    np.multiply.reduce(x) 120

    存储每次计算的中间结果,使用accumlate

    np.add.accumulate(x) array([ 1, 3, 6, 10, 15], dtype=int32) np.multiply.accumulate(x) array([ 1, 2, 6, 24, 120], dtype=int32)

    数组值求和

    L = np.random.random(100) L array([0.49136078, 0.01839103, 0.91991384, 0.13177381, 0.2600643 , 0.02923518, 0.77325755, 0.49521021, 0.33113434, 0.45574834, 0.47412162, 0.00349015, 0.49932288, 0.44589546, 0.93984767, 0.99619881, 0.52594216, 0.93943259, 0.44115789, 0.7313022 , 0.2776895 , 0.13577211, 0.43562387, 0.19791174, 0.47942354, 0.52009533, 0.97920912, 0.71076579, 0.64008585, 0.16527465, 0.69195523, 0.25584534, 0.7037558 , 0.30470035, 0.71743962, 0.90586747, 0.60296546, 0.681635 , 0.9045025 , 0.7556861 , 0.93369632, 0.6550494 , 0.41460599, 0.97717921, 0.6634018 , 0.56156024, 0.67212571, 0.43282254, 0.5231683 , 0.125154 , 0.45976559, 0.32560204, 0.73702699, 0.49125856, 0.50577858, 0.64917083, 0.77767602, 0.04877991, 0.07148761, 0.24692516, 0.50769548, 0.43002011, 0.23512407, 0.88430909, 0.13330183, 0.45815716, 0.61568217, 0.33277298, 0.86144233, 0.04992675, 0.63823345, 0.25705266, 0.98923296, 0.35974098, 0.03186842, 0.91909251, 0.31016726, 0.55669283, 0.02614912, 0.44098185, 0.04383869, 0.92567677, 0.84858238, 0.32614017, 0.92564057, 0.66674633, 0.73754385, 0.163653 , 0.67709575, 0.34734872, 0.54384472, 0.50135868, 0.10911624, 0.75514575, 0.7312519 , 0.02659235, 0.90753253, 0.34714445, 0.36755756, 0.38999918]) #python中求和,sum() sum(L) 50.62069360714714 #Numpy中求和,np.sum( ) np.sum(L) 50.62069360714715

    最大值和最小值 np.min( )和np.max( )

    w = np.random.random(1000) np.min(L),np.max(L) (0.003490149101228579, 0.9961988098161547) R = np.random.random((3,4)) R array([[0.97113853, 0.65904247, 0.41905622, 0.66157014], [0.69910756, 0.33874723, 0.70891679, 0.12181356], [0.92407905, 0.657486 , 0.45301737, 0.08525889]]) R.min(axis=0) array([0.69910756, 0.33874723, 0.41905622, 0.08525889]) R.max(axis=1) array([0.97113853, 0.70891679, 0.92407905])

    函数汇总 | np.sum | 元素求和 |np.prod | 元素求积 |np.mean | 元素平均值 |np.std | 元素标准差 |np.var | 元素方差 | np.argmin | 找出最小值索引 |np.argmax| 找出最大值索引 |np.median | 计算元素中位数 |np.percentile | 计算基于元素排序的统计值 | np.any | 验证是否存在元素为真 |np.all | 验证所有元素是否为真

    import numpy as np H = np.array([189,170,189,163,183,171,185,168,173,183,173,173,175,178,183,193,178,173,174,183,183,168,170,178,182,180,183,178,182,188,175,179,183,193,182,183,177,185,188,182,185]) H array([189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 168, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177, 185, 188, 182, 185]) np.percentile(H,25) #求取第25%分位的数值 174.0 np.percentile(H,75) #求取第75%分位的数值 183.0

    6.比较

    ‘==’ :np.equal ‘!’: np.not_equal ‘<’ : np.less ‘<=’: np.less_equal ‘>’: np.greater ‘>=’ : np.greaterequal

    x = np.array([1,2,3,4,5,6]) x < 3 array([ True, True, False, False, False, False]) np.less(x,3) array([ True, True, False, False, False, False]) #二维数组比较 m = np.random.RandomState(0) x = m.randint(10,size=(3,4)) x array([[5, 0, 3, 3], [7, 9, 3, 5], [2, 4, 7, 6]]) x <6 array([[ True, True, True, True], [False, False, True, True], [ True, True, False, False]])

    统计记录个数:np.count_nonzero

    x array([[5, 0, 3, 3], [7, 9, 3, 5], [2, 4, 7, 6]]) #有多少个值小于6 np.count_nonzero(x<6) 8 #每行有多少至小于6 np.sum(x<6,axis=1) #第一行有四个小于6 #第二行有2个 #第三行有2个 array([4, 2, 2])

    判断所有的值是否为True:np.any,np.all

    #有没有值大于8 np.any(x>8) True #有没有值小于0 np.any(x<0) False

    7.花哨的索引

    花哨的索引与简单索引值类似,但是传递的是索引数组,而不是单个标量

    rand = np.random.RandomState(42) x = rand.randint(100,size=10) x array([51, 92, 14, 71, 60, 20, 82, 86, 74, 74])

    同时获得三个不同的元素

    #方法一 [x[3],x[7],x[2]] [71, 86, 14] #方法二:通过传递索引的单个列表或数组来获得 ind = [3,7,4] x[ind] array([71, 86, 60])

    利用花哨的索引,结果的形状与索引数组的形状一致,而不是与被索引数组的形状一致

    ind = np.array([[3,7],[4,5]])#结果的形状与ind数组形状一致 x[ind] array([[71, 86], [60, 20]])

    多维度索引 与标准的索引一样,第一个数是行,第二个数是列

    x = np.arange(12).reshape((3,4)) x array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) row = np.array([0,1,2]) col = np.array([2,1,3]) x[row,col] #结果的第一个值是x[0,2],第二个值是[1,1],第三个值是[2,3] array([ 2, 5, 11])

    组合索引

    x=np.arange(0,12) y=x.reshape(3,4) y array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) #花哨索引和简单索引组合 y[2,[2,0,1]] #前面为行,后面为例,即索引为2的行是第3行,[2,0,1]分别是第第三列,第一列,第二例 array([10, 8, 9]) #花哨索引和切片组合 y[1:,[2,0,1]] #1:,指第二行以后 array([[ 6, 4, 5], [10, 8, 9]])

    用花哨的索引修改值

    x = np.arange(10) i = np.array([2,1,8,4])#只修改索引为2,1,8,4的地方 x[i]=99 array([ 0, 99, 99, 3, 99, 5, 6, 7, 99, 9]) x[i] -= 10 array([ 0, 89, 89, 3, 89, 5, 6, 7, 89, 9]) x = np.zeros(10) x[[0,0]] = [4,6]#该操作先幅值x[0] = 4,然后幅值x[0] = 6,所以最后是最后值为6。其中[0,0]代表第一行第一列 x array([6., 0., 0., 0., 0., 0., 0., 0., 0., 0.])

    8.数组排序

    快速排序:np.sort和np.argsort

    x = np.array([2,1,4,3,5]) np.sort(x) array([1, 2, 3, 4, 5])

    argsort是返回索引值

    np.argsort(x) array([1, 0, 3, 2, 4], dtype=int64)

    沿着行或列排序

    R =np.random.RandomState(42) T =R.randint(0,10,(4,6)) T array([[6, 3, 7, 4, 6, 9], [2, 6, 7, 4, 3, 7], [7, 2, 5, 4, 1, 7], [5, 1, 4, 0, 9, 5]]) #对T的每一个列排序 np.sort(T,axis=0) array([[2, 1, 4, 0, 1, 5], [5, 2, 5, 4, 3, 7], [6, 3, 7, 4, 6, 7], [7, 6, 7, 4, 9, 9]]) #对T的,每一个行排序 np.sort(T,axis=1) array([[3, 4, 6, 6, 7, 9], [2, 3, 4, 6, 7, 7], [1, 2, 4, 5, 7, 7], [0, 1, 4, 5, 5, 9]])

    部分排序:分割 找到数组中第K小的值,使用np.partition函数,输入数组和数字K,输出是一个新数组,最左边是第K小的值,往右是任意顺序的其他值

    x = np.array([7,2,3,1,6,5,4]) np.partition(x,3)#3代表第3小的元素 array([2, 1, 3, 4, 6, 5, 7])

    沿着多维数组任意的轴进行分割

    np.partition(T,2,axis=1)#沿行排序,2代表第2小的元素 array([[3, 4, 6, 7, 6, 9], [2, 3, 4, 7, 6, 7], [1, 2, 4, 5, 7, 7], [0, 1, 4, 5, 9, 5]])
    Processed: 0.015, SQL: 9