What is the difference between np.array() and np.asarray()?
Since other questions are being redirected to this one which ask about asanyarray
or other array creation routines, it's probably worth having a brief summary of what each of them does.
The differences are mainly about when to return the input unchanged, as opposed to making a new array as a copy.
array
offers a wide variety of options (most of the other functions are thin wrappers around it), including flags to determine when to copy. A full explanation would take just as long as the docs (see Array Creation, but briefly, here are some examples:
Assume a
is an ndarray
, and m
is a matrix
, and they both have a dtype
of float32
:
np.array(a)
andnp.array(m)
will copy both, because that's the default behavior.np.array(a, copy=False)
andnp.array(m, copy=False)
will copym
but nota
, becausem
is not anndarray
.np.array(a, copy=False, subok=True)
andnp.array(m, copy=False, subok=True)
will copy neither, becausem
is amatrix
, which is a subclass ofndarray
.np.array(a, dtype=int, copy=False, subok=True)
will copy both, because thedtype
is not compatible.
array
that control when copying happens:asarray
: The input will be returned uncopied iff it's a compatiblendarray
(copy=False
).asanyarray
: The input will be returned uncopied iff it's a compatiblendarray
or subclass likematrix
(copy=False
,subok=True
).ascontiguousarray
: The input will be returned uncopied iff it's a compatiblendarray
in contiguous C order (copy=False
,order='C')
.asfortranarray
: The input will be returned uncopied iff it's a compatiblendarray
in contiguous Fortran order (copy=False
,order='F'
).require
: The input will be returned uncopied iff it's compatible with the specified requirements string.copy
: The input is always copied.fromiter
: The input is treated as an iterable (so, e.g., you can construct an array from an iterator's elements, instead of anobject
array with the iterator); always copied.
asarray_chkfinite
(same copying rules as asarray
, but raises ValueError
if there are any nan
or inf
values), and constructors for subclasses like matrix
or for special cases like record arrays, and of course the actual ndarray
constructor (which lets you create an array directly out of strides over a buffer). What is the difference between ndarray and array in NumPy?
numpy.array
is just a convenience function to create an ndarray
; it is not a class itself.
You can also create an array using numpy.ndarray
, but it is not the recommended way. From the docstring of numpy.ndarray
:
Most of the meat of the implementation is in C code, here in multiarray, but you can start looking at the ndarray interfaces here:Arrays should be constructed using
array
,zeros
orempty
... The parameters given here refer to a
low-level method (ndarray(...)
) for instantiating an array.
https://github.com/numpy/numpy/blob/master/numpy/core/numeric.py
Is there a significant overhead in calling `np.asarray' on a NumPy array?
Short answer: Since you are checking with isinstance()
, you may use numpy.asanyarray()
which will pass through any ndarray
and its subclasses without overhead.
According to the docs for numpy.asarray(), when the input is already an ndarray
type, there is no overhead when the input is already an array: no copying happens, they "pass through". Although, it is worth noting that a subclass of ndarray
does not pass through.
Since in your original code you are using isinstance(x, numpy.ndarray)
, you most likely will want numpy.asanyarray()
which passes though the subclasses of ndarray
also, which would be more efficient for your use case. (Because isinstance()
returns true for subclasses as well)
Returns: out : ndarray Array interpretation of a. No copy isThis example from the docs (plus my own comments) explains the differences and why
performed if the input is already an ndarray with matching dtype and
order. If a is a subclass of ndarray, a base class ndarray is
returned.
asanyarray()
is better for your use case:>>> issubclass(np.recarray, np.ndarray)
True # This is to show that recarray is a subclass of ndarray
>>> a = np.array([(1.0, 2), (3.0, 4)], dtype='f4,i4').view(np.recarray)
>>> np.asarray(a) is a
False # Here a copy happens which is an overhead you do not want,
# because the input type recarray is only a subclass of ndarray
>>> np.asanyarray(a) is a
True # Here no copying happens, your subclass of ndarray passes through.
What are the differences between numpy arrays and matrices? Which one should I use?
As per the official documents, it's not anymore advisable to use matrix class since it will be removed in the future.
https://numpy.org/doc/stable/reference/generated/numpy.matrix.html
As other answers already state that you can achieve all the operations with NumPy arrays.
np.ascontiguousarray versus np.asarray with Cython
You should be able to just do:
np.ndarray[double, ndim=1, mode="c"] arr = np.array([1,2,3], dtype=np.float64, order="c")
From the docs for np.array
:order : {'C', 'F', 'A'}, optional
Specify the order of the array. If order is 'C' (default), then the
array will be in C-contiguous order (last-index varies the
fastest). If order is 'F', then the returned array
will be in Fortran-contiguous order (first-index varies the
fastest). If order is 'A', then the returned array may
be in any order (either C-, Fortran-contiguous, or even
discontiguous).
My understanding is that you only need to use np.ascontiguousarray
if the array you are trying to pass was generated from some non-contiguous slice of another array. If you are creating the array from scratch, it shouldn't be necessary. For example:
a = np.arange(10)
a.flags['C_CONTIGUOUS'] # True
b = a[::2]
b.flags['C_CONTIGUOUS'] # False
c = np.ascontiguousarray(b)
c.flags['C_CONTIGUOUS'] # True
Also, perhaps consider using the typed memoryview interfacedouble[::1] arr = np.array([1,2,3], dtype=np.float64)
Related Topics
Overriding the Save Method in Django Modelform
How to Change Any Data Type into a String
What Is the Inverse Function of Zip in Python
Safely Create a File If and Only If It Does Not Exist with Python
Plotting Results of Hierarchical Clustering Ontop of a Matrix of Data in Python
Continuing in Python's Unittest When an Assertion Fails
How to Strip All Whitespace from String
Importerror: Cannot Import Name Numpy_Mkl
Filename and Line Number of Python Script
SQL Alchemy Orm Returning a Single Column, How to Avoid Common Post Processing
Is There a Numpy Builtin to Reject Outliers from a List
What Does Model.Train() Do in Pytorch
How to Extract a Subset of a Colormap as a New Colormap in Matplotlib
Why Use Sys.Path.Append(Path) Instead of Sys.Path.Insert(1, Path)