How to get all 2D diagonals of a 3D NumPy array? Last Updated : 02 Sep, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report Let's see the program for getting all 2D diagonals of a 3D NumPy array. So, for this we are using numpy.diagonal() function of NumPy library. This function return specified diagonals from an n-dimensional array. Syntax: numpy.diagonal(a, axis1, axis2)Parameters: a: represents array from which diagonals has to be taken axis1: represents first axis to be taken for 2-d subarray axis2: represents second axis to be taken for 2-d subarray Return: array of diagonal elements. Now, let's see an example: Example 1: Python3 # Import the numpy package import numpy as np # Create 3D-numpy array # of 4 rows and 4 columns arr = np.arange(3 * 4 * 4).reshape(3, 4, 4) print("Original 3d array:\n", arr) # Create 2D diagonal array diag_arr = np.diagonal(arr, axis1 = 1, axis2 = 2) print("2d diagonal array:\n", diag_arr) Output: Original 3d array: [[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 15]] [[16 17 18 19] [20 21 22 23] [24 25 26 27] [28 29 30 31]] [[32 33 34 35] [36 37 38 39] [40 41 42 43] [44 45 46 47]]] 2d diagonal array: [[ 0 5 10 15] [16 21 26 31] [32 37 42 47]] Example 2: Python3 # Import the numpy package import numpy as np # Create 3D numpy array # of 3 rows and 4 columns arr = np.arange(3 * 3 * 4).reshape(3, 3, 4) print("Original 3d array:\n", arr) # Create 2D diagonal array diag_arr = np.diagonal(arr, axis1 = 1, axis2 = 2) print("2d diagonal array:\n", diag_arr) Output: Original 3d array: [[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] [[12 13 14 15] [16 17 18 19] [20 21 22 23]] [[24 25 26 27] [28 29 30 31] [32 33 34 35]]] 2d diagonal array: [[ 0 5 10] [12 17 22] [24 29 34]] Example 3: Python3 # Import the numpy package import numpy as np # Create 3D numpy array # of 5 rows and 6 columns arr = np.arange(3 * 5 * 6).reshape(3, 5, 6) print("Original 3d array:\n", arr) # Create 2D diagonal array diag_arr = np.diagonal(arr, axis1 = 1, axis2 = 2) print("2d diagonal array:\n", diag_arr) Output: Original 3d array: [[[ 0 1 2 3 4 5] [ 6 7 8 9 10 11] [12 13 14 15 16 17] [18 19 20 21 22 23] [24 25 26 27 28 29]] [[30 31 32 33 34 35] [36 37 38 39 40 41] [42 43 44 45 46 47] [48 49 50 51 52 53] [54 55 56 57 58 59]] [[60 61 62 63 64 65] [66 67 68 69 70 71] [72 73 74 75 76 77] [78 79 80 81 82 83] [84 85 86 87 88 89]]] 2d diagonal array: [[ 0 7 14 21 28] [30 37 44 51 58] [60 67 74 81 88]] Comment More infoAdvertise with us Next Article How to get all 2D diagonals of a 3D NumPy array? U utkarsh_kumar Follow Improve Article Tags : Python Python-numpy Python numpy-arrayManipulation Practice Tags : python Similar Reads Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo 10 min read Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth 15+ min read Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p 11 min read Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list 10 min read Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test 9 min read Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co 11 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien 3 min read Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes 9 min read Like