What is the difference between Numpy’s array() and asarray() functions? When should you use one rather than the other? They seem to generate identical output for all the inputs I can think of.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The definition of asarray is:
def asarray(a, dtype=None, order=None):
return array(a, dtype, copy=False, order=order)
So it is like array, except it has fewer options, and copy=False. array has copy=True by default.
The main difference is that array (by default) will make a copy of the object, while asarray will not unless necessary.
Method 2
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 copymbut nota, becausemis not anndarray.np.array(a, copy=False, subok=True)andnp.array(m, copy=False, subok=True)will copy neither, becausemis amatrix, which is a subclass ofndarray.np.array(a, dtype=int, copy=False, subok=True)will copy both, because thedtypeis 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 compatiblendarray(copy=False).asanyarray: The input will be returned uncopied iff it’s a compatiblendarrayor subclass likematrix(copy=False,subok=True).ascontiguousarray: The input will be returned uncopied iff it’s a compatiblendarrayin contiguous C order (copy=False,order='C').asfortranarray: The input will be returned uncopied iff it’s a compatiblendarrayin 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 anobjectarray 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).
Method 3
The difference can be demonstrated by this example:
-
generate a matrix
>>> A = numpy.matrix(numpy.ones((3,3))) >>> A matrix([[ 1., 1., 1.], [ 1., 1., 1.], [ 1., 1., 1.]]) -
use
numpy.arrayto modifyA. Doesn’t work because you are modifying a copy>>> numpy.array(A)[2]=2 >>> A matrix([[ 1., 1., 1.], [ 1., 1., 1.], [ 1., 1., 1.]]) -
use
numpy.asarrayto modifyA. It worked because you are modifyingAitself>>> numpy.asarray(A)[2]=2 >>> A matrix([[ 1., 1., 1.], [ 1., 1., 1.], [ 2., 2., 2.]])
Hope this helps!
Method 4
The differences are mentioned quite clearly in the documentation of array and asarray. The differences lie in the argument list and hence the action of the function depending on those parameters.
The function definitions are :
numpy.array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0)
and
numpy.asarray(a, dtype=None, order=None)
The following arguments are those that may be passed to array and not asarray as mentioned in the documentation :
copy : bool, optional If true (default), then the object is copied.
Otherwise, 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.).subok : bool, optional If True, then sub-classes will be
passed-through, otherwise the returned array will be forced to be a
base-class array (default).ndmin : int, optional Specifies the minimum number of dimensions that
the resulting array should have. Ones will be pre-pended to the shape
as needed to meet this requirement.
Method 5
asarray(x) is like array(x, copy=False)
Use asarray(x) when you want to ensure that x will be an array before any other operations are done. If x is already an array then no copy would be done. It would not cause a redundant performance hit.
Here is an example of a function that ensure x is converted into an array first.
def mysum(x):
return np.asarray(x).sum()
Method 6
Here’s a simple example that can demonstrate the difference.
The main difference is that array will make a copy of the original data and using different object we can modify the data in the original array.
import numpy as np a = np.arange(0.0, 10.2, 0.12) int_cvr = np.asarray(a, dtype = np.int64)
The contents in array (a), remain untouched, and still, we can perform any operation on the data using another object without modifying the content in original array.
Method 7
Let’s Understand the difference between np.array() and np.asarray() with the example:
np.array(): Convert input data (list, tuple, array, or other sequence type) to an ndarray and copies the input data by default.
np.asarray(): Convert input data to an ndarray but do not copy if the input is already an ndarray.
#Create an array...
arr = np.ones(5); # array([1., 1., 1., 1., 1.])
#Now I want to modify `arr` with `array` method. Let's see...
arr = np.array(arr)[3] = 200; # array([1., 1., 1., 1., 1.])
No change in the array because we are modify a copy of the arr.
Now, modify arr with asarray() method.
arr = np.asarray(arr)[3] = 200; # array([1., 200, 1., 1., 1.])
The change occur in this array because we are work with the original array now.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0