{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# NumPy - Numeric Python\n", "NumPy is the core library for scientifc computing Python. The module provides high-performance implementations for dealing with n-dimensional arrays and tools for working with these arrays." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## How to work with NumPy\n", "The fundamental data structure in NumPy is called _array_. An array is a n-dimensional container. It can be used for \n", "\n", "* vectors\n", "* matrices\n", "* n-dimensional data\n", "\n", "Internally a NumPy array is stored row major (C order) by default. Note that this is different to Matlab where arrays are stored column major." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Import NumPy" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%matplotlib notebook\n", "import matplotlib.pyplot as plt\n", "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Creating NumPy arrays" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# integer vector, 1D array\n", "v = np.array([1, 2, 3])\n", "v" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# matrix, 2D array\n", "M = np.array([[1, 2], \n", " [3, 4]], dtype=float)\n", "M" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# volume, 3D array\n", "V = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8 , 9], [10, 11, 12]]])\n", "V" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Initial placeholders" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# create array of zeros with 3 rows and 4 columns\n", "np.zeros((3, 4))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# create array of ones \n", "np.ones((2, 3, 4), dtype=np.int16)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# create array of evenly spaced values\n", "# args: start, stop, step: inclusive start, exclusive stop\n", "np.arange(3, 11, 2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# create array with evenly spaced values using number of samples\n", "# start, stop, num_samples\n", "x = np.linspace(-1, 4, 20)\n", "\n", "_, ax = plt.subplots(figsize=(5, 3.5))\n", "ax.plot(x, np.sin(x))\n", "\n", "_, ax = plt.subplots(figsize=(5, 3.5))\n", "ax.plot(np.sin(x))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# create a constant array \n", "np.full((2, 3), 7)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# create a n x n identity matrix\n", "np.eye(3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# create an array with random values\n", "np.random.random((2, 3))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# create an empty array (attention: memory is uninitialized!)\n", "np.empty((3, 2))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "base = np.array([1, 2, 3])\n", "\n", "a = []\n", "for i in range(10):\n", " a.append(base.copy())\n", "a = np.array(a)\n", " \n", "b = np.stack([base.copy()] * 10)\n", "c = np.repeat(base[None], 10, axis=0)\n", "d = np.tile(base, (10, 1))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(a)\n", "assert np.allclose(a, b) and np.allclose(b, c) and np.allclose(c, d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Inspecting array" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# array dimensions\n", "V.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# number of dimensions\n", "V.ndim" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# number of elements\n", "V.size" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# data type of array elements\n", "V.dtype" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Array operations" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# sum\n", "V.sum()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# min\n", "V.min()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# max\n", "V.max()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# mean\n", "V.mean()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# standard deviation\n", "V.std()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Arithmetic Operations" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.arange(3)\n", "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# addition\n", "a + a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a + 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# exp\n", "np.exp(a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# * is elementwise multiplication\n", "# @ is __matmul__\n", "# a is one-dimensional\n", "for inner in [(a * a).sum(), a @ a, a.dot(a), a.T.dot(a)]:\n", " print(inner)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for outer in [np.outer(a, a), a[None] * a[:, None]]:\n", " print(outer)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Slicing, Indexing\n", "In python indexing starts at 0!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# get first element\n", "a\n", "a[0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "M" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# get 1st row\n", "M[0] # = M[0,:]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# get last column\n", "M[:,-1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "v = np.arange(10)\n", "v" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# get every second element\n", "# [start:stop:step]\n", "v[1::2]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# boolean indexing\n", "v[v > 3]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "v > 3" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "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.10.7" } }, "nbformat": 4, "nbformat_minor": 2 }