{
  "__type": "IngestedDoc",
  "__tag": 4010,
  "_content": {},
  "_ordered_sections": [],
  "item_file": null,
  "item_line": null,
  "item_type": null,
  "aliases": [],
  "example_section_data": {
    "__type": "Section",
    "__tag": 4015,
    "children": [],
    "title": [],
    "level": 0,
    "target": null
  },
  "see_also": [],
  "signature": null,
  "references": null,
  "qa": "user:how-to-index",
  "arbitrary": [
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Admonition",
          "__tag": 4056,
          "kind": "seealso",
          "base_type": "note",
          "children": [
            {
              "__type": "AdmonitionTitle",
              "__tag": 4055,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "seealso :ref:`basics.indexing`"
                }
              ]
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "This page tackles common examples. For an in-depth look into indexing, refer to "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "basics.indexing",
              "reference": {
                "__type": "LocalRef",
                "__tag": 4022,
                "kind": "docs",
                "path": "user:basics.indexing"
              },
              "kind": "exists"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "."
            }
          ]
        }
      ],
      "title": [
        {
          "__type": "Text",
          "__tag": 4046,
          "value": "How to index "
        },
        {
          "__type": "InlineRole",
          "__tag": 4003,
          "value": "ndarrays <.ndarray>",
          "domain": null,
          "role": "class",
          "inventory": null
        }
      ],
      "level": 0,
      "target": "how-to-index"
    },
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Use "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "basic-indexing",
              "reference": {
                "__type": "LocalRef",
                "__tag": 4022,
                "kind": "docs",
                "path": "user:basics.indexing"
              },
              "kind": "exists"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " features like "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "slicing-and-striding",
              "reference": {
                "__type": "LocalRef",
                "__tag": 4022,
                "kind": "docs",
                "path": "user:basics.indexing"
              },
              "kind": "exists"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", and "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "dimensional-indexing-tools",
              "reference": {
                "__type": "LocalRef",
                "__tag": 4022,
                "kind": "docs",
                "path": "user:basics.indexing"
              },
              "kind": "exists"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "."
            }
          ]
        },
        {
          "__type": "Blockquote",
          "__tag": 4059,
          "children": [
            {
              "__type": "Code",
              "__tag": 4050,
              "value": ">>> a = np.arange(30).reshape(2, 3, 5)\n>>> a\narray([[[ 0,  1,  2,  3,  4],\n        [ 5,  6,  7,  8,  9],\n        [10, 11, 12, 13, 14]],\n<BLANKLINE>\n        [[15, 16, 17, 18, 19],\n        [20, 21, 22, 23, 24],\n        [25, 26, 27, 28, 29]]])\n>>> a[0, 2, :]\narray([10, 11, 12, 13, 14])\n>>> a[0, :, 3]\narray([ 3,  8, 13])",
              "execution_status": null
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Note that the output from indexing operations can have different shape from the original object. To preserve the original dimensions after indexing, you can use "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "newaxis",
              "domain": null,
              "role": "func",
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ". To use other such tools, refer to "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "dimensional-indexing-tools",
              "reference": {
                "__type": "LocalRef",
                "__tag": 4022,
                "kind": "docs",
                "path": "user:basics.indexing"
              },
              "kind": "exists"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "."
            }
          ]
        },
        {
          "__type": "Blockquote",
          "__tag": 4059,
          "children": [
            {
              "__type": "Code",
              "__tag": 4050,
              "value": ">>> a[0, :, 3].shape\n(3,)\n>>> a[0, :, 3, np.newaxis].shape\n(3, 1)\n>>> a[0, :, 3, np.newaxis, np.newaxis].shape\n(3, 1, 1)",
              "execution_status": null
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Variables can also be used to index      "
            }
          ]
        },
        {
          "__type": "Code",
          "__tag": 4050,
          "value": ">>> y = 0\n>>> a[y, :, y+3]\narray([ 3,  8, 13])",
          "execution_status": null
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Refer to "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "dealing-with-variable-indices",
              "reference": {
                "__type": "LocalRef",
                "__tag": 4022,
                "kind": "docs",
                "path": "user:basics.indexing"
              },
              "kind": "exists"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " to see how to use "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "python:slice"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " and "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "Ellipsis",
              "domain": "py",
              "role": "data",
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " in your index variables."
            }
          ]
        }
      ],
      "title": [
        {
          "__type": "Text",
          "__tag": 4046,
          "value": "Access specific/arbitrary rows and columns"
        }
      ],
      "level": 1,
      "target": null
    },
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "To index columns, you have to index the last axis. Use "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "dimensional-indexing-tools",
              "reference": {
                "__type": "LocalRef",
                "__tag": 4022,
                "kind": "docs",
                "path": "user:basics.indexing"
              },
              "kind": "exists"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " to get the desired number of dimensions      "
            }
          ]
        },
        {
          "__type": "Code",
          "__tag": 4050,
          "value": ">>> a = np.arange(24).reshape(2, 3, 4)\n>>> a\narray([[[ 0,  1,  2,  3],\n        [ 4,  5,  6,  7],\n        [ 8,  9, 10, 11]],\n<BLANKLINE>\n       [[12, 13, 14, 15],\n        [16, 17, 18, 19],\n        [20, 21, 22, 23]]])\n>>> a[..., 3]\narray([[ 3,  7, 11],\n       [15, 19, 23]])",
          "execution_status": null
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "To index specific elements in each column, make use of "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "advanced-indexing",
              "reference": {
                "__type": "LocalRef",
                "__tag": 4022,
                "kind": "docs",
                "path": "user:basics.indexing"
              },
              "kind": "exists"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " as below      "
            }
          ]
        },
        {
          "__type": "Code",
          "__tag": 4050,
          "value": ">>> arr = np.arange(3*4).reshape(3, 4)\n>>> arr\narray([[ 0,  1,  2,  3],\n       [ 4,  5,  6,  7],\n       [ 8,  9, 10, 11]])\n>>> column_indices = [[1, 3], [0, 2], [2, 2]]\n>>> np.arange(arr.shape[0])\narray([0, 1, 2])\n>>> row_indices = np.arange(arr.shape[0])[:, np.newaxis]\n>>> row_indices\narray([[0],\n       [1],\n       [2]])",
          "execution_status": null
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Use the "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "row_indices"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " and "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "column_indices"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " for advanced indexing      "
            }
          ]
        },
        {
          "__type": "Code",
          "__tag": 4050,
          "value": ">>> arr[row_indices, column_indices]\narray([[ 1,  3],\n       [ 4,  6],\n       [10, 10]])",
          "execution_status": null
        }
      ],
      "title": [
        {
          "__type": "Text",
          "__tag": 4046,
          "value": "Index columns"
        }
      ],
      "level": 2,
      "target": null
    },
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Use "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "take",
              "domain": null,
              "role": "meth",
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ". See also "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "take_along_axis",
              "domain": null,
              "role": "meth",
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " and "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "put_along_axis",
              "domain": null,
              "role": "meth",
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "."
            }
          ]
        },
        {
          "__type": "Blockquote",
          "__tag": 4059,
          "children": [
            {
              "__type": "Code",
              "__tag": 4050,
              "value": ">>> a = np.arange(30).reshape(2, 3, 5)\n>>> a\narray([[[ 0,  1,  2,  3,  4],\n        [ 5,  6,  7,  8,  9],\n        [10, 11, 12, 13, 14]],\n<BLANKLINE>\n        [[15, 16, 17, 18, 19],\n        [20, 21, 22, 23, 24],\n        [25, 26, 27, 28, 29]]])\n>>> np.take(a, [2, 3], axis=2)\narray([[[ 2,  3],\n        [ 7,  8],\n        [12, 13]],\n<BLANKLINE>\n        [[17, 18],\n        [22, 23],\n        [27, 28]]])\n>>> np.take(a, [2], axis=1)\narray([[[10, 11, 12, 13, 14]],\n<BLANKLINE>\n        [[25, 26, 27, 28, 29]]])",
              "execution_status": null
            }
          ]
        }
      ],
      "title": [
        {
          "__type": "Text",
          "__tag": 4046,
          "value": "Index along a specific axis"
        }
      ],
      "level": 2,
      "target": null
    },
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Use "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "slicing-and-striding",
              "reference": {
                "__type": "LocalRef",
                "__tag": 4022,
                "kind": "docs",
                "path": "user:basics.indexing"
              },
              "kind": "exists"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " to access chunks of a large array      "
            }
          ]
        },
        {
          "__type": "Code",
          "__tag": 4050,
          "value": ">>> a = np.arange(100).reshape(10, 10)\n>>> a\narray([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9],\n        [10, 11, 12, 13, 14, 15, 16, 17, 18, 19],\n        [20, 21, 22, 23, 24, 25, 26, 27, 28, 29],\n        [30, 31, 32, 33, 34, 35, 36, 37, 38, 39],\n        [40, 41, 42, 43, 44, 45, 46, 47, 48, 49],\n        [50, 51, 52, 53, 54, 55, 56, 57, 58, 59],\n        [60, 61, 62, 63, 64, 65, 66, 67, 68, 69],\n        [70, 71, 72, 73, 74, 75, 76, 77, 78, 79],\n        [80, 81, 82, 83, 84, 85, 86, 87, 88, 89],\n        [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]])\n>>> a[2:5, 2:5]\narray([[22, 23, 24],\n       [32, 33, 34],\n       [42, 43, 44]])\n>>> a[2:5, 1:3]\narray([[21, 22],\n       [31, 32],\n       [41, 42]])\n>>> a[:5, :5]\narray([[ 0,  1,  2,  3,  4],\n       [10, 11, 12, 13, 14],\n       [20, 21, 22, 23, 24],\n       [30, 31, 32, 33, 34],\n       [40, 41, 42, 43, 44]])",
          "execution_status": null
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "The same thing can be done with advanced indexing in a slightly more complex way. Remember that "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "advanced indexing creates a copy",
              "reference": {
                "__type": "LocalRef",
                "__tag": 4022,
                "kind": "docs",
                "path": "user:basics.copies"
              },
              "kind": "exists"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ":      "
            }
          ]
        },
        {
          "__type": "Code",
          "__tag": 4050,
          "value": ">>> a[np.arange(5)[:, None], np.arange(5)[None, :]]\narray([[ 0,  1,  2,  3,  4],\n       [10, 11, 12, 13, 14],\n       [20, 21, 22, 23, 24],\n       [30, 31, 32, 33, 34],\n       [40, 41, 42, 43, 44]])",
          "execution_status": null
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "You can also use "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "mgrid",
              "domain": null,
              "role": "meth",
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " to generate indices      "
            }
          ]
        },
        {
          "__type": "Code",
          "__tag": 4050,
          "value": ">>> indices = np.mgrid[0:6:2]\n>>> indices\narray([0, 2, 4])\n>>> a[:, indices]\narray([[ 0,  2,  4],\n       [10, 12, 14],\n       [20, 22, 24],\n       [30, 32, 34],\n       [40, 42, 44],\n       [50, 52, 54],\n       [60, 62, 64],\n       [70, 72, 74],\n       [80, 82, 84],\n       [90, 92, 94]])",
          "execution_status": null
        }
      ],
      "title": [
        {
          "__type": "Text",
          "__tag": 4046,
          "value": "Create subsets of larger matrices"
        }
      ],
      "level": 1,
      "target": null
    },
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [
        {
          "__type": "Text",
          "__tag": 4046,
          "value": "Filter values"
        }
      ],
      "level": 1,
      "target": null
    },
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Use "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "nonzero",
              "domain": null,
              "role": "meth",
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " to get a tuple of array indices of non-zero elements  corresponding to every dimension      "
            }
          ]
        },
        {
          "__type": "Code",
          "__tag": 4050,
          "value": ">>> z = np.array([[1, 2, 3, 0], [0, 0, 5, 3], [4, 6, 0, 0]])\n>>> z\narray([[1, 2, 3, 0],\n       [0, 0, 5, 3],\n       [4, 6, 0, 0]])\n>>> np.nonzero(z)\n(array([0, 0, 0, 1, 1, 2, 2]), array([0, 1, 2, 2, 3, 0, 1]))",
          "execution_status": null
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Use "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "flatnonzero",
              "domain": null,
              "role": "meth",
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " to fetch indices of elements that are non-zero in the flattened version of the ndarray   "
            }
          ]
        },
        {
          "__type": "Code",
          "__tag": 4050,
          "value": " >>> np.flatnonzero(z)\n\tarray([0, 1, 2, 6, 7, 8, 9])",
          "execution_status": null
        }
      ],
      "title": [
        {
          "__type": "Text",
          "__tag": 4046,
          "value": "Non-zero elements"
        }
      ],
      "level": 2,
      "target": null
    },
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Use "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "where",
              "domain": null,
              "role": "meth",
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " to generate indices based on conditions and then use "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "advanced-indexing",
              "reference": {
                "__type": "LocalRef",
                "__tag": 4022,
                "kind": "docs",
                "path": "user:basics.indexing"
              },
              "kind": "exists"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "."
            }
          ]
        },
        {
          "__type": "Blockquote",
          "__tag": 4059,
          "children": [
            {
              "__type": "Code",
              "__tag": 4050,
              "value": ">>> a = np.arange(30).reshape(2, 3, 5)\n>>> indices = np.where(a % 2 == 0)\n>>> indices\n(array([0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1]), \narray([0, 0, 0, 1, 1, 2, 2, 2, 0, 0, 1, 1, 1, 2, 2]), \narray([0, 2, 4, 1, 3, 0, 2, 4, 1, 3, 0, 2, 4, 1, 3]))\n>>> a[indices]\narray([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28])",
              "execution_status": null
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Or, use "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "boolean-indexing",
              "reference": {
                "__type": "LocalRef",
                "__tag": 4022,
                "kind": "docs",
                "path": "user:basics.indexing"
              },
              "kind": "exists"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ":      "
            }
          ]
        },
        {
          "__type": "Code",
          "__tag": 4050,
          "value": ">>> a > 14\narray([[[False, False, False, False, False],\n        [False, False, False, False, False],\n        [False, False, False, False, False]],\n<BLANKLINE>\n       [[ True,  True,  True,  True,  True],\n        [ True,  True,  True,  True,  True],\n        [ True,  True,  True,  True,  True]]])\n>>> a[a > 14]\narray([15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29])",
          "execution_status": null
        }
      ],
      "title": [
        {
          "__type": "Text",
          "__tag": 4046,
          "value": "Arbitrary conditions"
        }
      ],
      "level": 2,
      "target": null
    },
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Use assignment with filtering to replace desired values      "
            }
          ]
        },
        {
          "__type": "Code",
          "__tag": 4050,
          "value": ">>> p = np.arange(-10, 10).reshape(2, 2, 5)\n>>> p\narray([[[-10,  -9,  -8,  -7,  -6],\n        [ -5,  -4,  -3,  -2,  -1]],\n<BLANKLINE>\n       [[  0,   1,   2,   3,   4],\n        [  5,   6,   7,   8,   9]]])\n>>> q = p < 0\n>>> q\narray([[[ True,  True,  True,  True,  True],\n        [ True,  True,  True,  True,  True]],\n<BLANKLINE>\n       [[False, False, False, False, False],\n        [False, False, False, False, False]]])\n>>> p[q] = 0\n>>> p\narray([[[0, 0, 0, 0, 0],\n        [0, 0, 0, 0, 0]],\n<BLANKLINE>\n       [[0, 1, 2, 3, 4],\n        [5, 6, 7, 8, 9]]])",
          "execution_status": null
        }
      ],
      "title": [
        {
          "__type": "Text",
          "__tag": 4046,
          "value": "Replace values after filtering"
        }
      ],
      "level": 2,
      "target": null
    },
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Use "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "argmax",
              "domain": null,
              "role": "meth",
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " and "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "argmin",
              "domain": null,
              "role": "meth",
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ":      "
            }
          ]
        },
        {
          "__type": "Code",
          "__tag": 4050,
          "value": ">>> a = np.arange(30).reshape(2, 3, 5)\n>>> np.argmax(a)\n29\n>>> np.argmin(a)\n0",
          "execution_status": null
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Use the "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "axis"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " keyword to get the indices of maximum and minimum values along a specific axis      "
            }
          ]
        },
        {
          "__type": "Code",
          "__tag": 4050,
          "value": ">>> np.argmax(a, axis=0)\narray([[1, 1, 1, 1, 1],\n       [1, 1, 1, 1, 1],\n       [1, 1, 1, 1, 1]])\n>>> np.argmax(a, axis=1)\narray([[2, 2, 2, 2, 2],\n       [2, 2, 2, 2, 2]])\n>>> np.argmax(a, axis=2)\narray([[4, 4, 4],\n       [4, 4, 4]])\n<BLANKLINE>\n>>> np.argmin(a, axis=1)\narray([[0, 0, 0, 0, 0],\n       [0, 0, 0, 0, 0]])\n>>> np.argmin(a, axis=2)\narray([[0, 0, 0],\n       [0, 0, 0]])",
          "execution_status": null
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Set "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "keepdims"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " to "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "True"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " to keep the axes which are reduced in the result as dimensions with size one      "
            }
          ]
        },
        {
          "__type": "Code",
          "__tag": 4050,
          "value": ">>> np.argmin(a, axis=2, keepdims=True)\narray([[[0],\n        [0],\n        [0]],\n<BLANKLINE>\n       [[0],\n        [0],\n        [0]]])\n>>> np.argmax(a, axis=1, keepdims=True)\narray([[[2, 2, 2, 2, 2]],\n<BLANKLINE>\n       [[2, 2, 2, 2, 2]]])",
          "execution_status": null
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "To get the indices of each maximum or minimum value for each (N-1)-dimensional array in an N-dimensional array, use "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "reshape",
              "domain": null,
              "role": "meth",
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " to reshape the array to a 2D array, apply "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "argmax",
              "domain": null,
              "role": "meth",
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " or "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "argmin",
              "domain": null,
              "role": "meth",
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " along "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "axis=1"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " and use "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "unravel_index",
              "domain": null,
              "role": "meth",
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " to recover the index of the values per slice      "
            }
          ]
        },
        {
          "__type": "Code",
          "__tag": 4050,
          "value": ">>> x = np.arange(2*2*3).reshape(2, 2, 3) % 7  # 3D example array\n>>> x\narray([[[0, 1, 2],\n        [3, 4, 5]],\n<BLANKLINE>\n       [[6, 0, 1],\n        [2, 3, 4]]])\n>>> x_2d = np.reshape(x, (x.shape[0], -1))\n>>> indices_2d = np.argmax(x_2d, axis=1)\n>>> indices_2d\narray([5, 0])\n>>> np.unravel_index(indices_2d, x.shape[1:])\n(array([1, 0]), array([2, 0]))",
          "execution_status": null
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "The first array returned contains the indices along axis 1 in the original array, the second array contains the indices along axis 2. The highest value in "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "x[0]"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " is therefore "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "x[0, 1, 2]"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "."
            }
          ]
        }
      ],
      "title": [
        {
          "__type": "Text",
          "__tag": 4046,
          "value": "Fetch indices of max/min values"
        }
      ],
      "level": 1,
      "target": null
    },
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "It must be kept in mind that basic indexing produces "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "views <view>"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " and advanced indexing produces "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "copies <copy>"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", which are computationally less efficient. Hence, you should take care to use basic indexing wherever possible instead of advanced indexing."
            }
          ]
        }
      ],
      "title": [
        {
          "__type": "Text",
          "__tag": 4046,
          "value": "Index the same ndarray multiple times efficiently"
        }
      ],
      "level": 1,
      "target": null
    },
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Nicolas Rougier's "
            },
            {
              "__type": "Link",
              "__tag": 4049,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "100 NumPy exercises"
                }
              ],
              "url": "https://github.com/rougier/numpy-100",
              "title": ""
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " provide a good insight into how indexing is combined with other operations. Exercises "
            },
            {
              "__type": "Link",
              "__tag": 4049,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "6"
                }
              ],
              "url": "https://github.com/rougier/numpy-100/blob/master/100_Numpy_exercises_with_solutions.md#6-create-a-null-vector-of-size-10-but-the-fifth-value-which-is-1-",
              "title": ""
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", "
            },
            {
              "__type": "Link",
              "__tag": 4049,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "8"
                }
              ],
              "url": "https://github.com/rougier/numpy-100/blob/master/100_Numpy_exercises_with_solutions.md#8-reverse-a-vector-first-element-becomes-last-",
              "title": ""
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", "
            },
            {
              "__type": "Link",
              "__tag": 4049,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "10"
                }
              ],
              "url": "https://github.com/rougier/numpy-100/blob/master/100_Numpy_exercises_with_solutions.md#10-find-indices-of-non-zero-elements-from-120040-",
              "title": ""
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", "
            },
            {
              "__type": "Link",
              "__tag": 4049,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "15"
                }
              ],
              "url": "https://github.com/rougier/numpy-100/blob/master/100_Numpy_exercises_with_solutions.md#15-create-a-2d-array-with-1-on-the-border-and-0-inside-",
              "title": ""
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", "
            },
            {
              "__type": "Link",
              "__tag": 4049,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "16"
                }
              ],
              "url": "https://github.com/rougier/numpy-100/blob/master/100_Numpy_exercises_with_solutions.md#16-how-to-add-a-border-filled-with-0s-around-an-existing-array-",
              "title": ""
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", "
            },
            {
              "__type": "Link",
              "__tag": 4049,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "19"
                }
              ],
              "url": "https://github.com/rougier/numpy-100/blob/master/100_Numpy_exercises_with_solutions.md#19-create-a-8x8-matrix-and-fill-it-with-a-checkerboard-pattern-",
              "title": ""
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", "
            },
            {
              "__type": "Link",
              "__tag": 4049,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "20"
                }
              ],
              "url": "https://github.com/rougier/numpy-100/blob/master/100_Numpy_exercises_with_solutions.md#20-consider-a-678-shape-array-what-is-the-index-xyz-of-the-100th-element-",
              "title": ""
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", "
            },
            {
              "__type": "Link",
              "__tag": 4049,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "45"
                }
              ],
              "url": "https://github.com/rougier/numpy-100/blob/master/100_Numpy_exercises_with_solutions.md#45-create-random-vector-of-size-10-and-replace-the-maximum-value-by-0-",
              "title": ""
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", "
            },
            {
              "__type": "Link",
              "__tag": 4049,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "59"
                }
              ],
              "url": "https://github.com/rougier/numpy-100/blob/master/100_Numpy_exercises_with_solutions.md#59-how-to-sort-an-array-by-the-nth-column-",
              "title": ""
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", "
            },
            {
              "__type": "Link",
              "__tag": 4049,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "64"
                }
              ],
              "url": "https://github.com/rougier/numpy-100/blob/master/100_Numpy_exercises_with_solutions.md#64-consider-a-given-vector-how-to-add-1-to-each-element-indexed-by-a-second-vector-be-careful-with-repeated-indices-",
              "title": ""
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", "
            },
            {
              "__type": "Link",
              "__tag": 4049,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "65"
                }
              ],
              "url": "https://github.com/rougier/numpy-100/blob/master/100_Numpy_exercises_with_solutions.md#65-how-to-accumulate-elements-of-a-vector-x-to-an-array-f-based-on-an-index-list-i-",
              "title": ""
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", "
            },
            {
              "__type": "Link",
              "__tag": 4049,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "70"
                }
              ],
              "url": "https://github.com/rougier/numpy-100/blob/master/100_Numpy_exercises_with_solutions.md#70-consider-the-vector-1-2-3-4-5-how-to-build-a-new-vector-with-3-consecutive-zeros-interleaved-between-each-value-",
              "title": ""
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", "
            },
            {
              "__type": "Link",
              "__tag": 4049,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "71"
                }
              ],
              "url": "https://github.com/rougier/numpy-100/blob/master/100_Numpy_exercises_with_solutions.md#71-consider-an-array-of-dimension-553-how-to-mulitply-it-by-an-array-with-dimensions-55-",
              "title": ""
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", "
            },
            {
              "__type": "Link",
              "__tag": 4049,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "72"
                }
              ],
              "url": "https://github.com/rougier/numpy-100/blob/master/100_Numpy_exercises_with_solutions.md#72-how-to-swap-two-rows-of-an-array-",
              "title": ""
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", "
            },
            {
              "__type": "Link",
              "__tag": 4049,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "76"
                }
              ],
              "url": "https://github.com/rougier/numpy-100/blob/master/100_Numpy_exercises_with_solutions.md#76-consider-a-one-dimensional-array-z-build-a-two-dimensional-array-whose-first-row-is-z0z1z2-and-each-subsequent-row-is--shifted-by-1-last-row-should-be-z-3z-2z-1-",
              "title": ""
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", "
            },
            {
              "__type": "Link",
              "__tag": 4049,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "80"
                }
              ],
              "url": "https://github.com/rougier/numpy-100/blob/master/100_Numpy_exercises_with_solutions.md#80-consider-an-arbitrary-array-write-a-function-that-extract-a-subpart-with-a-fixed-shape-and-centered-on-a-given-element-pad-with-a-fill-value-when-necessary-",
              "title": ""
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", "
            },
            {
              "__type": "Link",
              "__tag": 4049,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "81"
                }
              ],
              "url": "https://github.com/rougier/numpy-100/blob/master/100_Numpy_exercises_with_solutions.md#81-consider-an-array-z--1234567891011121314-how-to-generate-an-array-r--1234-2345-3456--11121314-",
              "title": ""
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", "
            },
            {
              "__type": "Link",
              "__tag": 4049,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "84"
                }
              ],
              "url": "https://github.com/rougier/numpy-100/blob/master/100_Numpy_exercises_with_solutions.md#84-extract-all-the-contiguous-3x3-blocks-from-a-random-10x10-matrix-",
              "title": ""
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", "
            },
            {
              "__type": "Link",
              "__tag": 4049,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "87"
                }
              ],
              "url": "https://github.com/rougier/numpy-100/blob/master/100_Numpy_exercises_with_solutions.md#87-consider-a-16x16-array-how-to-get-the-block-sum-block-size-is-4x4-",
              "title": ""
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", "
            },
            {
              "__type": "Link",
              "__tag": 4049,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "90"
                }
              ],
              "url": "https://github.com/rougier/numpy-100/blob/master/100_Numpy_exercises_with_solutions.md#90-given-an-arbitrary-number-of-vectors-build-the-cartesian-product-every-combinations-of-every-item-",
              "title": ""
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", "
            },
            {
              "__type": "Link",
              "__tag": 4049,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "93"
                }
              ],
              "url": "https://github.com/rougier/numpy-100/blob/master/100_Numpy_exercises_with_solutions.md#93-consider-two-arrays-a-and-b-of-shape-83-and-22-how-to-find-rows-of-a-that-contain-elements-of-each-row-of-b-regardless-of-the-order-of-the-elements-in-b-",
              "title": ""
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", "
            },
            {
              "__type": "Link",
              "__tag": 4049,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "94"
                }
              ],
              "url": "https://github.com/rougier/numpy-100/blob/master/100_Numpy_exercises_with_solutions.md#94-considering-a-10x3-matrix-extract-rows-with-unequal-values-eg-223-",
              "title": ""
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " are specially focused on indexing."
            }
          ]
        },
        {
          "__type": "Target",
          "__tag": 4061,
          "label": "6"
        },
        {
          "__type": "Target",
          "__tag": 4061,
          "label": "8"
        },
        {
          "__type": "Target",
          "__tag": 4061,
          "label": "10"
        },
        {
          "__type": "Target",
          "__tag": 4061,
          "label": "15"
        },
        {
          "__type": "Target",
          "__tag": 4061,
          "label": "16"
        },
        {
          "__type": "Target",
          "__tag": 4061,
          "label": "19"
        },
        {
          "__type": "Target",
          "__tag": 4061,
          "label": "20"
        },
        {
          "__type": "Target",
          "__tag": 4061,
          "label": "45"
        },
        {
          "__type": "Target",
          "__tag": 4061,
          "label": "59"
        },
        {
          "__type": "Target",
          "__tag": 4061,
          "label": "64"
        },
        {
          "__type": "Target",
          "__tag": 4061,
          "label": "65"
        },
        {
          "__type": "Target",
          "__tag": 4061,
          "label": "70"
        },
        {
          "__type": "Target",
          "__tag": 4061,
          "label": "71"
        },
        {
          "__type": "Target",
          "__tag": 4061,
          "label": "72"
        },
        {
          "__type": "Target",
          "__tag": 4061,
          "label": "76"
        },
        {
          "__type": "Target",
          "__tag": 4061,
          "label": "80"
        },
        {
          "__type": "Target",
          "__tag": 4061,
          "label": "81"
        },
        {
          "__type": "Target",
          "__tag": 4061,
          "label": "84"
        },
        {
          "__type": "Target",
          "__tag": 4061,
          "label": "87"
        },
        {
          "__type": "Target",
          "__tag": 4061,
          "label": "90"
        },
        {
          "__type": "Target",
          "__tag": 4061,
          "label": "93"
        },
        {
          "__type": "Target",
          "__tag": 4061,
          "label": "94"
        }
      ],
      "title": [
        {
          "__type": "Text",
          "__tag": 4046,
          "value": "Further reading"
        }
      ],
      "level": 1,
      "target": null
    }
  ],
  "local_refs": []
}