{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "7247d6d7",
            "cell_type": "markdown",
            "source": "<div class='alert alert-warning'>\n\nSciPy's interactive examples with Jupyterlite are experimental and may not always work as expected. Execution of cells containing imports may result in large downloads (up to 60MB of content for the first import from SciPy). Load times when importing from SciPy may take roughly 10-20 seconds. If you notice any problems, feel free to open an [issue](https://github.com/scipy/scipy/issues/new/choose).\n\n</div>",
            "metadata": {}
        },
        {
            "id": "f1c7df6c",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy import ndimage\na = np.zeros((6, 6), dtype=int)\na[2:4, 2:4] = 1\na[4, 4] = 1\na[:2, :3] = 2\na[0, 5] = 3\na",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[2, 2, 2, 0, 0, 3],\n       [2, 2, 2, 0, 0, 0],\n       [0, 0, 1, 1, 0, 0],\n       [0, 0, 1, 1, 0, 0],\n       [0, 0, 0, 0, 1, 0],\n       [0, 0, 0, 0, 0, 0]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "f428d78d",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "val_indices = ndimage.value_indices(a)",
            "outputs": []
        },
        {
            "id": "ef6af64a",
            "cell_type": "markdown",
            "source": "The dictionary `val_indices` will have an entry for each distinct\nvalue in the input array.\n",
            "metadata": {}
        },
        {
            "id": "f5a19dec",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "val_indices.keys()",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "dict_keys([np.int64(0), np.int64(1), np.int64(2), np.int64(3)])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "b7b4e745",
            "cell_type": "markdown",
            "source": "The entry for each value is an index tuple, locating the elements\nwith that value.\n",
            "metadata": {}
        },
        {
            "id": "a29c83f0",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "ndx1 = val_indices[1]\nndx1",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(array([2, 2, 3, 3, 4]), array([2, 3, 2, 3, 4]))"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "ec560f95",
            "cell_type": "markdown",
            "source": "This can be used to index into the original array, or any other\narray with the same shape.\n",
            "metadata": {}
        },
        {
            "id": "00b5ea55",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "a[ndx1]",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([1, 1, 1, 1, 1])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "9ac94466",
            "cell_type": "markdown",
            "source": "If the zeros were to be ignored, then the resulting dictionary\nwould no longer have an entry for zero.\n",
            "metadata": {}
        },
        {
            "id": "2fd6b832",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "val_indices = ndimage.value_indices(a, ignore_value=0)\nval_indices.keys()",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "dict_keys([np.int64(1), np.int64(2), np.int64(3)])"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}