美文网首页
numpy数组学习心得(1)

numpy数组学习心得(1)

作者: 不做废物 | 来源:发表于2018-08-01 20:12 被阅读0次

    numpy array数据结构学习心得(1)

    注,第一块 黑色区域为输入代码, 第二块黑色区域为代码的执行结果。。。以此类推

    numpy数组的创建

    • 从python列表(list)中创建数组
    • 从头创建数组
    import numpy as np
    # 从列表创建
    print(np.array([1,4,2,1,2]))
    np.array([range(i, i+3) for i in [3,6,9]])
    
    [1 4 2 1 2]
    
    array([[ 3,  4,  5],
           [ 6,  7,  8],
           [ 9, 10, 11]])
    
    # 从头创建数组
    np.zeros(10, dtype = int)  # 默认类型为浮点型,这里设置为整型
    
    array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    
    np.ones((2,3), dtype = float)
    
    array([[1., 1., 1.],
           [1., 1., 1.]])
    
    np.full((3,5), 66)  # 第一个参数设置 数组的 shape属性,第二个参数为用来填充的值
    
    array([[66, 66, 66, 66, 66],
           [66, 66, 66, 66, 66],
           [66, 66, 66, 66, 66]])
    
    np.arange(0,10,2)  # 类似内置函数 range的用法, 此处创建的是一维数组
    
    array([0, 2, 4, 6, 8])
    
    np.linspace(0,1,10)  # 类似matlab的linspace,创建10个数,均匀分布于0-1(含端点)
    
    array([0.        , 0.11111111, 0.22222222, 0.33333333, 0.44444444,
           0.55555556, 0.66666667, 0.77777778, 0.88888889, 1.        ])
    
    np.random.random((3,3))  # 创建shape为(3,3)的 0~1上的均匀分布随机数组
    
    array([[0.2732437 , 0.37765833, 0.40748153],
           [0.63105065, 0.73776397, 0.90088783],
           [0.54190688, 0.49405903, 0.93175355]])
    
    mu = 0
    sigma = 1
    np.random.normal(mu,sigma, (3,3)) # 创建正态分布随机数组,mu为均值,sigma为方差
    
    array([[ 0.95616699,  0.99284334,  0.16659225],
           [-0.02009492, -1.20051115, -0.26599785],
           [-0.33018642,  0.0495724 ,  0.6749862 ]])
    
    np.random.randint(0,10, (3,3))  # 创建[0,10) 区间上的整数均匀分布(术语可能并不很统计)随机数组
    
    array([[2, 1, 7],
           [0, 0, 3],
           [1, 3, 1]])
    
    x = 2
    np.eye(x) # 创建shape为x *x 的单位阵
    
    array([[1., 0.],
           [0., 1.]])
    
    np.empty(3)  # 创建一个一维数组,长度为3,数值为任意值
    
    array([4.763e-321, 4.763e-321, 0.000e+000])
    
    np.empty((3,3))  #  同上
    
    array([[0.95616699, 0.99284334, 0.16659225],
           [0.02009492, 1.20051115, 0.26599785],
           [0.33018642, 0.0495724 , 0.6749862 ]])
    

    numpy数组的属性

    • nidm(数组的维度)
    • shape(数组的形状,即每个维度的大小)
    • size (数组的总大小,即包含多少个值)
    • dtype (数组的数据类型)
    x3 = np.random.randint(10, size = (3,4,5))
    x3
    
    array([[[6, 2, 0, 3, 6],
            [6, 9, 2, 6, 4],
            [2, 1, 9, 9, 2],
            [2, 8, 7, 3, 5]],
    
           [[2, 7, 0, 2, 3],
            [4, 7, 0, 2, 9],
            [0, 3, 2, 2, 2],
            [3, 1, 7, 0, 8]],
    
           [[5, 7, 9, 3, 7],
            [7, 7, 1, 0, 2],
            [2, 9, 2, 8, 6],
            [4, 7, 3, 1, 0]]])
    
    print('x3 ndim:',x3.ndim)
    print('x3 shape:',x3.shape)
    print('x3 size:',x3.size)
    print('x3 dtype:',x3.dtype)
    
    x3 ndim: 3
    x3 shape: (3, 4, 5)
    x3 size: 60
    x3 dtype: int32
    

    其他属性还包括

    • itemsize (数组单个元素字节大小)
    • nbytes (数组的总字节大小)
    print('itemsize:', x3.itemsize, 'bytes')
    print('nbytes:', x3.nbytes, 'bytes')
    
    itemsize: 4 bytes
    nbytes: 240 bytes
    

    相关文章

      网友评论

          本文标题:numpy数组学习心得(1)

          本文链接:https://www.haomeiwen.com/subject/ndtdvftx.html