NumPy ndarray.all() vs np.all(ndarray) vs all(ndarray)

前端 未结 3 1726
夕颜
夕颜 2021-02-20 17:06

What is the the difference between the three \"all\" methods in Python/NumPy? What is the reason for the performance difference? Is it true that ndarray.all() is always the fast

3条回答
  •  被撕碎了的回忆
    2021-02-20 17:21

    The difference between np.all(a) and a.all() is simple:

    • If a is a numpy.array then np.all() will simply call a.all().
    • If a is not a numpy.array the np.all() call will convert it to an numpy.array and then call a.all(). a.all() on the other hand will fail because a wasn't a numpy.array and therefore probably has no all method.

    The difference between np.all and all is more complicated.

    • The all function works on any iterable (including list, sets, generators, ...). np.all works only for numpy.arrays (including everything that can be converted to a numpy array, i.e. lists and tuples).
    • np.all processes an array with specified data type, that makes it pretty efficient when comparing for != 0. all however needs to evaluate bool for each item, that's much slower.
    • processing arrays with python functions is pretty slow because each item in the array needs to be converted to a python object. np.all doesn't need to do that conversion.

    Note that the timings depend also on the type of your a. If you process a python list all can be faster for relativly short lists. If you process an array, np.all and a.all() will be faster in almost all cases (except maybe for object arrays, but I won't go down that path, that way lies madness).

提交回复
热议问题