Why does numpy have a corresponding function for many ndarray methods?

前端 未结 3 1561
梦如初夏
梦如初夏 2020-12-01 17:03

A few examples:

numpy.sum()
ndarray.sum()
numpy.amax()
ndarray.max()
numpy.dot()
ndarray.dot()

... and quite a few more. Is it to support s

3条回答
  •  悲&欢浪女
    2020-12-01 17:29

    As others have noted, the identically-named NumPy functions and array methods are often equivalent (they end up calling the same underlying code). One might be preferred over the other if it makes for easier reading.

    However, in some instances the two behave different slightly differently. In particular, using the ndarray method sometimes emphasises the fact that the method is modifying the array in-place.

    For example, np.resize returns a new array with the specified shape. On the other hand, ndarray.resize changes the shape of the array in-place. The fill values used in each case are also different.

    Similarly, a.sort() sorts the array a in-place, while np.sort(a) returns a sorted copy.

提交回复
热议问题