{ "cells": [ { "cell_type": "markdown", "id": "9bdc9aaa", "metadata": {}, "source": [ "# Indexing and slicing\n", "\n", "Indexing is the selection of a subset of your data or individual elements. This is very easy in one-dimensional arrays; they behave similarly to Python lists:" ] }, { "cell_type": "code", "execution_count": 1, "id": "30333f50", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T17:09:02.577164Z", "iopub.status.busy": "2026-05-21T17:09:02.576982Z", "iopub.status.idle": "2026-05-21T17:09:02.610646Z", "shell.execute_reply": "2026-05-21T17:09:02.610332Z", "shell.execute_reply.started": "2026-05-21T17:09:02.577145Z" } }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "id": "7b193366", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T17:09:02.611115Z", "iopub.status.busy": "2026-05-21T17:09:02.611003Z", "iopub.status.idle": "2026-05-21T17:09:02.621792Z", "shell.execute_reply": "2026-05-21T17:09:02.621481Z", "shell.execute_reply.started": "2026-05-21T17:09:02.611106Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 0.4343705 , -0.7714655 , 0.59067828],\n", " [ 0.60518834, -2.01837288, 0.44319161],\n", " [-0.11469401, 0.74668828, -0.16745715],\n", " [-0.83560905, 0.75199224, -0.11023069]])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rng = np.random.default_rng()\n", "data = rng.normal(size=(4, 3))\n", "data" ] }, { "cell_type": "code", "execution_count": 3, "id": "6cd243fc", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T17:09:02.622458Z", "iopub.status.busy": "2026-05-21T17:09:02.622284Z", "iopub.status.idle": "2026-05-21T17:09:02.625046Z", "shell.execute_reply": "2026-05-21T17:09:02.624779Z", "shell.execute_reply.started": "2026-05-21T17:09:02.622425Z" } }, "outputs": [ { "data": { "text/plain": [ "array([-0.83560905, 0.75199224, -0.11023069])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data[3]" ] }, { "cell_type": "code", "execution_count": 4, "id": "f333c3fe", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T17:09:02.625634Z", "iopub.status.busy": "2026-05-21T17:09:02.625491Z", "iopub.status.idle": "2026-05-21T17:09:02.627901Z", "shell.execute_reply": "2026-05-21T17:09:02.627682Z", "shell.execute_reply.started": "2026-05-21T17:09:02.625623Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 0.60518834, -2.01837288, 0.44319161],\n", " [-0.11469401, 0.74668828, -0.16745715]])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data[1:3]" ] }, { "cell_type": "code", "execution_count": 5, "id": "81ffc2e6", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T17:09:02.628234Z", "iopub.status.busy": "2026-05-21T17:09:02.628133Z", "iopub.status.idle": "2026-05-21T17:09:02.630067Z", "shell.execute_reply": "2026-05-21T17:09:02.629812Z", "shell.execute_reply.started": "2026-05-21T17:09:02.628222Z" } }, "outputs": [], "source": [ "data[1:3] = rng.normal(size=(2, 3))" ] }, { "cell_type": "code", "execution_count": 6, "id": "e002d2a0", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T17:09:02.631936Z", "iopub.status.busy": "2026-05-21T17:09:02.631830Z", "iopub.status.idle": "2026-05-21T17:09:02.634186Z", "shell.execute_reply": "2026-05-21T17:09:02.633925Z", "shell.execute_reply.started": "2026-05-21T17:09:02.631928Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 0.4343705 , -0.7714655 , 0.59067828],\n", " [ 1.17091858, 2.06859473, -0.14522314],\n", " [ 0.50547431, -0.96297007, 0.61641233],\n", " [-0.83560905, 0.75199224, -0.11023069]])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data" ] }, { "cell_type": "markdown", "id": "84bfdb46", "metadata": {}, "source": [ "