NumPy创建数组的几种方式介绍

创建数组

创建数组的几种常见机制:

  1. numpy 中定义的原生数组创建函数
  2. 使用Python中类数组对象创建 numpy 数组

一、 numpy 中定义的原生数组创建函数

1. numpy.zeros(shape, dtype=float, order='C')

参数

  • shape:int or tuple of lnts,新数组的形状大小;
  • dtype:data-type, optional,指定数组元素的数据类型(dtype);
  • order:{‘C’, ‘F’}, optional,行优先或者列优先;

Returns

  • out:ndarray,函数返回一个全零数组。
>>>np.zeros([2,3], dtype=int)
array([[0, 0, 0],
       [0, 0, 0]])

2. numpy.empty(shape, dtype=float, order='C')

参数

  • shape:int or tuple of lnts,新数组的形状大小;
  • dtype:data-type, optional,指定数组元素的数据类型(dtype);
  • order:{‘C’, ‘F’}, optional,行优先或者列优先;

Returns

  • out:ndarray,函数返回一个未初始化的数组。

>>>np.zeros([2,3], dtype=int)
array([[          0,           0,   242358960],
       [        720,           0, -2147483648]])

zeros 函数不同,empty 不会将元素初始化为零,因此创建速度更快。但是它就需要用户手动设置数组的所有元素值,谨慎使用。

3. numpy.ones(shape, dtype=float, order='C')

参数

  • shape:int or tuple of lnts,新数组的形状大小;
  • dtype:data-type, optional,指定数组元素的数据类型(dtype);
  • order:{‘C’, ‘F’}, optional,行优先或者列优先;

Returns

  • out:ndarray,函数返回一个全一数组。

>>>np.ones([2,3], dtype=int)
array([[1, 1, 1],
       [1, 1, 1]])

4. numpy.full(shape, fill_value, dtype=None, order='C')

参数

  • shape:int or sequence of lnts,新数组的形状大小;
  • fill_value:将被赋值给新数组的元素值;
  • dtype:data-type, optional,指定数组元素的数据类型(dtype);
  • order:{‘C’, ‘F’}, optional,行优先或者列优先;

Returns

  • out:ndarray,函数返回一个全为fill_value值的数组。
>>>np.full([2,3], 4, dtype=int)
array([[4, 4, 4],
       [4, 4, 4]])

5. numpy.zeros_like(a, dtype=float, order='C', subok=True, shape=None)

参数

  • a:array_like,创建的新数组定义了和这个对象相同的形状和数据类型;
  • dtype:data-type, optional,重新指定数组元素的数据类型(dtype);
  • order:{‘C’, ‘F’, ‘K’}, optional,存储顺序,'C’表示C-order,'F’表示F-order,'K’表示遵循a的顺序;
  • subok:bool, optional,如果为True,则新创建的数组将使用a的子类类型,否则它将是基类数组;
  • shape:int or sequence of ints, optional,重新指定结果的形状。

Returns

  • out:ndarray,函数返回一个全零数组。
>>>a = np.zeros([2,3], dtype=int)
>>>np.zeros_like(a, dtype=float)
array([[0., 0., 0.],
       [0., 0., 0.]])

6. numpy.empty_like(a, dtype=float, order='C', subok=True, shape=None)

参数

  • a:array_like,创建的新数组定义了和这个对象相同的形状和数据类型;
  • dtype:data-type, optional,重新指定数组元素的数据类型(dtype);
  • order:{‘C’, ‘F’, ‘K’}, optional,存储顺序,'C’表示C-order,'F’表示F-order,'K’表示遵循a的顺序;
  • subok:bool, optional,如果为True,则新创建的数组将使用a的子类类型,否则它将是基类数组;
  • shape:int or sequence of ints, optional,重新指定结果的形状。

Returns

  • out:ndarray,函数返回一个未初始化的数组。
>>>a = np.zeros([2,3], dtype=int)
>>>np.empty_like(a)
array([[0, 0, 0],
       [0, 0, 0]])

7. numpy.ones_like(a, dtype=float, order='C', subok=True, shape=None)

参数

  • a:array_like,创建的新数组定义了和这个对象相同的形状和数据类型;
  • dtype:data-type, optional,重新指定数组元素的数据类型(dtype);
  • order:{‘C’, ‘F’, ‘K’}, optional,存储顺序,'C’表示C-order,'F’表示F-order,'K’表示遵循a的顺序;
  • subok:bool, optional,如果为True,则新创建的数组将使用a的子类类型,否则它将是基类数组;
  • shape:int or sequence of ints, optional,重新指定结果的形状。

Returns

  • out:ndarray,函数返回一个全一数组。
>>>a = np.zeros([2,3], dtype=int)
>>>np.ones_like(a)
array([[1, 1, 1],
       [1, 1, 1]])

8. numpy.full_like(a, fill_value, dtype=None, order='K', subok=True, shape=None)

参数

  • a:array_like,创建的新数组定义了和这个对象相同的形状和数据类型;
  • fill_value:将被赋值给新数组的元素值;
  • dtype:data-type, optional,重新指定数组元素的数据类型(dtype);
  • order:{‘C’, ‘F’, ‘K’}, optional,存储顺序,'C’表示C-order,'F’表示F-order,'K’表示遵循a的顺序;
  • subok:bool, optional,如果为True,则新创建的数组将使用a的子类类型,否则它将是基类数组;
  • shape:int or sequence of ints, optional,重新指定结果的形状。

Returns

  • out:ndarray,函数返回一个全一数组。
>>>a = np.zeros([2,3], dtype=int)
>>>np.full_like(a, 4)
array([[4, 4, 4],
       [4, 4, 4]])

9. numpy.eye(N, M=None, k=0, dtype=<class 'float'>, order='C')

参数

  • N:int,输出数组的行数;
  • M:int, optional,输出数组的列数,如果为None,默认等与N;
  • k:int, optional,0表示主对角线,正数表示上对角线,负数表示下对角线;
  • dtype:data-type, optional,重新指定数组元素的数据类型(dtype);
  • order:{‘C’, ‘F’}, optional,行优先或者列优先;

Returns

  • out:ndarray,函数返回一个对角线元素为1,其余元素为零的数组。
>>>np.eye(4, M=5, dtype=float)
array([[1., 0., 0., 0., 0.],
       [0., 1., 0., 0., 0.],
       [0., 0., 1., 0., 0.],
       [0., 0., 0., 1., 0.]])

>>>np.eye(4, M=5, k=1, dtype=float)
array([[0., 1., 0., 0., 0.],
       [0., 0., 1., 0., 0.],
       [0., 0., 0., 1., 0.],
       [0., 0., 0., 0., 1.]])

10. numpy.identity(n, dtype=<class 'float'>)

参数

  • n:int,输出数组的行数;
  • dtype:data-type, optional,指定数组元素的数据类型(dtype);

Returns

  • out:ndarray,函数返回一个对角线元素为1的n×n数组。
>>>np.identity(4, dtype=int)
array([[1, 0, 0, 0],
       [0, 1, 0, 0],
       [0, 0, 1, 0],
       [0, 0, 0, 1]])

11. numpy.arange([start, ]stop, [step, ]dtype=None)

参数

  • start:int or real, optional,间隔开始,包括开始该值,默认为0;
  • stop:int or real,间隔结束,不包括该值;
  • step:int or real, optional,间隔长度,默认为1;
  • dtype:dtype, optional,输出数组的数据类型。

Returns

  • out:ndarray,返回固定间隔值的数组。
# arange(stop)
>>>np.arange(6, dtype=int)
array([0, 1, 2, 3, 4, 5])

# arange(start, stop)
>>>np.arange(3, 9, dtype=int)
array([3, 4, 5, 6, 7, 8])

# arange(start, stop, step) 

>>>np.arange(3, 6, 0.5, dtype=int)
array([3, 3, 3, 3, 3, 3])
>>>np.arange(3, 6, 0.5, dtype=float)
array([3. , 3.5, 4. , 4.5, 5. , 5.5])

注意

  1. 如果是浮点参数,那么输出数组的长度是 ceil(stop-start)/step , 因为浮点溢出问题,可能会导致输出数组的最后一个元素大于 stop 值。因此,这种情况下,应该使用numpy.linspace
>>>np.arange(-3, 3, 0.5, dtype=int)
array([-3, -2, -1,  0,  1,  2,  3,  4,  5,  6,  7,  8])
  1. 内置的 range 函数生成任意大小的Python内置整型, numpy.arange生成的是numpy.int32 or numpy.int64 。这可能会导致对于大的正数的错误结果。

12. numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)

参数

  • start:array_like,序列的起始值;
  • stop:array_like,序列的终止值,如果endpoint为true,该值包含于数列中;
  • num:int, optional,要生成的等步长的样本数量,默认为50;
  • endpoint:bool, optional,该值为 true 时,数列中包含stop值,反之不包含,默认是True;
  • retstep:bool, optional,如果为 True 时,生成的数组中会显示间距,反之不显示;
  • dtype: dtype, optional,ndarray 的数据类型;

Returns

  • samples:ndarray,等距间隔的序列;
  • step:float, optional,间隔长度;
>>>np.linspace(2.0, 3.0, num=5)
array([2.  , 2.25, 2.5 , 2.75, 3.  ])
>>>np.linspace(2.0, 3.0, num=5, endpoint=False)
array([2. ,  2.2,  2.4,  2.6,  2.8])
>>>np.linspace(2.0, 3.0, num=5, retstep=True)
(array([2.  ,  2.25,  2.5 ,  2.75,  3.  ]), 0.25)

13. numpy.logspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)

参数

  • start:array_like,序列的起始值;
  • stop:array_like,序列的终止值,如果endpoint为true,该值包含于数列中;
  • num:int, optional,要生成的等步长的样本数量,默认为50;
  • endpoint:bool, optional,该值为 true 时,数列中包含stop值,反之不包含,默认是True;
  • retstep:bool, optional,如果为 True 时,生成的数组中会显示间距,反之不显示;
  • dtype: dtype, optional,ndarray 的数据类型;

Returns

  • samples:ndarray,对数刻度上等距的采样序列;
>>>np.logspace(2.0, 3.0, num=4)
array([ 100.        ,  215.443469  ,  464.15888336, 1000.        ])
>>>np.logspace(2.0, 3.0, num=4, endpoint=False)
array([100.        ,  177.827941  ,  316.22776602,  562.34132519])
>>>np.logspace(2.0, 3.0, num=4, base=2.0)
array([4.        ,  5.0396842 ,  6.34960421,  8.        ])

二、从Python的 array_like 对象转化为 NumPy数组

1. numpy.array(object, dtype=None, *, copy=True, order='K', subok=False, ndmin=0, like=None)

参数

  • object:array-like结构的数值数据对象,如果是标量,返回包含该对象的0维数组;
  • dtype:data-type, optional,输出数组的数据类型;
  • copy :bool, optional,值为True,则复制对象;
  • order:{‘C’, ‘F’, ‘K’}, optional,存储顺序,'C’表示C-order,'F’表示F-order,'K’表示遵循a的顺序;
  • subok:bool, optional,如果为True,直接返回子类,否则返回数组;
  • ndminint, optional,指定生成的数组应具有的最小维度数。为了满足这一要求,将根据需要对其形状进行预处理;

Returns

  • out:ndarray,满足要求的数组对象;
>>>np.array([1, 2, 3])
array([1, 2, 3])

# 多维数组
>>>np.array([[1, 2], [3, 4]])
array([[1, 2],
       [3, 4]])

# 最小二维
>>>np.array([1, 2, 3], ndmin=2)
array([[1, 2, 3]])

# 结构化数据类型对象
>>>x = np.array([(1,2),(3,4)],dtype=[('a','<i4'),('b','<i4')])
x['a']
array([1, 3])

# 从子类创造一个数组
>>>np.array(np.mat('1 2; 3 4'))
array([[1, 2],
       [3, 4]])
>>>np.array(np.mat('1 2; 3 4'), subok=True)
matrix([[1, 2],
        [3, 4]])

2. numpy.asarray(a, dtype=None, order=None, *, like=None)

参数

  • a:array_like,输入数据,任何形式都会被转化为数组;
  • dtype:data_type, optional,设置输出数据的数据类型;
  • order:{‘C’, ‘F’, ‘K’}, optional,存储顺序,'C’表示C-order,'F’表示F-order,'K’表示遵循a的顺序;

Returns

  • out:ndarray,如果输入的 a 已经是满足要求的数组了,则不会复制,直接返回。如果 a 是数组的子类,则返回基类ndarray。
# 列表生成数组
>>>a = [1, 2]
>>>np.asarray(a)
array([1, 2])

# a 是数组,不发生复制
>>>a = np.array([1, 2])
>>>np.asarray(a) is a
True

# a 是数组,重新设置了新的数据类型,发生复制

>>>a = np.array([1, 2], dtype=np.float32)
>>>np.asarray(a, dtype=np.float32) is a
True
>>>np.asarray(a, dtype=np.float64) is a
False

numpy.arraynumpy.asarray 的区别:
当参数本身就是数组时,numpy.array 会重新产生一个副本,而 numpy.asarray 不会。

3. numpy.asanyarray(a, dtype=None, order=None, *, like=None)

参数

  • a:array_like,输入数据,任何形式都会被转化为数组;
  • dtype:data_type, optional,设置输出数据的数据类型;
  • order:{‘C’, ‘F’, ‘K’}, optional,存储顺序,'C’表示C-order,'F’表示F-order,'K’表示遵循a的顺序;

Returns

  • out:ndarray or an ndarray subclass,如果输入的 a 已经是满足要求的数组或数组的子类,则不会复制,直接返回。
>>>a = [1, 2]
>>>np.asanyarray(a)
array([1, 2])

>>>a = np.array([(1.0, 2), (3.0, 4)], dtype='f4,i4').view(np.recarray)
>>>np.asanyarray(a) is a
True

4. numpy.asmatrix(data, dtype=None)

参数

  • data:array_like,输入数据;
  • dtype:data_type, optional,设置输出数据的数据类型;
    Returns
  • mat:matrix,输出转化后的矩阵,如果输入已经是一个矩阵,则不发生复制。
>>>x = np.array([[1, 2], [3, 4]])
>>>m = np.asmatrix(x)
>>>x[0,0] = 5
>>>m
matrix([[5, 2],
        [3, 4]])

5. numpy.copy(a, order='K', subok=False)

参数

  • a:array_like,输入数据;
  • order:{‘C’, ‘F’, ‘K’}, optional,存储顺序,'C’表示C-order,'F’表示F-order,'K’表示遵循a的顺序;
  • subok:bool, optional,如果为True,则子类将被返回,否则返回的数组将被强制为基类数组(默认为False)。

Returns

  • out:数组a;效果等同于np.array(a, copy=True)
  • 4
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值