About this question
I am currently working with a multidimensional array by using the NumPy. My doubt is what specific insights I can achieve if I use “ndim” attribute under NumPy.
I am currently working with a multidimensional array by using the NumPy. My doubt is what specific insights I can achieve if I use “ndim” attribute under NumPy.
Log in to share your answer and help other learners.
Log in to answerBest Answer · By JanBask Python Expert
Dhananjay Singh JanBask Expert
Answered on Dec 28, 2023
In the context of Python programming language, using ndim we can find that the “ndim” attribute in NumPy allows you to gain or determine the wide range of dimensions or axes of the arrays of NumPy. It mainly returns an integer which indicates the dimensions of the array.
The ndim attribute is famous for providing a quick way to gain the dimensionality of NumPy array which can ease your task of manipulation of various arrays and even operation related to analysis.
Here is the example given to showcase the use of “ndim” attribute in NumPy:-
Import numpy as np
# Creating NumPy arrays of different dimensions
Array_1d = np.array([1, 2, 3]) # 1-dimensional array
Array_2d = np.array([[1, 2], [3, 4]]) # 2-dimensional array
Array_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) # 3-dimensional array
# Using ndim to determine the dimensions of arrays
Print(“Dimensions of array_1d:”, array_1d.ndim)
Print(“Dimensions of array_2d:”, array_2d.ndim)
Print(“Dimensions of array_3d:”, array_3d.ndim)
Here is the output given of the above process:-
Dimensions of array_1d: 1
Dimensions of array_2d: 2
Dimensions of array_3d: 3
Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.
Most-read guides across JanBask
Common Python interview questions, answered
Guides, tips and career advice on Python from JanBask experts.
Python How to Become a Python Developer: A Step-by-Step Guide for Beginners
Discover how to become a Python Developer via a practical 7-step guide build projects, maintain clean code, practice daily, and…
Python PCEP Certification Guide: Entry-Level Python Programmer Certification
Explore this PCEP certification guide for insights on cost, exam format, passing score, application, training, and study tips.…
Python Top 50+ Python Projects ideas for Beginners to Advanced
Discover innovative Python project ideas, explore advanced Python projects, and find good Python projects for beginners and…
Python How To Comment Out Multiple Lines In Python Like A Pro
Commenting out code is a common practice among programmers. It allows you to temporarily disable parts of your code without…