{ "cells": [ { "cell_type": "markdown", "id": "3058d174", "metadata": {}, "source": [ "# `ndarray` – an N-dimensional array object\n", "\n", "`ndarray` allows mathematical operations on whole blocks of data, using a similar syntax to similar operations between [scalar](https://en.wikipedia.org/wiki/Scalar_(mathematics)) elements. In NumPy, there are many different types for describing scalars, mostly based on types from the C language and those compatible with Python.\n", "\n", "
\n", "\n", "**See also:**\n", "\n", "* [Array Scalars](https://numpy.org/devdocs/reference/arrays.scalars.html)\n", "
\n", "
\n", "\n", "**Note:**\n", "\n", "Whenever this tutorial talks about _array_ or `ndarray`, in most cases it refers to the `ndarray` object.\n", "
" ] }, { "cell_type": "code", "execution_count": 1, "id": "87b9dcf7", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T14:58:56.684035Z", "iopub.status.busy": "2026-05-17T14:58:56.683774Z", "iopub.status.idle": "2026-05-17T14:58:56.714915Z", "shell.execute_reply": "2026-05-17T14:58:56.714568Z", "shell.execute_reply.started": "2026-05-17T14:58:56.684014Z" } }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "id": "f0dbdbe9", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T14:58:56.715410Z", "iopub.status.busy": "2026-05-17T14:58:56.715287Z", "iopub.status.idle": "2026-05-17T14:58:56.717571Z", "shell.execute_reply": "2026-05-17T14:58:56.717172Z", "shell.execute_reply.started": "2026-05-17T14:58:56.715397Z" } }, "outputs": [], "source": [ "py_list = [2020, 2021, 20222]\n", "array_1d = np.array(py_list)" ] }, { "cell_type": "code", "execution_count": 3, "id": "dab65ddf", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T14:58:56.718052Z", "iopub.status.busy": "2026-05-17T14:58:56.717952Z", "iopub.status.idle": "2026-05-17T14:58:56.721160Z", "shell.execute_reply": "2026-05-17T14:58:56.720860Z", "shell.execute_reply.started": "2026-05-17T14:58:56.718043Z" } }, "outputs": [ { "data": { "text/plain": [ "array([ 2020, 2021, 20222])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_1d" ] }, { "cell_type": "markdown", "id": "b3b0a43c", "metadata": {}, "source": [ "Nested sequences, such as a list of lists of equal length, can be converted into a multidimensional array:" ] }, { "cell_type": "code", "execution_count": 4, "id": "c4ca7f18", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T14:58:56.721814Z", "iopub.status.busy": "2026-05-17T14:58:56.721668Z", "iopub.status.idle": "2026-05-17T14:58:56.723482Z", "shell.execute_reply": "2026-05-17T14:58:56.723204Z", "shell.execute_reply.started": "2026-05-17T14:58:56.721802Z" } }, "outputs": [], "source": [ "list_of_lists = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]\n", "array_2d = np.array(list_of_lists)" ] }, { "cell_type": "code", "execution_count": 5, "id": "5908e6ef", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T14:58:56.723829Z", "iopub.status.busy": "2026-05-17T14:58:56.723742Z", "iopub.status.idle": "2026-05-17T14:58:56.726220Z", "shell.execute_reply": "2026-05-17T14:58:56.725875Z", "shell.execute_reply.started": "2026-05-17T14:58:56.723821Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 2, 3, 4],\n", " [ 5, 6, 7, 8],\n", " [ 9, 10, 11, 12]])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_2d" ] }, { "cell_type": "markdown", "id": "2ca45895", "metadata": {}, "source": [ "Since `list_of_lists` was a list with three lists, the NumPy array `array_2d` has two dimensions whose shape is derived from the data. With the attributes [ndim](https://numpy.org/devdocs/reference/generated/numpy.ndarray.ndim.html) and [shape](https://numpy.org/devdocs/reference/generated/numpy.ndarray.shape.html) we can output the number of dimensions and the outline of `array_2d`:" ] }, { "cell_type": "code", "execution_count": 6, "id": "acbfea93", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T14:58:56.728195Z", "iopub.status.busy": "2026-05-17T14:58:56.728005Z", "iopub.status.idle": "2026-05-17T14:58:56.730174Z", "shell.execute_reply": "2026-05-17T14:58:56.729972Z", "shell.execute_reply.started": "2026-05-17T14:58:56.728186Z" } }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_2d.ndim" ] }, { "cell_type": "code", "execution_count": 7, "id": "3bef667e", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T14:58:56.730624Z", "iopub.status.busy": "2026-05-17T14:58:56.730550Z", "iopub.status.idle": "2026-05-17T14:58:56.732634Z", "shell.execute_reply": "2026-05-17T14:58:56.732425Z", "shell.execute_reply.started": "2026-05-17T14:58:56.730616Z" } }, "outputs": [ { "data": { "text/plain": [ "(3, 4)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_2d.shape" ] }, { "cell_type": "markdown", "id": "1a9267d8", "metadata": {}, "source": [ "To give you an idea of the syntax, I first create an array of random numbers with five columns and seven `slices`:" ] }, { "cell_type": "code", "execution_count": 8, "id": "8a008d03", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T14:58:56.732999Z", "iopub.status.busy": "2026-05-17T14:58:56.732943Z", "iopub.status.idle": "2026-05-17T14:58:56.742230Z", "shell.execute_reply": "2026-05-17T14:58:56.741872Z", "shell.execute_reply.started": "2026-05-17T14:58:56.732993Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[0.92924119, 0.56249986, 0.89913907],\n", " [0.74688464, 0.71027425, 0.11651989],\n", " [0.45495343, 0.19435449, 0.20933617],\n", " [0.31051074, 0.99903966, 0.79290766],\n", " [0.77962503, 0.58949486, 0.85441874],\n", " [0.36481064, 0.53221103, 0.18984443],\n", " [0.46607016, 0.54310155, 0.33321979]])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rng = np.random.default_rng()\n", "data = rng.random((7, 3))\n", "data" ] }, { "cell_type": "markdown", "id": "f3b875e5", "metadata": {}, "source": [ "`ndarray` is a generic multidimensional container. Each array has a shape, a tuple, which indicates the size of the individual dimensions. With `shape`, I can output the number of rows and columns in an array:" ] }, { "cell_type": "markdown", "id": "c6284a5e", "metadata": {}, "source": [ "In addition to `np.array`, there are a number of other functions for creating new arrays. [zeros](https://numpy.org/doc/stable/reference/generated/numpy.zeros.html) and [ones](https://numpy.org/doc/stable/reference/generated/numpy.ones.html), for example, create arrays of zeros and ones, respectively, with a specific length or shape. [empty](https://numpy.org/doc/stable/reference/generated/numpy.empty.html) creates an array without initialising its values to a specific value. To create a higher dimensional array using these methods, pass a tuple for the shape:" ] }, { "cell_type": "code", "execution_count": 9, "id": "535012fb", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T14:58:56.742731Z", "iopub.status.busy": "2026-05-17T14:58:56.742631Z", "iopub.status.idle": "2026-05-17T14:58:56.745435Z", "shell.execute_reply": "2026-05-17T14:58:56.745160Z", "shell.execute_reply.started": "2026-05-17T14:58:56.742723Z" } }, "outputs": [ { "data": { "text/plain": [ "array([0., 0., 0., 0.])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.zeros(4)" ] }, { "cell_type": "code", "execution_count": 10, "id": "06c5c70f", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T14:58:56.745842Z", "iopub.status.busy": "2026-05-17T14:58:56.745766Z", "iopub.status.idle": "2026-05-17T14:58:56.748010Z", "shell.execute_reply": "2026-05-17T14:58:56.747784Z", "shell.execute_reply.started": "2026-05-17T14:58:56.745835Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[1., 1., 1., 1.],\n", " [1., 1., 1., 1.],\n", " [1., 1., 1., 1.]])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.ones((3, 4))" ] }, { "cell_type": "code", "execution_count": 11, "id": "b452ec6c", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T14:58:56.748379Z", "iopub.status.busy": "2026-05-17T14:58:56.748311Z", "iopub.status.idle": "2026-05-17T14:58:56.750408Z", "shell.execute_reply": "2026-05-17T14:58:56.750161Z", "shell.execute_reply.started": "2026-05-17T14:58:56.748373Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[[0., 0., 0., 0.],\n", " [0., 0., 0., 0.],\n", " [0., 0., 0., 0.]],\n", "\n", " [[0., 0., 0., 0.],\n", " [0., 0., 0., 0.],\n", " [0., 0., 0., 0.]]])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.empty((2, 3, 4))" ] }, { "cell_type": "markdown", "id": "7faf6f63", "metadata": {}, "source": [ "
\n", "\n", "**Note:**\n", "\n", "You may not safely assume that the `np.empty` function returns an array of zeros, as it returns uninitialised memory and may therefore contain garbage values.\n", "
" ] }, { "cell_type": "markdown", "id": "c11209a9", "metadata": {}, "source": [ "[arange](https://numpy.org/doc/stable/reference/generated/numpy.arange.html) is an array-valued version of the Built-in Python [range](https://docs.python.org/3/library/functions.html#func-range) function:" ] }, { "cell_type": "code", "execution_count": 12, "id": "91c43273", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T14:58:56.750820Z", "iopub.status.busy": "2026-05-17T14:58:56.750747Z", "iopub.status.idle": "2026-05-17T14:58:56.753105Z", "shell.execute_reply": "2026-05-17T14:58:56.752840Z", "shell.execute_reply.started": "2026-05-17T14:58:56.750812Z" } }, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.arange(4)" ] }, { "cell_type": "markdown", "id": "025541bc", "metadata": {}, "source": [ "Other NumPy standard functions for creating arrays are:\n", "\n", "Function | Description\n", ":------- | :----------\n", "`array` | converts input data (list, tuple, array or other sequence types) into an `ndarray` by either deriving a `dtype` or explicitly specifying a `dtype`; by default, copies the input data into the array\n", "`asarray` | converts the input to an `ndarray`, but does not copy if the input is already an `ndarray`\n", "`arange` | like Python built-in `range`, but returns an `ndarray` instead of a list\n", "`ones`, `ones_like` | `ones` creates an array of 1s in the given form and `dtype`; `ones_like` takes another array and creates an `ones` array in the same form and dtype\n", "`zeros`, `zeros_like` | like `ones` and `ones_like`, but creates arrays with 0s instead\n", "`empty`, `empty_like` | creates new arrays by allocating new memory, but does not fill them with values like `ones` and `zeros`\n", "`full`, `full_like` | creates an array of the given `shape` and `dtype`, setting all values to the given fill value; `full_like` takes another array and creates a filled array with the same `shape` and `dtype`\n", "`eye`, `identity` | creates a square N × N identity matrix (1s on the diagonal and 0s elsewhere)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.13 Kernel", "language": "python", "name": "python313" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.0" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }