• numpy


    1. import numpy as np
    1. np.arange(10)
    array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
    
    1. # 对列表中的元素开平方
    2. b = [3, 4, 9]
    3. np.sqrt(b)
    array([1.73205081, 2.        , 3.        ])
    
    1. # 使用array函数创建一维数组
    2. a = np.array([1,2,3,4])
    3. a
    array([1, 2, 3, 4])
    
    1. # 使用array创建二维数组
    2. b = np.array([[1,2,3],[3,4,6]])
    3. b
    array([[1, 2, 3],
           [3, 4, 6]])
    
    1. ## 使用dtype参数来设置数组的类型
    2. c = np.array([3,4,5], dtype=float)
    3. c
    array([3., 4., 5.])
    
    1. ## 使用ndim参数来设置数组的维度
    2. d = np.array([3,4,6], dtype=float,ndmin=3)
    3. d
    array([[[3., 4., 6.]]])
    

    使用arange创建数组

    1. e = np.arange(1,11)
    2. e
    array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
    
    1. ## 设置步长
    2. np.arange(1,11,2)
    array([1, 3, 5, 7, 9])
    
    1. ## 设置dtype
    2. np.arange(1,11,2,dtype=float)
    array([1., 3., 5., 7., 9.])
    

    随机数

    1. ## random函数创建0.0-1.0之间的随机数数组,注意不包括号1.0
    2. np.random.random(size=5) ### 一维数组
    array([0.14109248, 0.57757689, 0.31587811, 0.90959059, 0.25585209])
    
    1. ## 创建二维数组
    2. np.random.random(size=(3,4)) ### 3,4 表示3行吧列
    array([[0.27963513, 0.28511459, 0.63027425, 0.99946311],
           [0.14654823, 0.62364526, 0.81366614, 0.88192038],
           [0.91244454, 0.4028762 , 0.11921253, 0.54991022]])
    
    1. ### 创建三维数组
    2. np.random.random(size=(2,3,4)) ### 2,3,4表示创建2个3行4列
    array([[[0.27409587, 0.11804355, 0.34964509, 0.93155881],
            [0.32850319, 0.42947898, 0.21363423, 0.94016219],
            [0.66905712, 0.72171425, 0.23520955, 0.4893854 ]],
    
           [[0.17257756, 0.99171624, 0.31052962, 0.61989267],
            [0.84792176, 0.79669383, 0.92678657, 0.90099817],
            [0.35242589, 0.95967321, 0.00670096, 0.91882932]]])
    

    随机整数

    1. np.random.randint(6,size=10) # 生成0到5之间的10个随机整数
    array([0, 0, 1, 4, 1, 4, 2, 5, 3, 3])
    
    1. ## 生成5-10之间的随机整数的二维数组
    2. np.random.randint(5,11,size=(4,3))
    array([[ 9,  7, 10],
           [10,  7,  5],
           [ 8,  8,  7],
           [ 7,  7, 10]])
    
    1. ## 生成三维数组
    2. np.random.randint(5,11,size=(2,4,3))
    array([[[ 7,  8,  5],
            [ 7,  5,  6],
            [ 8,  8,  8],
            [ 8,  5, 10]],
    
           [[ 7, 10,  6],
            [ 6,  8,  8],
            [ 9,  6,  6],
            [ 5,  8,  6]]])
    
    1. np.random.randint(5,11,size=(1,3,4))
    array([[[ 7, 10,  5, 10],
            [ 9, 10,  5, 10],
            [ 6,  9, 10,  9]]])
    

    randint中参数dtype的使用

    1. d = np.random.randint(10,size=5)
    1. d.dtype
    dtype('int32')
    
    1. f = np.random.randint(10,size=5,dtype=np.int64)
    1. f.dtype
    dtype('int64')
    

    正态分布

    1. ## 一维
    2. np.random.randn(4)
    array([-0.65738946, -1.36270863, -0.5901353 ,  0.63707697])
    
    1. ### 二维
    2. np.random.randn(2,3)
    array([[-0.86151919, -1.35400762,  0.60027059],
           [-0.44439982, -0.69328396,  1.15670548]])
    
    1. ### 三维
    2. np.random.randn(2,3,4)
    array([[[ 0.21530965,  1.419034  ,  1.2442361 , -1.47677229],
            [ 0.41533172, -0.8700582 ,  1.28418702,  0.77605165],
            [-1.63712476, -0.24687974,  1.38743355, -0.13334918]],
    
           [[-0.70745225, -0.73391537, -1.29003062, -0.96750294],
            [-0.48278997, -0.3381754 ,  2.30733888, -1.00871624],
            [-0.88437762,  0.68254093,  0.90741469, -0.70638608]]])
    

    以上是标准的正态分布:期望为0,方差为1

    创建指定期望和方差值的正态分布

    1. np.random.normal(size=5) ### 默认的期望是loc=0.0,方差是scale=1.0
    array([ 0.37960069,  0.77483781,  0.23537764,  0.97151935, -0.08167272])
    
    1. ### 指定loc和scale
    2. np.random.normal(loc=2,scale=3,size=5)
    array([ 5.49844885,  6.50644569, -2.05466087,  2.53766272,  2.40695409])
    
    1. np.random.normal(loc=2,scale=3,size=(3,4))
    array([[ 6.95415439,  2.37018031, -2.9699421 , -1.12177319],
           [ 2.69676034,  0.30211155,  4.04696743, -1.63856255],
           [ 2.90005922, -3.83970821,  3.07125062,  3.78305087]])
    

    ndarray对象属性

    1. ### 分别创建一维,二维,三维数组
    2. a1 = np.array([1,2,3,4,5])
    3. b1 = np.random.randint(4,10,size=(3,4))
    4. c1 = np.random.randn(2,3,4)
    1. a1
    array([1, 2, 3, 4, 5])
    
    1. b1
    array([[6, 9, 7, 6],
           [7, 5, 7, 5],
           [9, 8, 7, 7]])
    
    1. c1
    array([[[-1.70317407,  0.57392171, -0.00487497, -0.32478167],
            [ 0.69572239, -0.34328439, -1.31930338, -0.26807493],
            [ 1.22140779, -0.49691301,  0.69210554,  0.14662866]],
    
           [[ 0.083173  , -1.14836617,  1.5909447 ,  0.55987063],
            [-0.59362023, -0.73479667,  0.78516186, -0.46653616],
            [-0.02626701, -1.49199613,  1.2136789 ,  2.01738911]]])
    
    ndim属性
    1. print(a1.ndim,b1.ndim,c1.ndim)
    1 2 3
    
    shape属性
    1. print(a1.shape,b1.shape,c1.shape)
    (5,) (3, 4) (2, 3, 4)
    
    dtype属性
    1. print(a1.dtype,b1.dtype,c1.dtype)
    int32 int32 float64
    
    size属性,元素的总个数
    1. print(a1.size,b1.size,c1.size)
    5 12 24
    
    itemsize 每个元素所占的字节
    1. print(a1.itemsize,b1.itemsize,c1.itemsize)
    4 4 8
    

    zeros创建数组

    1. np.zeros(5)
    array([0., 0., 0., 0., 0.])
    
    1. np.zeros(5,dtype=int)
    array([0, 0, 0, 0, 0])
    
    1. np.zeros((3,4))
    array([[0., 0., 0., 0.],
           [0., 0., 0., 0.],
           [0., 0., 0., 0.]])
    

    ones创建数组

    1. np.ones(10)
    array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
    
    1. np.ones((2,5),dtype=int)
    array([[1, 1, 1, 1, 1],
           [1, 1, 1, 1, 1]])
    

    empty

    1. np.empty(8)
    array([0.0000000e+000, 0.0000000e+000, 0.0000000e+000, 0.0000000e+000,
           0.0000000e+000, 7.3516968e-321, 1.3796137e-306, 0.0000000e+000])
    
    1. np.empty((3,4))
    array([[0., 0., 0., 0.],
           [0., 0., 0., 0.],
           [0., 0., 0., 0.]])
    

    linspace 创建等差数列

    1. np.linspace(1,10,10)
    array([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])
    
    1. np.linspace(5,20,5)
    array([ 5.  ,  8.75, 12.5 , 16.25, 20.  ])
    
    1. np.linspace(5,20,5,endpoint=False) #### endpoint默认为True
    array([ 5.,  8., 11., 14., 17.])
    

    logspace创建等比数列

    1. np.logspace(0,9,10,base=2) ### base默认为空0。0
    array([  1.,   2.,   4.,   8.,  16.,  32.,  64., 128., 256., 512.])
    

    numpy 索引与切片

    1. d1 = np.arange(10)
    1. d1
    array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
    
    1. d1[0]
    0
    
    1. d1[-1]
    9
    
    1. ### 切片[start:stop:step]
    2. d1[:]
    array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
    
    1. d1[3:]
    array([3, 4, 5, 6, 7, 8, 9])
    
    1. d1[3:5]
    array([3, 4])
    
    1. d1[1:7:2]
    array([1, 3, 5])
    
    1. d1[::-1] # -1表示反向获取
    array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
    
    1. d1[-5:-2]
    array([5, 6, 7])
    
    1. d1[-7:-2:2]
    array([3, 5, 7])
    

    二维数组的切片

    1. x = np.arange(1,13)
    1. x
    array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])
    
    1. ### 对一维数组修改形状
    2. x1 = x.reshape(4,3)
    1. x1
    array([[ 1,  2,  3],
           [ 4,  5,  6],
           [ 7,  8,  9],
           [10, 11, 12]])
    
    1. x1[1]
    array([4, 5, 6])
    
    1. x1[1][2]
    6
    
    1. ### 二维数组切片【行进行切片,列进行切片】
    2. x1[:,:]
    array([[ 1,  2,  3],
           [ 4,  5,  6],
           [ 7,  8,  9],
           [10, 11, 12]])
    
    1. x1[:,1]
    array([ 2,  5,  8, 11])
    
    1. x1[:,0:2] ### 所有行第一列第二列
    array([[ 1,  2],
           [ 4,  5],
           [ 7,  8],
           [10, 11]])
    
    1. x1[::2,:] #### 奇数行所有列
    array([[1, 2, 3],
           [7, 8, 9]])
    
    1. x1[::2,0:2] ## 奇数行,第一,二列
    array([[1, 2],
           [7, 8]])
    

    坐标获取

    1. x1[1,2]
    6
    
    1. x1[(1,2),(2,0)]
    array([6, 7])
    
    1. x1[-1]
    array([10, 11, 12])
    
    1. x1[::-1]
    array([[10, 11, 12],
           [ 7,  8,  9],
           [ 4,  5,  6],
           [ 1,  2,  3]])
    
    1. x1[::-1,::-1]
    array([[12, 11, 10],
           [ 9,  8,  7],
           [ 6,  5,  4],
           [ 3,  2,  1]])
    

    数组的复制

    1. sub_a = x1[:2,:2]
    1. sub_a
    array([[1, 2],
           [4, 5]])
    
    1. sub_a[0][0] =100
    1. sub_a
    array([[100,   2],
           [  4,   5]])
    
    1. x1
    array([[100,   2,   3],
           [  4,   5,   6],
           [  7,   8,   9],
           [ 10,  11,  12]])
    
    1. ### 通过切片可以获取到新的数组,即使赋值给新的变量,但是还是原来数组的视图,如果对切片数组中元素的值进行修改,原数组中也会改变
    2. ### numpy中的copy实现了深 考贝
    3. sub = np.copy(x1[:2,:2])
    1. sub
    array([[100,   2],
           [  4,   5]])
    
    1. sub[0][1] = 21
    1. sub
    array([[100,  21],
           [  4,   5]])
    
    1. x1
    array([[100,   2,   3],
           [  4,   5,   6],
           [  7,   8,   9],
           [ 10,  11,  12]])
    
    改变数组的维度
    1. w = np.arange(1,25)
    1. w
    array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
           18, 19, 20, 21, 22, 23, 24])
    
    1. w.reshape(4,6) ###二维
    array([[ 1,  2,  3,  4,  5,  6],
           [ 7,  8,  9, 10, 11, 12],
           [13, 14, 15, 16, 17, 18],
           [19, 20, 21, 22, 23, 24]])
    
    1. w.reshape((3,8))
    array([[ 1,  2,  3,  4,  5,  6,  7,  8],
           [ 9, 10, 11, 12, 13, 14, 15, 16],
           [17, 18, 19, 20, 21, 22, 23, 24]])
    
    1. ### 三维
    2. w.reshape((2,3,4))
    array([[[ 1,  2,  3,  4],
            [ 5,  6,  7,  8],
            [ 9, 10, 11, 12]],
    
           [[13, 14, 15, 16],
            [17, 18, 19, 20],
            [21, 22, 23, 24]]])
    
    1. np.reshape(w,(3,8))
    array([[ 1,  2,  3,  4,  5,  6,  7,  8],
           [ 9, 10, 11, 12, 13, 14, 15, 16],
           [17, 18, 19, 20, 21, 22, 23, 24]])
    
    1. np.reshape(w,(4,3,2))
    array([[[ 1,  2],
            [ 3,  4],
            [ 5,  6]],
    
           [[ 7,  8],
            [ 9, 10],
            [11, 12]],
    
           [[13, 14],
            [15, 16],
            [17, 18]],
    
           [[19, 20],
            [21, 22],
            [23, 24]]])
    
    将多维修改成一维数组
    1. bb = np.reshape(w,(2,4,3))
    1. bb
    array([[[ 1,  2,  3],
            [ 4,  5,  6],
            [ 7,  8,  9],
            [10, 11, 12]],
    
           [[13, 14, 15],
            [16, 17, 18],
            [19, 20, 21],
            [22, 23, 24]]])
    
    1. bb.reshape(24)
    array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
           18, 19, 20, 21, 22, 23, 24])
    
    1. bb.reshape(-1)
    array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
           18, 19, 20, 21, 22, 23, 24])
    
    1. bb.ravel()
    array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
           18, 19, 20, 21, 22, 23, 24])
    
    1. bb.flatten()
    array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
           18, 19, 20, 21, 22, 23, 24])
    

    数组的拼接

    1. ### 创建两个二维数组
    2. h1 = np.array([[1,2,3],[4,5,6]])
    3. h2 = np.array([[11,12,13],[14,15,16]])
    1. h1
    array([[1, 2, 3],
           [4, 5, 6]])
    
    1. h2
    array([[11, 12, 13],
           [14, 15, 16]])
    
    1. ### 小平拼接
    2. np.hstack([h1,h2])
    array([[ 1,  2,  3, 11, 12, 13],
           [ 4,  5,  6, 14, 15, 16]])
    
    1. np.hstack((h1,h2))
    array([[ 1,  2,  3, 11, 12, 13],
           [ 4,  5,  6, 14, 15, 16]])
    
    1. #### 垂直拼接
    2. np.vstack((h1,h2))
    array([[ 1,  2,  3],
           [ 4,  5,  6],
           [11, 12, 13],
           [14, 15, 16]])
    
    1. ### concatenate拼接
    2. np.concatenate((h1,h2),axis=0) ## axis=0是默认方向,垂直方向
    array([[ 1,  2,  3],
           [ 4,  5,  6],
           [11, 12, 13],
           [14, 15, 16]])
    
    1. np.concatenate((h1,h2))
    array([[ 1,  2,  3],
           [ 4,  5,  6],
           [11, 12, 13],
           [14, 15, 16]])
    
    1. np.concatenate((h1,h2),axis=1)
    array([[ 1,  2,  3, 11, 12, 13],
           [ 4,  5,  6, 14, 15, 16]])
    
    1. ### 三维数组 axis有0,1,2
    2. one = np.arange(1,13).reshape(1,2,6)
    3. two = np.arange(101,113).reshape(1,2,6)
    1. one
    array([[[ 1,  2,  3,  4,  5,  6],
            [ 7,  8,  9, 10, 11, 12]]])
    
    1. two
    array([[[101, 102, 103, 104, 105, 106],
            [107, 108, 109, 110, 111, 112]]])
    
    1. np.concatenate((one,two),axis=0)
    array([[[  1,   2,   3,   4,   5,   6],
            [  7,   8,   9,  10,  11,  12]],
    
           [[101, 102, 103, 104, 105, 106],
            [107, 108, 109, 110, 111, 112]]])
    
    1. np.concatenate((one,two),axis=1)
    array([[[  1,   2,   3,   4,   5,   6],
            [  7,   8,   9,  10,  11,  12],
            [101, 102, 103, 104, 105, 106],
            [107, 108, 109, 110, 111, 112]]])
    
    1. np.concatenate((one,two),axis=2)
    array([[[  1,   2,   3,   4,   5,   6, 101, 102, 103, 104, 105, 106],
            [  7,   8,   9,  10,  11,  12, 107, 108, 109, 110, 111, 112]]])
    
    数组的分隔
    1. s = np.arange(1,13)
    1. s
    array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])
    
    1. ### 平均分隔
    2. np.split(s,4)
    [array([1, 2, 3]), array([4, 5, 6]), array([7, 8, 9]), array([10, 11, 12])]
    
    1. ### 按位置分隔
    2. np.split(s,(4,6))
    [array([1, 2, 3, 4]), array([5, 6]), array([ 7,  8,  9, 10, 11, 12])]
    
    1. #### 二维
    2. ss = np.array([[1,2,3,4],[5,6,7,8],[8,10,11,12],[13,14,15,16]])
    1. ss
    array([[ 1,  2,  3,  4],
           [ 5,  6,  7,  8],
           [ 8, 10, 11, 12],
           [13, 14, 15, 16]])
    
    1. np.split(ss,2,axis=0)
    [array([[1, 2, 3, 4],
            [5, 6, 7, 8]]), array([[ 8, 10, 11, 12],
            [13, 14, 15, 16]])]
    
    1. np.split(ss,(2,3),axis=0)
    [array([[1, 2, 3, 4],
            [5, 6, 7, 8]]), array([[ 8, 10, 11, 12]]), array([[13, 14, 15, 16]])]
    
    1. np.split(ss,2,axis=1)
    [array([[ 1,  2],
            [ 5,  6],
            [ 8, 10],
            [13, 14]]), array([[ 3,  4],
            [ 7,  8],
            [11, 12],
            [15, 16]])]
    
    1. np.split(ss,[3],axis=1)
    [array([[ 1,  2,  3],
            [ 5,  6,  7],
            [ 8, 10, 11],
            [13, 14, 15]]), array([[ 4],
            [ 8],
            [12],
            [16]])]
    
    1. ## hsplit()小平方向分隔
    2. np.hsplit(ss,2)
    [array([[ 1,  2],
            [ 5,  6],
            [ 8, 10],
            [13, 14]]), array([[ 3,  4],
            [ 7,  8],
            [11, 12],
            [15, 16]])]
    
    1. np.hsplit(ss,[3])
    [array([[ 1,  2,  3],
            [ 5,  6,  7],
            [ 8, 10, 11],
            [13, 14, 15]]), array([[ 4],
            [ 8],
            [12],
            [16]])]
    
    1. np.vsplit(ss,2) ### vsplit()垂直分隔
    [array([[1, 2, 3, 4],
            [5, 6, 7, 8]]), array([[ 8, 10, 11, 12],
            [13, 14, 15, 16]])]
    
    1. np.vsplit(ss,[1])
    [array([[1, 2, 3, 4]]), array([[ 5,  6,  7,  8],
            [ 8, 10, 11, 12],
            [13, 14, 15, 16]])]
    

    数组的转置

    1. ### 创建二维数组
    2. tr = np.arange(1,25).reshape(8,3)
    1. tr
    array([[ 1,  2,  3],
           [ 4,  5,  6],
           [ 7,  8,  9],
           [10, 11, 12],
           [13, 14, 15],
           [16, 17, 18],
           [19, 20, 21],
           [22, 23, 24]])
    
    1. ## 使用transpose a[j][i] --->a[i][j]
    2. tr.transpose()
    array([[ 1,  4,  7, 10, 13, 16, 19, 22],
           [ 2,  5,  8, 11, 14, 17, 20, 23],
           [ 3,  6,  9, 12, 15, 18, 21, 24]])
    
    1. tr.T
    array([[ 1,  4,  7, 10, 13, 16, 19, 22],
           [ 2,  5,  8, 11, 14, 17, 20, 23],
           [ 3,  6,  9, 12, 15, 18, 21, 24]])
    
    1. np.transpose(tr)
    array([[ 1,  4,  7, 10, 13, 16, 19, 22],
           [ 2,  5,  8, 11, 14, 17, 20, 23],
           [ 3,  6,  9, 12, 15, 18, 21, 24]])
    
    1. ## 多维数组
    2. trs = tr.reshape(2,3,4)
    1. trs
    array([[[ 1,  2,  3,  4],
            [ 5,  6,  7,  8],
            [ 9, 10, 11, 12]],
    
           [[13, 14, 15, 16],
            [17, 18, 19, 20],
            [21, 22, 23, 24]]])
    
    1. np.transpose(trs) ## 默认是从a[i][j][k] ---->a[k][j][i]
    array([[[ 1, 13],
            [ 5, 17],
            [ 9, 21]],
    
           [[ 2, 14],
            [ 6, 18],
            [10, 22]],
    
           [[ 3, 15],
            [ 7, 19],
            [11, 23]],
    
           [[ 4, 16],
            [ 8, 20],
            [12, 24]]])
    
    1. np.transpose(trs,(1,2,0)) ### 这里的1,2,0是数组维度的下标
    array([[[ 1, 13],
            [ 2, 14],
            [ 3, 15],
            [ 4, 16]],
    
           [[ 5, 17],
            [ 6, 18],
            [ 7, 19],
            [ 8, 20]],
    
           [[ 9, 21],
            [10, 22],
            [11, 23],
            [12, 24]]])
    

    函数

    1. ###创建一个二维数组
    2. v2 = np.arange(9).reshape(3,3)
    1. v2
    array([[0, 1, 2],
           [3, 4, 5],
           [6, 7, 8]])
    
    1. ### 创建一个一维数组
    2. v1 = np.array([10,10,10])
    1. v1
    array([10, 10, 10])
    
    1. ### 算术函数
    2. np.add(v1,v2)
    array([[10, 11, 12],
           [13, 14, 15],
           [16, 17, 18]])
    
    1. v1+v2
    array([[10, 11, 12],
           [13, 14, 15],
           [16, 17, 18]])
    
    1. np.subtract(v1,v2)
    array([[10,  9,  8],
           [ 7,  6,  5],
           [ 4,  3,  2]])
    
    1. ### out参数的使用
    2. y = np.empty((3,3),dtype=np.int)
    3. np.multiply(v2,10,out=y)
    array([[ 0, 10, 20],
           [30, 40, 50],
           [60, 70, 80]])
    
    1. ### 三角函数
    2. np.sin(np.array([0,30,60,90]))
    array([ 0.        , -0.98803162, -0.30481062,  0.89399666])
    
    1. ### 向上取整
    2. f = np.array([1.0,4.55,123,0.567,25.33])
    3. np.around(f)
    array([  1.,   5., 123.,   1.,  25.])
    
    1. np.ceil(f)
    array([  1.,   5., 123.,   1.,  26.])
    
    1. ## 向下取整
    2. np.floor(f)
    array([  1.,   4., 123.,   0.,  25.])
    

    统计函数

    1. ### power()幂次方
    2. t = np.arange(1,13).reshape(3,4)
    1. t
    array([[ 1,  2,  3,  4],
           [ 5,  6,  7,  8],
           [ 9, 10, 11, 12]])
    
    1. np.power(t,2)
    array([[  1,   4,   9,  16],
           [ 25,  36,  49,  64],
           [ 81, 100, 121, 144]], dtype=int32)
    
    1. ## power()中参数out的使用
    2. x = np.arange(5)
    3. y = np.zeros(10)
    1. np.power(2,x,out=y[:5])
    array([ 1.,  2.,  4.,  8., 16.])
    
    1. y
    array([ 1.,  2.,  4.,  8., 16.,  0.,  0.,  0.,  0.,  0.])
    

    求中位数

    1. z = np.array([4,3,2,5,2,1])
    1. np.median(z) ### 对数组排序 [1,2,2,3,4,5] 数组中元素个数为偶数 中位数指:中间两个数的平均值
    2.5
    
    1. np.median(np.array([4,3,2,5,6])) # 对数组排序 [2,2,3,4,5] 数组中元素个数为奇数 中位数指:中间的数
    4.0
    
    1. ### 二维数组
    2. z2 = np.arange(1,13).reshape(3,4)
    1. z2
    array([[ 1,  2,  3,  4],
           [ 5,  6,  7,  8],
           [ 9, 10, 11, 12]])
    
    1. np.median(z2,axis=0)
    array([5., 6., 7., 8.])
    
    1. np.median(z2,axis=1)
    array([ 2.5,  6.5, 10.5])
    

    mean求平均值

    1. np.mean(z)
    2.8333333333333335
    
    1. np.mean(z2)
    6.5
    
    1. np.mean(z2,axis=0)
    array([5., 6., 7., 8.])
    
    1. np.mean(z2,axis=1)
    array([ 2.5,  6.5, 10.5])
    
    1. #sum()
    2. np.max(z)
    5
    
    1. np.sum(z)
    17
    
    1. np.min(z)
    1
    
    1. np.argmax(z) ### 返回最大值为的下标
    3
    
    1. np.argmin(z)
    5
    




  • 相关阅读:
    组装query,query汇总,query字段
    POJ 1276, Cash Machine
    POJ 1129, Channel Allocation
    POJ 2531, Network Saboteur
    POJ 1837, Balance
    POJ 3278, Catch That Cow
    POJ 2676, Sudoku
    POJ 3126, Prime Path
    POJ 3414, Pots
    POJ 1426, Find The Multiple
  • 原文地址:https://www.cnblogs.com/baiyifengyun/p/14101768.html
Copyright © 2020-2023  润新知