October 2019
Beginner to intermediate
674 pages
16h 43m
English
NumPy has the newaxis method to add additional axes to the existing array:
# Creating a 1D array with 7 elementsIn [98]: array_x = np.array([0, 1, 2, 3, 4, 5, 6])In [99]: array_x.shapeOut[99]: (7,)# Adding a new axis changes the 1D array to 2DIn [100]: array_x[:, np.newaxis]Out[100]:array([[0],[1],[2],[3],[4],[5],[6]])In [101]: array_x[:, np.newaxis].shapeOut[101]: (7, 1)# Adding 2 new axis to the 1D array to make it 3DIn [102]: array_x[:, np.newaxis, np.newaxis]Out[102]:array([[[0]],[[1]],[[2]],[[3]],[[4]],[[5]],[[6]]])In [103]: array_x[:, np.newaxis, np.newaxis].shapeOut[103]: (7, 1, 1)
Read now
Unlock full access