Numpy: Should I use newaxis or None?

前端 未结 1 441
长情又很酷
长情又很酷 2020-11-28 23:10

In numpy one can use the \'newaxis\' object in the slicing syntax to create an axis of length one, e.g.:

import numpy as np
print np.zeros((3,5))[:,np.newaxi         


        
相关标签:
1条回答
  • 2020-11-28 23:26

    None is allowed because numpy.newaxis is merely an alias for None.

    In [1]: import numpy
    
    In [2]: numpy.newaxis is None
    Out[2]: True
    

    The authors probably chose it because they needed a convenient constant, and None was available.

    As for why you should prefer newaxis over None: mainly it's because it's more explicit, and partly because someday the numpy authors might change it to something other than None. (They're not planning to, and probably won't, but there's no good reason to prefer None.)

    0 讨论(0)
提交回复
热议问题