{ "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": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "id": "7b193366", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0.84744338, 0.72444063, -0.1683032 ],\n", " [-0.35206446, 1.2773733 , 0.42331498],\n", " [-0.63538581, 0.28763612, 1.70806247],\n", " [ 0.5533326 , 1.22789904, -0.12317261],\n", " [-0.09251626, -0.00445541, -0.28129234],\n", " [-0.05594547, -0.7873575 , 2.18726381],\n", " [-1.03566553, -0.15670435, -0.58689331],\n", " [-0.00324649, -1.54265608, 2.0516329 ],\n", " [ 1.89165757, -0.80526861, 0.17857485],\n", " [-1.04799141, -0.72118009, 0.26854029]])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rng = np.random.default_rng()\n", "data = rng.normal(size=(10, 3))\n", "data" ] }, { "cell_type": "code", "execution_count": 3, "id": "6cd243fc", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([-0.09251626, -0.00445541, -0.28129234])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data[4]" ] }, { "cell_type": "code", "execution_count": 4, "id": "f333c3fe", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-0.63538581, 0.28763612, 1.70806247],\n", " [ 0.5533326 , 1.22789904, -0.12317261]])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data[2:4]" ] }, { "cell_type": "code", "execution_count": 5, "id": "81ffc2e6", "metadata": {}, "outputs": [], "source": [ "data[2:4] = rng.normal(size=(2, 3))" ] }, { "cell_type": "code", "execution_count": 6, "id": "e002d2a0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0.84744338, 0.72444063, -0.1683032 ],\n", " [-0.35206446, 1.2773733 , 0.42331498],\n", " [-0.98336479, -0.51449641, 1.50154858],\n", " [-0.0927469 , 0.71687669, 1.15388018],\n", " [-0.09251626, -0.00445541, -0.28129234],\n", " [-0.05594547, -0.7873575 , 2.18726381],\n", " [-1.03566553, -0.15670435, -0.58689331],\n", " [-0.00324649, -1.54265608, 2.0516329 ],\n", " [ 1.89165757, -0.80526861, 0.17857485],\n", " [-1.04799141, -0.72118009, 0.26854029]])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data" ] }, { "cell_type": "markdown", "id": "84bfdb46", "metadata": {}, "source": [ "