Differencebetween Np.Array() and Np.Asarray()

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) and np.array(m) will copy both, because that's the default behavior.
  • np.array(a, copy=False) and np.array(m, copy=False) will copy m but not a, because m is not an ndarray.
  • np.array(a, copy=False, subok=True) and np.array(m, copy=False, subok=True) will copy neither, because m is a matrix, which is a subclass of ndarray.
  • np.array(a, dtype=int, copy=False, subok=True) will copy both, because the dtype is not compatible.

Most of the other functions are thin wrappers around array that control when copying happens:

  • asarray: The input will be returned uncopied iff it's a compatible ndarray (copy=False).
  • asanyarray: The input will be returned uncopied iff it's a compatible ndarray or subclass like matrix (copy=False, subok=True).
  • ascontiguousarray: The input will be returned uncopied iff it's a compatible ndarray in contiguous C order (copy=False, order='C').
  • asfortranarray: The input will be returned uncopied iff it's a compatible ndarray 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 an object array with the iterator); always copied.

There are also convenience functions, like 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:

Arrays should be constructed using array, zeros or empty ... The parameters given here refer to a
low-level method (ndarray(...)) for instantiating an array.

Most of the meat of the implementation is in C code, here in multiarray, but you can start looking at the ndarray interfaces here:

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 is
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.

This example from the docs (plus my own comments) explains the differences and why 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 interface

double[::1] arr = np.array([1,2,3], dtype=np.float64)


Related Topics



Leave a reply



Submit