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

前端 未结 6 788
孤城傲影
孤城傲影 2020-11-28 00:52

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 in

6条回答
  •  失恋的感觉
    2020-11-28 01:23

    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.

提交回复
热议问题