numpy.array#
- numpy.array(object,dtype=None,*,copy=True,order='K',subok=False,ndmin=0,like=None)#
Create an array.
- Parameters:
- objectarray_like
An 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 isreturned.- dtypedata-type, optional
The desired data-type for the array. If not given, NumPy will try to usea default
dtype
that can represent the values (by applying promotionrules when necessary.)- copybool, optional
If
True
(default), then the array data is copied. IfNone
,a copy will only be made if__array__
returns a copy, if obj isa nested sequence, or if a copy is needed to satisfy any of the otherrequirements (dtype
,order
, etc.). Note that any copy ofthe data is shallow, i.e., for arrays with object dtype, the newarray will point to the same objects. See Examples forndarray.copy
.ForFalse
it raises aValueError
if 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 array, thenewly created array will be in C order (row major) unless ‘F’ isspecified, 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=None
and a copy is made for other reasons, the result isthe same as ifcopy=True
, with some exceptions for ‘A’, see theNotes section. The default order is ‘K’.- subokbool, optional
If True, then sub-classes will be passed-through, otherwisethe returned array will be forced to be a base-class array (default).
- ndminint, optional
Specifies the minimum number of dimensions that the resultingarray should have. Ones will be prepended to the shape asneeded to meet this requirement.
- likearray_like, optional
Reference object to allow the creation of arrays which are notNumPy arrays. If an array-like passed in as
like
supportsthe__array_function__
protocol, the result will be definedby it. In this case, it ensures the creation of an array objectcompatible 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_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.
copy
Return an array copy of the given object.
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 isnot necessarily ‘C’ as expected. This is likely a bug.Examples
>>>importnumpyasnp>>>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)array([[1, 2, 3]])
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([1, 3], dtype=int32)
Creating an array from sub-classes:
>>>np.array(np.asmatrix('1 2; 3 4'))array([[1, 2], [3, 4]])
>>>np.array(np.asmatrix('1 2; 3 4'),subok=True)matrix([[1, 2], [3, 4]])