python学习-numpy.array

2019-12-29 学习numpy中的array函数用法
一.在命令行中输入help(numpy.array)得到以下输出
help(numpy.array)
Help on built-in function array in module numpy.core.multiarray:
array(…)
array(object, dtype=None, copy=True, order=‘K’, subok=False, ndmin=0)

Create an array.

Parameters
----------
  1. object : array_like
    An array, any object exposing the array interface数组接口, an object whose array method returns an array, or any (nested嵌套) sequence序列.

  2. dtype : data-type, optional(可选可不选的)
    The desired data-type for the array. If not given, then the type will be determined as the minimum type required to hold the objects in the sequence如果不给定data-type,则data-type将会被默认为是能够在序列sequence中容纳该object的最小类型. This argument can only be used to ‘upcast’ the array但是这只能处理向上数组的情况. For downcasting, use the .astype(t) method对于downcasting的类型,则使用.astype(t) 方式来处理.

  3. copy : bool, optional
    If true (default), then the object is copied. Otherwise, a copy will only be made if array returns a copy, if obj is a nested sequence嵌套序列,or if a copy is needed to satisfy any of the other requirements
    (dtype, order, etc.).

  4. order : {‘K’, ‘A’, ‘C’, ‘F’}, optional
    Specify the memory layout存储配置 of the array.
    4.1 If object is not an array, the newly created array will be in C order (row major) unless ‘F’ is specified, in which case it will be in Fortran order (column major).
    4.2 If object is an array the following holds.
    在这里插入图片描述

    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 theNotes section. The default order is ‘K’.

  5. subok : bool, optional
    If True, then sub-classes子类 will be passed-through, otherwise the returned array will be forced to be a base-class array默认为基础类型数组 (default).

  6. ndmin : int, optional
    Specifies the minimum number of dimensions that the resulting array should have. ndmin参数是用于定义结果数组应该拥有的最低维数。 Ones will be pre-pended 预先考虑to the shape as needed to meet this requirement.

二.返回结果
Returns
-------
out : ndarray
An array object satisfying the specified requirements.

See Also
--------
empty_like : Return an empty array with shape and type of input.
                   输出一个和input的shape和type一致的空array
ones_like : Return an array of ones with shape and type of input.
                    按input的shape和type输出一个全1数组
zeros_like : Return an array of zeros with shape and type of input.
                   按input的shape、type输出一个全零的数组
full_like : Return a new array with shape of input filled with value.
                   按input的shape和type输出一个new array
empty : Return a new uninitialized array.
                  返回一个未初始化的数组
ones : Return a new array setting values to one.
                  返回一个new array,setting values to one
zeros : Return a new array setting values to zero.
                  返回一个已经设定了值为0的新数组
full : Return a new array of given shape filled with value.
                 返回一个给定shape的新数组,filled with value

Notes:
When order is 'A' and `object` is an array in neither 'C' nor 'F' order,
and a copy is forced by a change in dtype, then the order of the result is
not necessarily 'C' as expected. This is likely a bug.

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

Upcasting:

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

More than one dimension:

>>> np.array([[1, 2], [3, 4]]) #必须使用[ ]将不同行括起来,否则会报错!
array([[1, 2],
       [3, 4]])

Minimum dimensions 2:

>>> np.array([1, 2, 3], ndmin=2) #ndmin用于指定需要的最小维数
array([[1, 2, 3]])

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

Type provided:
>>> np.array([1, 2, 3], dtype=complex) #python中complex()用于创建一个复数or将一个字符串或数转化为复数。
array([ 1.+0.j,  2.+0.j,  3.+0.j])

Data-type consisting of more than one element:

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

Creating an array from sub-classes:

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

>>> np.array(np.mat('1 2; 3 4'), subok=True)
 #subok为TRUE则返回子类sub-class值,为false则返回一个base-class array
matrix([[1, 2],
        [3, 4]])

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_42714175/article/details/85338169