Although I have seen some answers for this question, I was wondering if there's any better way to solve this problem.
For example,
N = 15
A = np.repeat(1/N, N)
if I do this the result will have shape (15, )
If I want the shape to be (15,1), I think I can do
A = np.repeat(1/N, N)[:,np.newaxis]
I also have the same kind of problems with
np.ones(N); np.zeros(N)
and I can probably make the second dimension as 1, by using "np.newaxis"
But my question is, are there any better ways to do this?
np.newaxisis a good tool for this task (though I use the shorterNone). There's a minimal speed penalty. The idiom is clear (to experienced users). And it is useful in many other circumstances.A. There is a-1shortcut, which often puzzles beginners. Timings for a small task like this are tricky (we are just making aview), butnewaxistimes faster. But for me it's mainly a matter of readability. The purpose ofnewaxisis clear.1is notempty.