## Numpy - array vs asarray

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.

**What is the difference among np.array, np.asarray and np ,** The major difference is that np.array will make a copy of the original object and not edit it unless copy is not set to false. Meaning that the numpy.asarray(data, dtype=None, order=None)[source] Here, data: Data that you want to convert to an array. dtype: This is an optional argument. If not specified, the data type is inferred from the input data. Order: Default is C which is an essential row style.

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.

**Numpy,** 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 numpy.asarray(a, dtype=None, order=None)¶. Convert the input to an array. Input data, in any form that can be converted to an array. This includes lists, lists of tuples, tuples, tuples of tuples, tuples of lists and ndarrays.

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.array`

to modify`A`

. 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.asarray`

to modify`A`

. It worked because you are modifying`A`

itself>>> numpy.asarray(A)[2]=2 >>> A matrix([[ 1., 1., 1.], [ 1., 1., 1.], [ 2., 2., 2.]])

Hope this helps!

**numpy.asanyarray,** If a is an ndarray or a subclass of ndarray, it is returned as-is and no copy is asarray: Similar function which always returns ndarrays. ascontiguousarray numpy.asarray()function is used when we want to convert input to an array. Input can be lists, lists of tuples, tuples, tuples of tuples, tuples of lists and ndarrays. Syntax : numpy.asarray(arr, dtype=None, order=None) Parameters : arr : [array_like] Input data, in any form that can be converted to an array.

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 arrayshould have. Ones will be pre-pended to the shape as needed to meet this requirement.

**numpy.asarray,** Parameters: a : array_like. Input data, in any form that can be converted to an array. This includes lists, lists of tuples, tuples, tuples of tuples, tuples of lists and numpy.asarray(a, dtype=None, order=None)¶. Convert the input to an array. a : array_like. Input data, in any form that can be converted to an array. This includes lists, lists of tuples, tuples, tuples of tuples, tuples of lists and ndarrays.

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.

**numpy.asarray() in Python,** arr : [array_like] Input data, in any form that can be converted to an array. This includes lists, lists of tuples, tuples, tuples of tuples, tuples of lists and ndarrays. dtype Numpy is also much more flexible, e.g. it supports arrays of any type of Python objects, and is also able to interact "natively" with your own objects if they conform to the array interface.

**asanyarray vs. asarray,** Was there discussion around which of `asarray` or asanyarray` to prefer? The problem with np.matrix is that it's a subclass, but it's not 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

**NumPy Array manipulation: asarray() function,** Return value: 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 The main problem with subclassing for numpy.ndarray is that it guarantees too much: a large set of operations/methods along with a specific memory layout exposed as part of its public API. Worse, ndarray itself is a little quirky (e.g., with indexing, and its handling of scalars vs. 0d arrays).

**NumPy array creation: asarray() function,** NumPy array creation: asarray() function, example - Convert the input to an if the input is already an ndarray with matching dtype and order.