{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Iterating Over Arrays & Array-Traversal Order\n", "In this section, you will learn:\n", "\n", "- About NumPy's functions for iterating over an array\n", "- That there is more than one valid way for NumPy to perform this operation, which amounts to how NumPy traverses a multidimensional array.\n", "- The row-major array traversal methodology, which is utilized by NumPy by default." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "NumPy provides valuable tools for iterating over any array, such that each element can be visited in the array, regardless of the array's shape. For example, recall that Python's built-in `enumerate` function permits us to produce each item in an iterable, along with its index of iteration:\n", "\n", "```python\n", "# enumerating the items in an iterable\n", ">>> [i for i in enumerate(\"abcdef\")]\n", "[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e'), (5, 'f')]\n", "```\n", "\n", "Similarly, NumPy provides the [ndenumerate](https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndenumerate.html#numpy.ndenumerate) function, which enumerates each element in an N-dimensional array, specifying the N-dimensional index for each element.\n", "```python\n", "# Demonstrating `np.ndenumerate`.\n", "# A shape-(2, 2, 3) array\n", ">>> x = np.array([[[ 25, 6, -35],\n", "... [ 9, -40, -29]],\n", "... \n", "... [[ -8, 2, -37],\n", "... [ 17, 10, -45]]])\n", "\n", ">>> [i for i in np.ndenumerate(x)]\n", "[((0, 0, 0), 25),\n", " ((0, 0, 1), 6),\n", " ((0, 0, 2), -35),\n", " ((0, 1, 0), 9),\n", " ((0, 1, 1), -40),\n", " ((0, 1, 2), -29),\n", " ((1, 0, 0), -8),\n", " ((1, 0, 1), 2),\n", " ((1, 0, 2), -37),\n", " ((1, 1, 0), 17),\n", " ((1, 1, 1), 10),\n", " ((1, 1, 2), -45)]\n", "```\n", "\n", "See that each triplet of integers specifies the index for the corresponding array element:\n", "\n", "```python\n", ">>> x[0, 0, 0]\n", "25\n", "\n", ">>> x[0, 0, 1]\n", "6\n", "\n", ">>> x[0, 0, 2]\n", "-35\n", "\n", ">>> x[0, 1, 0]\n", "9\n", "```\n", "\n", "See [the official NumPy documentation](https://docs.scipy.org/doc/numpy/reference/routines.indexing.html#iterating-over-arrays) for a complete listing of functions that facilitate iterating over arrays. The official documentation also provides [a detailed treatment of array iteration](https://docs.scipy.org/doc/numpy/reference/arrays.nditer.html#iterating-over-arrays), which is far more detailed than is warranted here. Next, we must discuss the default ordering that NumPy uses when traversing a N-dimensional array." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "## How to Traverse an Array: Row-major (C) vs Column-major (F) Traversal Ordering\n", "Note the order in which `np.ndenumerate` iterated over `x`. It first traversed the columns within row-0 of sheet-0 of `x`, and then it traversed the columns within the row-1 of sheet-0, and so on. What is special about this traversal order? Why, for instance, didn't it traverse the rows within a given column instead? We can also see that there is not a unique ordering for a `reshape` function to adhere to. For example, the following reshape operation could sensibly return either of the following results:\n", "```\n", " array([0, 1, 2, 3, 4, 5]).reshape(3, 2) --> array([[0, 1, 2], or array([[0, 2, 4],\n", " [3, 4, 5]]) [1, 3, 5]])\n", "```\n", "\n", "Both arrays are of the appropriate shape and preserve the ordering of the original sequence of numbers, depending on how you traverse them. The left array preserves the ordering of the original data if you traverse the columns within a row, and then proceed to the next row. This is known as **row-major** ordering. The array on the right preserves the ordering if you traverse the rows within a given column, and then transition to the next column. This is thus referred to as **column-major** ordering. One ordering is not inherently better than the other. That being said, *NumPy always defaults to row-major ordering whenever one of its functions involves array traversal*.\n", "\n", "