How to change dimension of NumPy array

In this post, we are going to learn how to change dimension of NumPy array. The numpy array dimensional describes the numbers of rows and columns it contains. if we have already a numpy array and we want to change its dimensional.The numpy library some built in functions are used that includes numpy.reshape(),numpy.resize() and attribute numpy.shape.To run this program the numpy library should be installed on our system,its is imported using this code “import numpy as np” in our program.

1. numpy.reshape() to change dimension of NumPy array


The numpy.reshape() function reshapes the array its changing the number of rows and columns or dimensional without changing its data. The newshape of the array should be compatible with the original array. The order argument is used to read elements of array based on index order and placed them into the array using the index order(C, F, A).

An important point to note here is that arguments in reshape should be equal to arrange arguments when they are multiplied. For Example in our example we are passing 8 in arange() so in reshape, we can pass 4,2 or 2,4 or 2,2,2

Syntax

numpy.reshape(arr,newshape,order=C,F,A)

How to change dimension of NumPy array:Python Program


  • We have intilalized an array using np.arange(1,9)
  • Change dimension of NumPy array using order =C
  • Change dimension of NumPy array using order =F
  • Change dimension of NumPy array using order =A
#Python Program to change dimension of NumPy array


import numpy as np

nparag = np.arange(1,9)

print("Intialize numpy array:",nparag)


print('\nreshape order by c:\n',np.reshape(nparag, (4, 2), order='C'))


print('\nreshape order by F:\n',np.reshape(nparag, (4, 2), order='F'))


print('\nreshape order by A:\n',np.reshape(nparag, (4, 2), order='A'))

Output

Intialize numpy array: [1 2 3 4 5 6 7 8]

reshape order by c:
 [[1 2]
 [3 4]
 [5 6]
 [7 8]]

reshape order by F:
 [[1 5]
 [2 6]
 [3 7]
 [4 8]]

reshape order by A:
 [[1 2]
 [3 4]
 [5 6]
 [7 8]]s

2. numpy.resize() to change dimension of NumPy array


The numpy.resize(arr,new_shape) function return an new array with specified shape.If the new array is larger than the original array.in this case, the new array is filled with repeated copies of the given array.

Whereas, array. resize(newshape) new array is filled with zeros instead of repeated copies array.

Syntax

numpy.resize(arr,new_shape)

Python Program to change dimension of NumPy array

  • We have intilalized an array using np.arange(1,9).
  • We have used np.resize() to resize the numPy array.
  • The new array is larger that original array.So new array filled with nparag repeated copy.
#Python Program to change dimension of NumPy array


import numpy as np
nparag = np.arange(1,9)

print("Intialize numpy array:",nparag)


print('\n resize numpy array :\n',np.resize(nparag, (4, 3)))




Output

Intialize numpy array: [1 2 3 4 5 6 7 8] 

resize numpy array :
 [[1 2 3]
 [4 5 6]
 [7 8 1]
 [2 3 4]]

3. numpy.shape to change dimesional of numpy array


The numpy.shape attribute returns a tuple.The elements of tuples give us length of corresponding array dimension.

In this python program example we have intalized numpy array using np.arange(1,9).The numpy array attribute shape is used to change the size of numpy array to (2,2,2)

import numpy as np
nparag = np.arange(1,9)

print("Intialize numpy array:",nparag)

print('Existing array shape:',nparag.shape)

nparag.shape = (2,2,2)
  

print('\n array  after change:\n',nparag)

print('\n array  shape:\n',nparag.shape)


Output

Intialize numpy array: [1 2 3 4 5 6 7 8]
Existing array shape: (8,)

 array  after change:
 [[[1 2]
  [3 4]]

 [[5 6]
  [7 8]]]

 array  shape:
 (2, 2, 2)

Summary

In this post we are going to we have learned how to change dimension of NumPy array using built in function and attribute of numpy library numpy.reshape(),numpy.resize() and attribute numpy.shape.