array

函数原型

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

参数:

  • object array_like对象,一个数组、展现数组接口的对象、具有函数array并且返回一个数组的对象、任何(嵌套)序列。如果是标量,会创建并返回一个0维数组

  • dtype 可选参数 数据类型 数组的预期的元素类型。如果未提供、那么通过所容纳的序列元素推断出所需的最低类型.

  • copy 可选的bool值参数, 默认为true,object对象会被copy。否则,仅仅在array返回一个copy, 或者 obj是一个嵌套的序列,或者需要copy来满足任何其他需求时

  • order 值为{'K', ‘A’, ‘C’, ‘F'}, 可选参数。声明数组的内存布局方式。如果object不是数组,那么新创建的数组会具有C语言的顺序(行优先),除非声明了’F’, 这样的话会使用Fortran顺序(列优先)。如果object对象是数组,那么使用如下的顺序。

        order参数       no copy     copy=True
        -------———— ----------- -----------------------------------------------------
        'K'         不变         F & C order preserved, otherwise most similar order
        'A'         不变         F order if input is F and not C, otherwise C order
        'C'         C order     C order
        'F'         F order     F order
    
      When `copy=False` and a copy is made for other reasons, the result is the same as if
      `copy=True` with some exceptions for 'A', see the Notes section.
    
      The default order is 'K'.
    
  • subok 可选的bool值参数

    : If True, then sub-classes will be passed-through, otherwise the returned array will be forced to be a base-class array (default). 如果设置为True,那么子类会被传递出去,否则返回的数组会被强迫转换成基类的数组(缺省行为)

  • ndmin[int, optional]{.classifier}

    : Specifies the minimum number of dimensions that the resulting array should have. Ones will be prepended to the shape as needed to meet this requirement.

  • like[array_like, optional]{.classifier}

    : Reference object to allow the creation of arrays which are not NumPy arrays. If an array-like passed in as like{.docutils .literal .notranslate} supports the __array_function__{.docutils .literal .notranslate} protocol, the result will be defined by it. In this case, it ensures the creation of an array object compatible with that passed in via this argument.

返回值:

  • out[ndarray], 满足指定需求的数组对象

array 例子

1
2
>>> np.array([1, 2, 3])
array([1, 2, 3])

向上转换类型

1
2
>>> np.array([1, 2, 3.0])
array([ 1.,  2.,  3.])

多维数组

1
2
3
>>> np.array([[1, 2], [3, 4]])
array([[1, 2],
       [3, 4]])

指定维度

1
2
>>> np.array([1, 2, 3], ndmin=2)
array([[1, 2, 3]])

提供类型

1
2
>>> np.array([1, 2, 3], dtype=complex)
array([ 1.+0.j,  2.+0.j,  3.+0.j])

多元素类型

1
2
3
>>> x = np.array([(1,2),(3,4)],dtype=[('a','<i4'),('b','<i4')])
>>> x['a']
array([1, 3])

从子类构建数组

1
2
3
4
5
6
7
>>> 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]])

zeros

返回具有指定shape和类型的新数组,填充元素0

函数原型

numpy.zeros(shape, dtype=float, order=‘C’, *, like=None)

参数:

  • shape 整数或者整数元组,新数组的shape,如(2,3)或2
  • dtype 数据类型 可选参数 比如 numpy.int8. 缺省值是 numpy.float64
  • order {‘C’, ‘F’}, 可选参数,缺省是‘C’。 内存存储多维数据的方式,‘C’表示行-优先(C语言模式),‘F’是列-优先(Fortran模式)
  • like 传入的函数,支持__array_function__ 的对象,传入次参数,那么返回结果由这个参数创建

返回值:

返回指定形状、指定类型和元素排序的元素为0的数组

例子

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
>>> np.zeros(5)
array([ 0.,  0.,  0.,  0.,  0.])

>>> np.zeros((5,), dtype=int)
array([0, 0, 0, 0, 0])

>>> np.zeros((2, 1))
array([[ 0.],
       [ 0.]])

>>> s = (2,2)
>>> np.zeros(s)
array([[ 0.,  0.],
       [ 0.,  0.]])

>>> np.zeros((2,), dtype=[('x', 'i4'), ('y', 'i4')]) # custom dtype
array([(0, 0), (0, 0)],
      dtype=[('x', '<i4'), ('y', '<i4')])

zeros_like

返回和输入参数参数具有相同shape和类型的新数组,填充元素0

函数原型

numpy.zeros_like(a, dtype=None, order=‘K’, subok=True, shape=None)

参数:

The shape and data-type of a define these same attributes of the returned array.

  • a 类数组对象, 返回的数组具有与a相同的形状和数据类型
  • shape 覆盖a的shape参数
  • dtype 覆盖a的数据类型 可选参数 比如 numpy.int8. 缺省值是 numpy.float64
  • order {‘C’, ‘F’, ‘A’, ‘K’}可选参数。 ‘C’ 表示 C-order, ‘F’ 表示 F-order, ‘A’ 表示 如果a是Fortran连续那么是‘F’,否则是‘C’ ‘K’ 表示和a的布局尽可能匹配
  • subok If True, then the newly created array will use the sub-class type of a, otherwise it will be a base-class array. Defaults to True.

返回值:

返回和输入参数具有相同shape和类型的新数组,填充元素0

例子

1
2
3
4
5
6
7
8
>>> x = np.arange(6)
>>> x = x.reshape((2, 3))
>>> x
array([[0, 1, 2],
       [3, 4, 5]])
>>> np.zeros_like(x)
array([[0, 0, 0],
       [0, 0, 0]])

ones

返回指定形状和类型的数组、填充元素‘1’.

函数原型

numpy.ones(shape, dtype=None, order=‘C’, *, like=None)[source]

例子

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

ones_like

返回和给定数组具有相同形状和类型的数组

函数原型

numpy.ones_like(a, dtype=None, order=‘K’, subok=True, shape=None)[source]

例子

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
>>> x = np.arange(6)
>>> x = x.reshape((2, 3))
>>> x
array([[0, 1, 2],
       [3, 4, 5]])

>>> np.ones_like(x)
array([[1, 1, 1],
       [1, 1, 1]])
>>> y = np.arange(3, dtype=float)
>>> y
array([0., 1., 2.])

>>> np.ones_like(y)
array([1.,  1.,  1.])

identity

返回主对角线上都是1的数组

函数原型

numpy.identity(n, dtype=None, *, like=None)[source]

例子

1
2
3
4
>>> np.identity(3)
array([[1.,  0.,  0.],
       [0.,  1.,  0.],
       [0.,  0.,  1.]])

empty

返回指定形状和类型的数组,并未初始化条目

函数原型

numpy.empty(shape, dtype=float, order=‘C’, *, like=None)

例子

1
2
3
4
5
6
7
8
>>> np.empty([2, 2])
array([[ -9.74499359e+001,   6.69583040e-309],
       [  2.13182611e-314,   3.06959433e-309]])      #未初始化

>>> np.empty([2, 2], dtype=int)
array([[-1073741821, -1067949133],
       [  496041986,    19249760]])                   #未初始化

empty_like

返回指定数组形状和类型的数组,并未初始化条目

函数原型

numpy.empty_like(prototype, dtype=None, order=‘K’, subok=True, shape=None)

例子

1
2
3
4
5
6
7
8
9
>>> a = ([1,2,3], [4,5,6])                         # a is array-like
>>> np.empty_like(a)
array([[-1073741821, -1073741821,           3],    # 未初始化
       [          0,           0, -1073741821]])
>>> a = np.array([[1., 2., 3.],[4.,5.,6.]])
>>> np.empty_like(a)
array([[ -2.00000715e+000,   1.48219694e-323,  -2.00000572e+000], # 未初始化
       [  4.38791518e-305,  -2.00000715e+000,   4.17269252e-309]])

arange

返回在指定区间内的均匀分布的值

arange函数,可以接受如下的位置参数被调用:

  • arange(stop): 从半开空间[0, stop)(包含开始的值,不包含结束的值)生成均匀分布的值

  • arange(start, stop): 从半开空间[start, stop)生成均匀分布的值

  • arange(start, stop, step) 从半开空间[start, stop)生成均匀分布的值, 步长由给定参数step确定.

对于整数参数, arange大致等同于Python的内置函数range, 不过返回的不是range实例,而是ndarray数组,

对于非整数的步长step,比如0.1,最好是使用numpy.linspace函数

函数原型

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

例子

1
2
3
4
5
6
7
8
>>> np.arange(3)
array([0, 1, 2])
>>> np.arange(3.0)
array([ 0.,  1.,  2.])
>>> np.arange(3,7)
array([3, 4, 5, 6])
>>> np.arange(3,7,2)
array([3, 5])

##linspace

返回指定区间的均匀分布的数字

函数原型

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

参数:

例子

1
2
3
4
5
6
>>> 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)

numpy.random.Generator.rand

函数原型

asdf

参数:

返回值:

例子

numpy.random.Generator.randn

函数原型

asdf

参数:

返回值:

例子

fromfunction

函数原型

asdf

参数:

返回值:

例子

fromfile

函数原型

asdf

参数:

返回值:

例子

copy{.title-ref},

函数原型

asdf

参数:

返回值:

例子

eye{.title-ref},

函数原型

asdf

参数:

返回值:

例子

[logspace]{.title-ref},

函数原型

asdf

参数:

返回值:

例子

[mgrid]{.title-ref},

函数原型

asdf

参数:

返回值:

例子

[ogrid]{.title-ref},

函数原型

asdf

参数:

返回值:

例子

[r_]{.title-ref},

函数原型

asdf

参数:

返回值:

例子

相关网站