{ "cells": [ { "cell_type": "markdown", "id": "54cf7c4b", "metadata": {}, "source": [ "# NumPy Tutorial 03: Indexing, Masking, and Reshaping" ] }, { "cell_type": "markdown", "id": "6fa31f23", "metadata": {}, "source": [ "## Download Notebook\n", "\n", "{download}`Download this notebook <03_indexing_slicing_and_reshaping.ipynb>`" ] }, { "cell_type": "code", "execution_count": null, "id": "a22d4443", "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "id": "e41a799f", "metadata": {}, "source": [ "## 1. Basic slicing returns views\n", "\n", "Changes on a slice may affect the original array." ] }, { "cell_type": "code", "execution_count": null, "id": "3e94260f", "metadata": {}, "outputs": [], "source": [ "a = np.arange(10)\n", "s = a[2:7]\n", "s[:] = -1\n", "print('slice:', s)\n", "print('original changed:', a)" ] }, { "cell_type": "markdown", "id": "7e061c54", "metadata": {}, "source": [ "## 2. Fancy indexing returns copies\n", "\n", "Integer-array indexing produces a copy, not a view." ] }, { "cell_type": "code", "execution_count": null, "id": "0c888f93", "metadata": {}, "outputs": [], "source": [ "a = np.arange(10)\n", "picked = a[[1, 3, 5, 7]]\n", "picked[:] = 100\n", "print('picked:', picked)\n", "print('original unchanged:', a)" ] }, { "cell_type": "markdown", "id": "9c2ac082", "metadata": {}, "source": [ "## 3. Boolean masking\n", "\n", "Boolean masks are expressive for filtering and assignment." ] }, { "cell_type": "code", "execution_count": null, "id": "901b2efe", "metadata": {}, "outputs": [], "source": [ "rng = np.random.default_rng(0)\n", "x = rng.normal(size=12)\n", "mask = x > 0\n", "\n", "print('x:', np.round(x, 3))\n", "print('positive values:', np.round(x[mask], 3))\n", "\n", "x2 = x.copy()\n", "x2[x2 < 0] = 0\n", "print('negative clipped to 0:', np.round(x2, 3))" ] }, { "cell_type": "markdown", "id": "5bbce50e", "metadata": {}, "source": [ "## 4. Multi-dimensional indexing\n", "\n", "Use row/column selectors and `:` slices to target blocks." ] }, { "cell_type": "code", "execution_count": null, "id": "506938f2", "metadata": {}, "outputs": [], "source": [ "m = np.arange(1, 17).reshape(4, 4)\n", "print('m:\\n', m)\n", "print('element (2,3):', m[2, 3])\n", "print('second column:', m[:, 1])\n", "print('middle block:\\n', m[1:3, 1:3])" ] }, { "cell_type": "markdown", "id": "d6a7dcb5", "metadata": {}, "source": [ "## 5. Reshape, transpose, and axis semantics\n", "\n", "Axis 0 is rows, axis 1 is columns for 2D arrays." ] }, { "cell_type": "code", "execution_count": null, "id": "afc4f658", "metadata": {}, "outputs": [], "source": [ "x = np.arange(24)\n", "m = x.reshape(2, 3, 4)\n", "\n", "print('shape:', m.shape)\n", "print('sum axis=0 shape:', m.sum(axis=0).shape)\n", "print('sum axis=1 shape:', m.sum(axis=1).shape)\n", "print('sum axis=2 shape:', m.sum(axis=2).shape)" ] }, { "cell_type": "markdown", "id": "97f9da05", "metadata": {}, "source": [ "## 6. Practice\n", "\n", "1. Build a 6x6 matrix from 1 to 36.\n", "2. Extract all even numbers using a boolean mask.\n", "3. Replace diagonal elements with 0." ] } ], "metadata": { "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 5 }