numpy.reshape(a, newshape, order='C')
Return reshaped array using the newshape as given.
a | array input |
newshape | New shape for the original array. Integer for 1-D array, tuple for multi Dimensional. |
order | {‘C’, ‘F’, ‘A’}, optional |
![]() | ![]() |
| Shape: (3, 4) Dimension 2 | Shape: (4, 3) Dimension 2 |
import numpy as np
ar=np.ones((6,))
print(ar)
print("Shape: ", ar.shape)
print("Dimension ", ar.ndim)
ar=ar.reshape((2,3))
print("##After using reshpae()##")
print(ar)
print("Shape: ", ar.shape)
print("Dimension ", ar.ndim)
Output
[1. 1. 1. 1. 1. 1.]
Shape: (6,)
Dimension 1
##After using reshpae()##
[[1. 1. 1.]
[1. 1. 1.]]
Shape: (2, 3)
Dimension 2
import numpy as np
ar=np.ones((3,2))
print(ar)
print("Shape: ", ar.shape)
print("Dimension ", ar.ndim)
ar=ar.reshape((2,3))
print("##After using reshpae()##")
print(ar)
print("Shape: ", ar.shape)
print("Dimension ", ar.ndim)
Output
[[1. 1.]
[1. 1.]
[1. 1.]]
Shape: (3, 2)
Dimension 2
##After using reshpae()##
[[1. 1. 1.]
[1. 1. 1.]]
Shape: (2, 3)
Dimension 2
import numpy as np
ar=np.ones((3,2,3))
print(ar)
print("Shape: ", ar.shape)
print("Dimension ", ar.ndim)
ar=ar.reshape((2,3,3))
print("##After using reshpae()##")
print(ar)
print("Shape: ", ar.shape)
print("Dimension ", ar.ndim)
Output
[[[1. 1. 1.]
[1. 1. 1.]]
[[1. 1. 1.]
[1. 1. 1.]]
[[1. 1. 1.]
[1. 1. 1.]]]
Shape: (3, 2, 3)
Dimension 3
##After using reshpae()##
[[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]]
Shape: (2, 3, 3)
Dimension 3
import numpy as np
ar=np.ones((3,2,3,4))
print(ar)
print("Shape: ", ar.shape)
print("Dimension ", ar.ndim)
ar=ar.reshape((9,8))
print("##After using reshpae()##")
print(ar)
print("Shape: ", ar.shape)
print("Dimension ", ar.ndim)
Output
[[[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]]
[[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]]
[[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]]]
Shape: (3, 2, 3, 4)
Dimension 4
##After using reshpae()##
[[1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1.]]
Shape: (9, 8)
Dimension 2
Numpy
eye()
bincount()
arange()
linspace()
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.