Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Numpy's array() and asarray() functions?

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.

like image 859
Benjamin Hodgson Avatar asked Jan 19 '13 15:01

Benjamin Hodgson


People also ask

What is Numpy Asarray?

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 arrays. Syntax : numpy.asarray(arr, dtype=None, order=None)

What is Asanyarray?

asanyarray() function is used when we want to convert input to an array but it pass ndarray subclasses through. Input can be scalars, lists, lists of tuples, tuples, tuples of tuples, tuples of lists and ndarrays. Syntax : numpy.asanyarray(arr, dtype=None, order=None)

What is subok in Python?

subok : [bool, optional] to make subclass of a or not. Return : array with the same shape and type as a given array.


1 Answers

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.

like image 183
unutbu Avatar answered Oct 04 '22 00:10

unutbu