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.newaxis,:].shape
# shape will be (3,1,5)
The documentation states that one can also use None
instead of newaxis
, the effect is exactly the same.
Is there any reason to choose one over the other? Is there any general preference or style guide? My impression is that newaxis
is more popular, probably because it is more explicit. So is there any reason why None
is allowed?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…