{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# Numpy Documentaion: https://numpy.org/doc/stable/"
      ],
      "metadata": {
        "id": "td115ZYDm2MH"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "#NumPy Indexing"
      ],
      "metadata": {
        "id": "kmIR7LHSeKG8"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Import Numpy\n",
        "import numpy as np"
      ],
      "metadata": {
        "id": "18mjiYBdg7u8"
      },
      "execution_count": 2,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Problem 1: Integer Indexing\n",
        "\n",
        "\n",
        "\n",
        "```\n",
        "arr = np.array([[10, 20, 30],\n",
        "                [40, 50, 60],\n",
        "                [70, 80, 90]])\n",
        "```\n",
        "\n",
        "- Use Integer indexing to retrieve the value '80'"
      ],
      "metadata": {
        "id": "omb-sIHQeNyX"
      }
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {
        "id": "o5OJGUkZeHCt",
        "outputId": "d2a4549b-6d3b-4819-87ee-f704f2004546",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "np.int64(80)"
            ]
          },
          "metadata": {},
          "execution_count": 3
        }
      ],
      "source": [
        "# Integer index the array, 'arr'\n",
        "arr = np.array([[10, 20, 30],\n",
        "                [40, 50, 60],\n",
        "                [70, 80, 90]])\n",
        "arr[-1][1]"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Problem 2: Indexing with an Array of Indices\n",
        "\n",
        "\n",
        "```\n",
        "arr = np.array([100, 200, 300, 400, 500])\n",
        "indices = np.array([0, 2, 4])\n",
        "```\n",
        "- Use your 'indices' array to extract elements from the array, 'arr'"
      ],
      "metadata": {
        "id": "F2TrltyTelB5"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Index the array 'arr' using the arrray 'indices'\n",
        "arr = np.array([100, 200, 300, 400, 500])\n",
        "indices = np.array([0, 2, 4])\n",
        "\n",
        "\n",
        "arr[indices]"
      ],
      "metadata": {
        "id": "1iukbqdselIa",
        "outputId": "4943fd4f-8c1d-48ec-9d75-ea1bd953378e",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "execution_count": 4,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([100, 300, 500])"
            ]
          },
          "metadata": {},
          "execution_count": 4
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "##Problem 3: Boolean Indexing\n",
        "\n",
        "\n",
        "\n",
        "```\n",
        "arr = np.array([5, 15, 25, 35, 45])\n",
        "```\n",
        "\n",
        "- Use Boolean Indexing to extract all values from 'arr' which are greater than 20"
      ],
      "metadata": {
        "id": "mUs7PrfleXAm"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Generate your boolean array and assign it to the name, 'bool_arr'\n",
        "arr = np.array([5, 15, 25, 35, 45])\n",
        "\n",
        "bool_arr = arr > 20"
      ],
      "metadata": {
        "id": "gLKq-c_1eXG6"
      },
      "execution_count": 5,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Use 'bool_arr' to index the original array 'arr'\n",
        "arr[bool_arr]"
      ],
      "metadata": {
        "id": "HqOIb1gQiHIL",
        "outputId": "e3c4267d-d3a5-4ff3-9bf4-f84b081e854b",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([25, 35, 45])"
            ]
          },
          "metadata": {},
          "execution_count": 6
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Problem 4: Multi-dimensional Fancy Indexing\n",
        "\n",
        "- Generate a 3x3 array with random numbers in the range 1-100\n",
        "\n",
        "\n",
        "```\n",
        "rows = np.array([0, 1, 2])\n",
        "cols = np.array([2, 0, 1])\n",
        "```\n",
        "- Use the rows and cols array to extract the elements: (0,2), (1,0), and (2,1).\n"
      ],
      "metadata": {
        "id": "veGr7dbEerQj"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "rows = np.array([0, 1, 2])\n",
        "cols = np.array([2, 0, 1])"
      ],
      "metadata": {
        "id": "hQM8ZRC0z-Wb"
      },
      "execution_count": 15,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Generate your 2D array\n",
        "my_arr = np.random.randint(1, 101, (3, 3))\n",
        "my_arr"
      ],
      "metadata": {
        "id": "pZLIHmwmerWf",
        "outputId": "ab94a3f3-0101-4732-9447-3107d239ad42",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "execution_count": 14,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[42, 21,  2],\n",
              "       [73, 19, 29],\n",
              "       [96, 55, 40]])"
            ]
          },
          "metadata": {},
          "execution_count": 14
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Use Fancy Indexing to Extract elements : (0,2), (1,0), and (2,1)\n",
        "my_arr[rows, cols]"
      ],
      "metadata": {
        "id": "of0X_7J8fMhl",
        "outputId": "9add0bcd-c85e-4d5c-8399-ac3538123597",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "execution_count": 16,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([ 2, 73, 55])"
            ]
          },
          "metadata": {},
          "execution_count": 16
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Numpy Slicing"
      ],
      "metadata": {
        "id": "DdfF21DDjwEP"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Problem 1: Basic 1D Slicing\n",
        "\n",
        "\n",
        "\n",
        "```\n",
        "arr = np.array([10, 20, 30, 40, 50, 60])\n",
        "```\n",
        "- Use slicing to extract [20, 30, 40] from the array 'arr'\n"
      ],
      "metadata": {
        "id": "debZzVAlj0c4"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "arr = np.array([10, 20, 30, 40, 50, 60])"
      ],
      "metadata": {
        "id": "5FX5zBey0JPE"
      },
      "execution_count": 17,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Slice the array 'arr'\n",
        "arr[1:4]"
      ],
      "metadata": {
        "id": "xXlBFFZWj3mY",
        "outputId": "b48cef58-7973-4f1c-ac02-a79e45dce761",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "execution_count": 18,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([20, 30, 40])"
            ]
          },
          "metadata": {},
          "execution_count": 18
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Problem 2: 2D Row Slicing\n",
        "\n",
        "\n",
        "\n",
        "\n",
        "```\n",
        "arr = np.array([[1, 2, 3],\n",
        "                [4, 5, 6],\n",
        "                [7, 8, 9],\n",
        "                [10, 11, 12]])\n",
        "```\n",
        "- Use slicing to extract the last two rows from arr\n"
      ],
      "metadata": {
        "id": "vHMxp3WEj4Qa"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "arr = np.array([[1, 2, 3],\n",
        "                [4, 5, 6],\n",
        "                [7, 8, 9],\n",
        "                [10, 11, 12]])"
      ],
      "metadata": {
        "id": "0e30Hr4-0OWD"
      },
      "execution_count": 19,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Slice the array 'arr' to extract the last two rows\n",
        "arr[2:, :]"
      ],
      "metadata": {
        "id": "fco4Qxhaj4WO",
        "outputId": "0beb3f66-1b1a-4c2b-f718-acecea0287d8",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "execution_count": 21,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[ 7,  8,  9],\n",
              "       [10, 11, 12]])"
            ]
          },
          "metadata": {},
          "execution_count": 21
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Problem 3: 2D Column Slicing\n",
        "\n",
        "\n",
        "\n",
        "```\n",
        "arr = np.array([[100, 200, 300, 400],\n",
        "                [500, 600, 700, 800]])\n",
        "```\n",
        "- Use column slicing to extract only the middle two columns\n",
        "- Save your slice to a new array called, 'my_slice'\n",
        "- Use a numpy method to calculate the sum of all elements in your array, 'my_slice'\n"
      ],
      "metadata": {
        "id": "0E-K-OSfj4d8"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "arr = np.array([[100, 200, 300, 400],\n",
        "                [500, 600, 700, 800]])"
      ],
      "metadata": {
        "id": "LEwyqxHA0W0y"
      },
      "execution_count": 22,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Extract the middle two columns from both rows in the array 'arr'\n",
        "my_slice = arr[:, 1:3]\n",
        "my_slice"
      ],
      "metadata": {
        "id": "SGFu_p6Vj4jo",
        "outputId": "66e1587b-a394-426e-9c34-0cd55b5c2ecc",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "execution_count": 26,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[200, 300],\n",
              "       [600, 700]])"
            ]
          },
          "metadata": {},
          "execution_count": 26
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Calculate the sum of the elements in your array, 'my_slice'\n",
        "my_slice.sum()"
      ],
      "metadata": {
        "id": "qF4O3ymOlG35",
        "outputId": "3a52c4e9-4cca-4d9d-e1e3-b7a5d0f871df",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "execution_count": 29,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "np.int64(1800)"
            ]
          },
          "metadata": {},
          "execution_count": 29
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Calculate the sum of the elements in your array, 'my_slice'\n",
        "np.sum(my_slice)"
      ],
      "metadata": {
        "id": "VPvHlHcI0mNU",
        "outputId": "ad27e34f-ae95-4964-8f64-066c438f6e05",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "execution_count": 30,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "np.int64(1800)"
            ]
          },
          "metadata": {},
          "execution_count": 30
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Problem 5: Reverse Slicing\n",
        "\n",
        "\n",
        "\n",
        "```\n",
        "arr = np.array([5, 10, 15, 20, 25, 30])\n",
        "```\n",
        "- Use slicing to reverse the array, 'arr'\n",
        "- Save the reveresed array to the variable name, 'rev_arr'\n",
        "- Use Integer indexing to access index 2 of your array, 'rev_arr'\n"
      ],
      "metadata": {
        "id": "oyOTlnNIj4re"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "arr = np.array([5, 10, 15, 20, 25, 30])"
      ],
      "metadata": {
        "id": "xK4JO05g0sSf"
      },
      "execution_count": 31,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Reverse the array, 'arr'\n",
        "inverse_arr = arr[::-1]\n",
        "\n",
        "inverse_arr"
      ],
      "metadata": {
        "id": "YB1oAoWpj4xl",
        "outputId": "8b9d3ff6-4897-43ff-9add-c94c06d46f1a",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "execution_count": 33,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([30, 25, 20, 15, 10,  5])"
            ]
          },
          "metadata": {},
          "execution_count": 33
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Integer index your reversed array to access index 2\n",
        "inverse_arr[2]"
      ],
      "metadata": {
        "id": "qCh6xyJsl254",
        "outputId": "b0c22d52-2adf-479b-c119-9033de01bc96",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "execution_count": 34,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "np.int64(20)"
            ]
          },
          "metadata": {},
          "execution_count": 34
        }
      ]
    }
  ]
}