{ "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": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "id": "f0dbdbe9", "metadata": {}, "outputs": [], "source": [ "py_list = [2020, 2021, 20222]\n", "array_1d = np.array(py_list)" ] }, { "cell_type": "code", "execution_count": 3, "id": "dab65ddf", "metadata": {}, "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": {}, "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": {}, "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": {}, "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": {}, "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": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0.60972663, -1.09008297, -0.54370769],\n", " [-0.8635271 , 0.65038133, -1.8683108 ],\n", " [-0.7872255 , 0.37868543, -0.84196325],\n", " [ 2.65143687, 1.12253299, -0.3692989 ],\n", " [-0.90096871, 0.31359236, -0.03624293],\n", " [-0.76783756, -0.50115094, 0.09854764],\n", " [-0.52639221, 0.49102799, 0.22972431]])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = np.random.randn(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": {}, "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": {}, "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": {}, "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": {}, "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 }