Numpy reshape()

Numpy

numpy.reshape(a, newshape, order='C')
Return reshaped array using the newshape as given.
aarray input
newshapeNew shape for the original array. Integer for 1-D array, tuple for multi Dimensional.
order{‘C’, ‘F’, ‘A’}, optional
Read more about shape().

Ones array of shape(3,4)Ones array of shape(4,3)
Shape: (3, 4)
Dimension 2
Shape: (4, 3)
Dimension 2
  1. While reshaping data of the original array will not be lost.
  2. The reshape array must be compatible with original shape , otherwise value error will be raised.
We will try to use reshape() with different dimensional arrays. To Understand the difference we will first use the shape() to get the tuple or integer about the Array and then use reshape() and get the details of the updated array. You can read more on how to create arrays by using ones(). We will input different shape() to create the array by using ones().

Along with shape() we will display the dimension of the array by using ndim

One dimensional array

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

Two dimensional array

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

Three dimensional array

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

Four dimensional array

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()
Subscribe to our YouTube Channel here

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2022 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer