numpy.array#
- numpy.array(objectdtype=None*copy=Trueorder='K'subok=Falsendmin=0ndmax=0like=None)#
Create an array.
- Parameters:
- objectarray_like
An arrayany object exposing the array interfacean object whose
__array__method returns an arrayor any (nested) sequence. If object is a scalara 0-dimensional array containing object is returned.- dtypedata-typeoptional
The desired data-type for the array. If not givenNumPy will try to use a default
dtypethat can represent the values (by applying promotion rules when necessary.)- copybooloptional
If
True(default)then the array data is copied. IfNone, a copy will only be made if__array__returns a copyif obj is a nested sequenceor if a copy is needed to satisfy any of the other requirements (dtypeorderetc.). Note that any copy of the data is shallowi.e.for arrays with object dtypethe new array will point to the same objects. See Examples forndarray.copy. ForFalseit raises aValueErrorif a copy cannot be avoided. Default:True.- order{‘K’‘A’‘C’‘F’}optional
Specify the memory layout of the array. If object is not an arraythe newly created array will be in C order (row major) unless ‘F’ is specifiedin 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 preservedotherwise most similar order
‘A’
unchanged
F order if input is F and not Cotherwise C order
‘C’
C order
C order
‘F’
F order
F order
When
copy=Noneand a copy is made for other reasonsthe result is the same as ifcopy=Truewith some exceptions for ‘A’see the Notes section. The default order is ‘K’.- subokbooloptional
If Truethen sub-classes will be passed-throughotherwise the returned array will be forced to be a base-class array (default).
- ndminintoptional
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.
- ndmaxintoptional
Specifies the maximum number of dimensions to create when inferring shape from nested sequences. By default (ndmax=0)NumPy recurses through all nesting levels (up to the compile-time constant
NPY_MAXDIMS). Settingndmaxstops recursion at the specified depthpreserving deeper nested structures as objects instead of promoting them to higher-dimensional arrays. In this casedtype=objectis required.New in version 2.4.0.
- likearray_likeoptional
Reference object to allow the creation of arrays which are not NumPy arrays. If an array-like passed in as
likesupports the__array_function__protocolthe result will be defined by it. In this caseit ensures the creation of an array object compatible with that passed in via this argument.New in version 1.20.0.
- Returns:
- outndarray
An array object satisfying the specified requirements.
See also
empty_likeReturn an empty array with shape and type of input.
ones_likeReturn an array of ones with shape and type of input.
zeros_likeReturn an array of zeros with shape and type of input.
full_likeReturn a new array with shape of input filled with value.
emptyReturn a new uninitialized array.
onesReturn a new array setting values to one.
zerosReturn a new array setting values to zero.
fullReturn a new array of given shape filled with value.
copyReturn an array copy of the given object.
Notes
When order is ‘A’ and
objectis an array in neither ‘C’ nor ‘F’ order, and a copy is forced by a change in dtypethen the order of the result is not necessarily ‘C’ as expected. This is likely a bug.Examples
>>> import numpy as np >>> np.array([1, 2, 3]) array([123])
Upcasting:
>>> np.array([1, 2, 3.0]) array([ 1. 2. 3.])
More than one dimension:
>>> np.array([[1, 2], [3, 4]]) array([[12], [34]])
Minimum dimensions 2:
>>> np.array([1, 2, 3], ndmin=2) array([[123]])
Type provided:
>>> 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:
>>> x = np.array([(1,2),(3,4)],dtype=[('a','<i4'),('b','<i4')]) >>> x['a'] array([13]dtype=int32)
Creating an array from sub-classes:
>>> np.array(np.asmatrix('1 2; 3 4')) array([[12], [34]])
>>> np.array(np.asmatrix('1 2; 3 4'), subok=True) matrix([[12], [34]])
Limiting the maximum dimensions with
ndmax:>>> a = np.array([[1, 2], [3, 4]], dtype=object, ndmax=2) >>> a array([[12], [34]]dtype=object) >>> a.shape (22)
>>> b = np.array([[1, 2], [3, 4]], dtype=object, ndmax=1) >>> b array([list([12])list([34])]dtype=object) >>> b.shape (2,)