English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

NumPy Array Shape Modification

The shape of an array is the number of elements in each dimension.

Get the shape of the array

Print 2-D array shape:

import numpy as np
arr = np.array([1, 2, 3, 4], [5, 6, 7, 8])
print(arr.shape)

Running Result

(2, 4)

The above example returns (2, 4) 2 dimensions, each dimension having 4 elements.

Using the value of 1,2,3,4 The vector created with 5 arrays, and verify that the value of the last dimension is 4arrays with

import numpy as np
arr = np.array([1, 2, 3, 4:5)
], ndmin=
print(arr)

Running Result

print('shape of array:', arr.shape)1 2 3 4[[[[[
]]]]1, 1, 1, 1, 4)
shape of array : (

What does the shape of a tuple represent?
Each integer at each index indicates the number of elements in the corresponding dimension. 4The index in the example above is 4, and our value is 5 , so we can say that the 4 + 1 elements ( 4 elements.

th) dimension has

Modify array shape

The outermost dimension will have 1-from 2-D reshaped to

D 12 elements' 1-D array to convert to 2-D Array.
The shape of an array is the number of elements in each dimension. By modifying the shape of an array, we can add or remove dimensions or change the number of elements in each dimension. 4 arrays, which in turn contain 3 arrays, each containing

import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
newarr = arr.reshape(4, 3)
print(newarr)

Running Result

[[ 1 2 3]
 [ 4 5 6]
 [ 7 8 9]
 [10 11 12]]

The outermost dimension will have 1-from 3-D reshaped to

D 12 elements' 1-D array to convert to 3-D Array.
The following array with 2 The outermost dimension will have 3 arrays, which in turn contain 2 arrays, each containing

import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
newarr = arr.reshape(2, 3, 2)
print(newarr)

Running Result

[[[ 1 2]
  [ 3 4]
  [ 5 6]]
 [[ 7 8]
  [ 9 10]
  [11 12]]
elements:

Can an array be reshaped into any shape?
Yes, as long as the number of elements required for reshaping is equal in both shapes. 8 elements, but we cannot reshape it to 1We can reshape 2 lines 2D array to 4 D array within 3 elements, but we cannot reshape it to 3 lines 2D array, because this will require 3x3 = 9 elements.

Attempt to reshape an array with 8 elements' 1D array conversion to have elements in each dimension 3 elements' 2D array (will cause an error):

import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
newarr = arr.reshape(3, 3)
print(newarr)

Running Result

Traceback (most recent call last):
  File "test.py", line 5, in

Unknown dimension

You can use an 'unknown' dimension.
This means you do not have to specify an exact number for one of the dimensions in the reshape method.
passing -1 as the value, NumPy will calculate the number for you.

by taking 8 elements' 1D array to convert to 2x2 elements' 3D Array:

import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
newarr = arr.reshape(2, 2, -1)
print(newarr)

Running Result

[[[1 2]
  [3 4]]
 [[5 6]
  [7 8]]
Note:We cannot pass -1 passed to more than one dimension.

Flattening Arrays

Flattening arrays (Flattening the arrays) means converting a multidimensional array to 1D Array.
We can use reshape(-1) to do this.
Convert the array to 1D Array:

import numpy as np
arr = np.array([1, 2, 3], [4, 5, 6])
newarr = arr.reshape(-1)
print(newarr)

Running Result

[1 2 3 4 5 6]

There are many functions that can change the shape of numpy flatten, ravel, and can also rearrange elements rot.90, flip, fliplr, flipud, and other functions belong to the intermediate to advanced part of numpy.