bundles / numpy 2.4.3 / numpy / array
built-in
numpy:array
Signature
array ( object , dtype = None , * , copy = True , order = K , subok = False , ndmin = 0 , ndmax = 0 , like = None ) Summary
Create an array.
Parameters
object: array_likeAn array, any object exposing the array interface, an object whose
__array__method returns an array, or any (nested) sequence. If object is a scalar, a 0-dimensional array containing object is returned.dtype: data-type, optionalThe desired data-type for the array. If not given, NumPy will try to use a default
dtypethat can represent the values (by applying promotion rules when necessary.)copy: bool, optionalIf
True(default), then the array data is copied. IfNone, 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.). Note that any copy of the data is shallow, i.e., for arrays with object dtype, the 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'}, optionalSpecify 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=Noneand a copy is made for other reasons, the result is the same as ifcopy=True, with some exceptions for 'A', see the Notes section. The default order is 'K'.subok: bool, optionalIf True, then sub-classes will be passed-through, otherwise the returned array will be forced to be a base-class array (default).
ndmin: int, optionalSpecifies the minimum number of dimensions that the resulting array should have. Ones will be prepended to the shape as needed to meet this requirement.
ndmax: int, optionalSpecifies 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 depth, preserving deeper nested structures as objects instead of promoting them to higher-dimensional arrays. In this case,dtype=objectis required.like: array_like, optionalReference object to allow the creation of arrays which are not NumPy arrays. If an array-like passed in as
likesupports the__array_function__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.
Returns
out: ndarrayAn array object satisfying the specified requirements.
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
import numpy as np np.array([1, 2, 3])✓
np.array([1, 2, 3.0])
✗np.array([[1, 2], [3, 4]])
✓np.array([1, 2, 3], ndmin=2)
✓np.array([1, 2, 3], dtype=complex)
✗x = np.array([(1,2),(3,4)],dtype=[('a','<i4'),('b','<i4')]) x['a']✓
np.array(np.asmatrix('1 2; 3 4'))
✓np.array(np.asmatrix('1 2; 3 4'), subok=True)
✓a = np.array([[1, 2], [3, 4]], dtype=object, ndmax=2) a a.shape✓
b = np.array([[1, 2], [3, 4]], dtype=object, ndmax=1) b b.shape✓
See also
- copy
Return an array copy of the given object.
- empty
Return a new uninitialized array.
- empty_like
Return an empty array with shape and type of input.
- full
Return a new array of given shape filled with value.
- full_like
Return a new array with shape of input filled with value.
- ones
Return a new array setting values to one.
- ones_like
Return an array of ones with shape and type of input.
- zeros
Return a new array setting values to zero.
- zeros_like
Return an array of zeros with shape and type of input.
Aliases
-
numpy.array
Referenced by
This package
- numpy_2_0_migration_guide
- reference:routines.linalg
- release:2.0.0-notes
- user:basics.creation
- user:basics.dispatch
- numpy_2_0_migration_guide
- reference:routines.linalg
- release:2.0.0-notes
- user:basics.creation
- user:basics.dispatch
- numpy_2_0_migration_guide
- reference:routines.linalg
- release:2.0.0-notes
- user:basics.creation
- user:basics.dispatch