前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >numpy.array

numpy.array

作者头像
狼啸风云
修改2022-09-03 20:49:14
5280
修改2022-09-03 20:49:14
举报

numpy.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. This argument can only be used to ‘upcast’ the array. For downcasting, use the .astype(t) method. 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. 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). If object is an array the following holds. order no copy copy=True ‘K’ unchanged F & C order preserved, otherwise most similar order ‘A’ unchanged 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, 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. Ones will be pre-pended to the shape as needed to meet this requirement.

order

no copy

copy=True

‘K’

unchanged

F & C order preserved, otherwise most similar order

‘A’

unchanged

F order if input is F and not C, otherwise C order

‘C’

C order

C order

‘F’

F order

F order

order

no copy

copy=True

‘K’

unchanged

F & C order preserved, otherwise most similar order

‘A’

unchanged

F order if input is F and not C, otherwise C order

‘C’

C order

C order

‘F’

F order

F order

Returns:

out : ndarray An array object satisfying the specified requirements.

See also

empty_like Return an empty array with shape and type of input.

ones_like Return an array of ones with shape and type of input.

zeros_like Return an array of zeros with shape and type of input.

full_like Return a new array with shape of input filled with value.

empty Return a new uninitialized array.

ones Return a new array setting values to one.

zeros Return a new array setting values to zero.

full Return a new array of given 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

代码语言:javascript
复制
>>> np.array([1, 2, 3])
array([1, 2, 3])

Upcasting:

代码语言:javascript
复制
>>> np.array([1, 2, 3.0])
array([ 1.,  2.,  3.])

More than one dimension:

代码语言:javascript
复制
>>> np.array([[1, 2], [3, 4]])
array([[1, 2],
       [3, 4]])

Minimum dimensions 2:

代码语言:javascript
复制
>>> np.array([1, 2, 3], ndmin=2)
array([[1, 2, 3]])

Type provided:

代码语言:javascript
复制
>>> np.array([1, 2, 3], dtype=complex)
array([ 1.+0.j,  2.+0.j,  3.+0.j])

Data-type consisting of more than one element:

代码语言:javascript
复制
>>> x = np.array([(1,2),(3,4)],dtype=[('a','<i4'),('b','<i4')])
>>> x['a']
array([1, 3])

Creating an array from sub-classes:
代码语言:javascript
复制
>>> 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]])
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-11-20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档