美文网首页
6-Python-NumPy创建数组

6-Python-NumPy创建数组

作者: 千千罐 | 来源:发表于2020-10-15 17:57 被阅读0次
    1、创建数组函数

    \color{red}{numpy.empty()} 创建一个指定形状(shape)及数据类型(dtype)且未初始化的数组:

    numpy.empty(shape, dtype = float, order = 'C')
    
    • shape 指定 数组维度
    • dtype 指定数据类型
    • order "C"和"F"分别代表,行优先和列优先,在计算机内存中的存储元素的顺序
    >>> import numpy as np
    >>> aa = np.empty([3,2], dtype = int) 
    >>> aa
    array([[140094824630248,        26048112],
           [140094709808288, 140094709812416],
           [140093243260955,               0]])
    

    注意 − 数组元素为随机值,值取决于内存,未初始化

    \color{red}{numpy.zeros()} 创建指定大小,元素为 0 的数组

    numpy.zeros(shape, dtype = float, order = 'C')
    
    • shape 指定 数组维度
    • dtype 指定数据类型
    • order C' 用于 C 的行数组,或者 'F' 用于 FORTRAN 的列数组
    '''  默认为浮点数 '''
    >>> np.zeros(5) 
    array([0., 0., 0., 0., 0.])
    
    ''' 设置类型为整数 '''
    >>> np.zeros((5,), dtype = np.int) 
    array([0, 0, 0, 0, 0])
    
    ''' 自定义类型 '''
    >>> np.zeros((2,2), dtype = [('x', 'i4'), ('y', 'i4')]) 
    array([[(0, 0), (0, 0)],
           [(0, 0), (0, 0)]], dtype=[('x', '<i4'), ('y', '<i4')])
    
    

    \color{red}{numpy.ones()} 创建指定大小,元素为 1 的数组

    numpy.ones(shape, dtype = None, order = 'C')
    
    • shape 指定 数组维度
    • dtype 指定数据类型
    • order C' 用于 C 的行数组,或者 'F' 用于 FORTRAN 的列数组
    # 2页,3行,4列,全1(ones代表生成的数组元素值都是1),指定数据类型
    >>> import numpy as np
    >>> np.ones((2,3,4),dtype=np.int16) 
    array([[[1, 1, 1, 1],
            [1, 1, 1, 1],
            [1, 1, 1, 1]],
    
           [[1, 1, 1, 1],
            [1, 1, 1, 1],
            [1, 1, 1, 1]]], dtype=int16)
    

    \color{red}{numpy.arange()}

    numpy.arange(start, stop, step, dtype)
    

    根据 start 与 stop 指定的范围 [start,stop) 以及 step 设定的步长,生成一个 ndarray

    • start : 起始值,默认为0,(包含)
    • stop : 终止值(不包含)
    • step : 步长,默认为1
    • dtype : 返回ndarray的数据类型,如果没有提供,则会使用输入数据的类型
    ''' 起点为0,间隔为1时可缺省(引起歧义下不可缺省) '''
    >>> np.arange(0,7,1,dtype=np.int16)     
    array([0, 1, 2, 3, 4, 5, 6], dtype=int16)
    
    ''' 起点为0,不超过10,步长为2 '''
    >>> np.arange(0,10,2) 
    array([0, 2, 4, 6, 8])
    
    ''' 改变输出形状 '''
    >>> np.arange(12).reshape(3,4)
    array([[ 0,  1,  2,  3],
           [ 4,  5,  6,  7],
           [ 8,  9, 10, 11]])
    
    ''' 2页,3行,4列 '''
    >>> np.arange(24).reshape(2,3,4)
    array([[[ 0,  1,  2,  3],
            [ 4,  5,  6,  7],
            [ 8,  9, 10, 11]],
    
           [[12, 13, 14, 15],
            [16, 17, 18, 19],
            [20, 21, 22, 23]]])
    

    \color{red}{numpy.eye()}生成对角矩阵

    >>> import numpy as np
    >>> np.eye(4)
    array([[1., 0., 0., 0.],
           [0., 1., 0., 0.],
           [0., 0., 1., 0.],
           [0., 0., 0., 1.]])
    

    \color{red}{numpy.linspace()} 创建一个有等差数列构成的一维数组

    numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
    
    • start:序列的起始值
    • stop:序列的终止值,如果endpoint为true,该值包含于数列中
    • num:要生成的等步长的样本数量,默认为50
    • endpoint:该值为 true 时,数列中包含stop值,反之不包含,默认是True。
    • retstep:如果为 True 时,生成的数组中会显示间距,反之不显示。
    • dtype:ndarray 的数据类型
    • 在区间[start, stop]中,返回num个等间距的样本
    • 当endpoint=False时,不包含结束端点,同时步长也会发生变化
    ''' 起点为-1,终点为2,取5个点 '''
    >>> np.linspace(-1,2,5) 
    array([-1.  , -0.25,  0.5 ,  1.25,  2.  ])
    
    ''' 不包含终点2,可以发现步长发生了变化,所有元素的值都不同了'''
    >>> np.linspace(-1,2,5,endpoint=False) 
    array([-1. , -0.4,  0.2,  0.8,  1.4])
    

    \color{red}{numpy.linspace()} 创建一个于等比数列

    np.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None)
    
    • start:序列的起始值为:base ** start
    • stop:序列的终止值为:base ** stop。如果endpoint为true,该值包含于数列中
    • num:要生成的等步长的样本数量,默认为50
    • endpoint:该值为 true 时,数列中中包含stop值,反之不包含,默认是True
    • base:对数 log 的底数
    • dtype:ndarray 的数据类型
    >>> import numpy as np
    ''' 默认底数是 10 '''
    >>> np.logspace(1.0,2.0,num=10)
    array([ 10.        ,  12.91549665,  16.68100537,  21.5443469 ,
            27.82559402,  35.93813664,  46.41588834,  59.94842503,
            77.42636827, 100.        ])
    
    ''' 底数修改为 2 '''
    >>> np.logspace(1.0,2.0,num=10,base=2)
    array([2.        , 2.16011948, 2.33305808, 2.5198421 , 2.72158   ,
           2.93946898, 3.1748021 , 3.42897593, 3.70349885, 4.        ])
    

    \color{red}{numpy.random.randint()} 返回一个随机数或随机数数组

    numpy.random.randint(low, high=None, size=None, dtype='l')
    
    • low: int 生成的数值最低要大于等于low
    • high: int (可选) 如果使用这个值,则生成的数值在[low, high)区间
    • high = None 时,生成的数值要在[0, low)区间内
    • size: int or tuple of ints(可选) 输出随机数的尺寸,比如size = (m * n* k)则输出同规模即m * n* k个随机数。默认是None的,仅仅返回满足要求的单一随机数。
    • dtype: dtype(可选): 想要输出的格式。如int64、int等等
    • 每次运行生成的数组都是随机不同的
    ''' 大于等于1,小于3,2行3列的随机整数 '''
    >>> np.random.randint(1,3,(2,3)) 
    array([[2, 2, 1],
           [2, 1, 1]])
    
    >>> np.random.randint(2, size=10)
    array([1, 0, 1, 0, 0, 1, 1, 0, 0, 0])
    
    >>> np.random.randint(1, size=10)
    array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    
    >>> np.random.randint(5, size=(2, 4))
    array([[3, 4, 4, 2],
           [1, 1, 4, 2]])
    
    >>> np.random.randint(2, high=10, size=(2,3))
    array([[8, 4, 2],
           [6, 6, 3]])
    

    相关文章

      网友评论

          本文标题:6-Python-NumPy创建数组

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