python np array_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

----------

object : array_like

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

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) 方式来处理.

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.).

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’.

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).

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','

>>> 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]])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值