Difference of Numpy Dimension Expand code
Question:
I got confuse between two Numpy dimension expand code.
First code is X[:, np.newaxis]
.
Second code is X[:, np.newaxis, :]
.
I ran this code on Jupyter, But it returns same shape and result.
What is the difference?
Answers:
Refer numpy documentation for newaxis.
https://numpy.org/doc/stable/reference/constants.html#numpy.newaxis
x = np.arange(3)
x[newaxis, :] is equivalent to x[newaxis] and x[None] Any dimension after np.newaxis
is still present in the resulting array, even if not explicitly denoted by a slice :
.
x[np.newaxis, :].shape
#(1, 3)
x[np.newaxis].shape
#(1, 3)
x[None].shape
#(1, 3)
x[:, np.newaxis].shape
#(3, 1)
Hence in your case
X[:,np.newaxis] is X[:, np.newaxis, :]
#True
PS- I think you got confused by ellipses...
and np.newaxis
.
X[...,np.newaxis].shape
#(10,2,1)
# newaxis is introduced after all the previous dimensions
X[:, np.newaxis].shape
#(10,1,2)
# newaxis is introduced at 1st index or 2nd position.
I add a few examples for confused people like me.
X = np.array([[i for i in range(10)], [i for i in range(10)]])
# (2, 10)
X[np.newaxis, :, :]
# (1, 2, 10) <- Same as X[np.newaxis]
X[:, np.newaxis, :]
# (2, 1, 10) <- Same as X[:, np.newaxis]
X[:, :, np.newaxis]
# (2, 10, 1) <- Same as X[:, :, np.newaxis]
In Summary, parameters are axis and np.newaxis.
Also, You can input np.newaxis only if you want add new axis at first.
(It means, you can skip axis parameter which is after of np.newaxis)
This is documentation of newaxis method.
https://numpy.org/doc/stable/reference/constants.html#numpy.newaxis
I got confuse between two Numpy dimension expand code.
First code is X[:, np.newaxis]
.
Second code is X[:, np.newaxis, :]
.
I ran this code on Jupyter, But it returns same shape and result.
What is the difference?
Refer numpy documentation for newaxis.
https://numpy.org/doc/stable/reference/constants.html#numpy.newaxis
x = np.arange(3)
x[newaxis, :] is equivalent to x[newaxis] and x[None] Any dimension after np.newaxis
is still present in the resulting array, even if not explicitly denoted by a slice :
.
x[np.newaxis, :].shape
#(1, 3)
x[np.newaxis].shape
#(1, 3)
x[None].shape
#(1, 3)
x[:, np.newaxis].shape
#(3, 1)
Hence in your case
X[:,np.newaxis] is X[:, np.newaxis, :]
#True
PS- I think you got confused by ellipses...
and np.newaxis
.
X[...,np.newaxis].shape
#(10,2,1)
# newaxis is introduced after all the previous dimensions
X[:, np.newaxis].shape
#(10,1,2)
# newaxis is introduced at 1st index or 2nd position.
I add a few examples for confused people like me.
X = np.array([[i for i in range(10)], [i for i in range(10)]])
# (2, 10)
X[np.newaxis, :, :]
# (1, 2, 10) <- Same as X[np.newaxis]
X[:, np.newaxis, :]
# (2, 1, 10) <- Same as X[:, np.newaxis]
X[:, :, np.newaxis]
# (2, 10, 1) <- Same as X[:, :, np.newaxis]
In Summary, parameters are axis and np.newaxis.
Also, You can input np.newaxis only if you want add new axis at first.
(It means, you can skip axis parameter which is after of np.newaxis)
This is documentation of newaxis method.
https://numpy.org/doc/stable/reference/constants.html#numpy.newaxis