Numpy: Create Higher Dimensional Arrays using ndmin()

As we know, an array can have any number of dimensions. If you want to create an array of higher dimensions, you can define the number of dimensions by using ndim() arguments.

Example:

import numpy as np
a=np.array([['a','b','c','d','e','f']], ndmin=6)
print(a)
print('number of dimensions :', a.ndim)

Output:

[[[[[['a' 'b' 'c' 'd' 'e' 'f']]]]]]
number of dimensions : 6

In the above example, the output array has 6 elements in the innermost dimension i.e. 6th dim, 1 element in 5th dim i.e. the vector, 4th dim has 1 element i.e. matrix with vector, the 3rd dim has 1 element i.e. 3D array, the 2nd dim has 1 element i.e. 4D array, and 1st dim has 1 element i.e. a 5D array.
ndim() function also return the number of dimensions of an array.

Thu, 09/09/2021 - 23:30

Authored by

Devanshi, is working as a Data Scientist with iVagus. She has expertise in Python, NumPy, Pandas and other data science technologies.