{ "cells": [ { "cell_type": "markdown", "metadata": { "tags": [ "meta", "toc_en" ] }, "source": [ "# Numpy snippets" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "This notebook is inspired by the following page: https://docs.scipy.org/doc/numpy-dev/user/numpy-for-matlab-users.html" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\"Open" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\"Open" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Import directives" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "hide" ] }, "outputs": [], "source": [ "%matplotlib inline\n", "#%matplotlib notebook\n", "\n", "from IPython.display import display" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import math" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create arrays" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.array([1, 2, 3])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.array([[1, 2, 3],[4, 5, 6]])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Special matrices" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.zeros(3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.zeros((3, 4))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.ones(3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.ones((3, 4))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.eye(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Arange" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.arange(10)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.arange(10, 20)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.arange(10, 20, 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Linspace" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.linspace(0., 2., 5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Meshgrid" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xx, yy = np.meshgrid([1, 2, 3], [4, 5, 6])\n", "\n", "print(xx)\n", "print()\n", "print(yy)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Random" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Uniform distribution in [0, 1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.random.rand(3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.random.rand(3, 4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Poisson distribution" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.random.poisson(10, size=[3, 4])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Multivariate normal distribution" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mu = np.array([0., 0.])\n", "cov = np.array([[1., 0.3],\n", " [0.3, 1.]])\n", "num_points = 10\n", "\n", "np.random.multivariate_normal(mu, cov, num_points)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Print options" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.get_printoptions()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Print large arrays" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "default_threshold = np.get_printoptions()[\"threshold\"]\n", "default_threshold" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Arrays with more than ``default_threshold`` elements are truncated." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "max_size = math.ceil(math.sqrt(default_threshold))\n", "max_size" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.random.randint(1, size=[max_size + 1, max_size + 1])\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Print the full array (set threshold to infinity):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.set_printoptions(threshold=np.inf)\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Go back to the default threshold:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.set_printoptions(threshold=default_threshold)\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dimension and shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.array([[1, 2, 3],[4, 5, 6]])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Number of dimensions:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a.ndim" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Number of elements:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a.size" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Number of elements per dimension:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Convert" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "l = [[1, 2, 3],[4, 5, 6]]\n", "a = np.array([[1, 2, 3],[4, 5, 6]])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Python list to Numpy array" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.array(l)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Numpy array to Python list" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a.tolist()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Copy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### np.copy()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.array([[1, 2, 3],[4, 5, 6]])\n", "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "b = a.copy()\n", "b" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a[0,0] = 10\n", "print(a)\n", "print(b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### np.astype()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.array([[1, 2, 3],[4, 5, 6]])\n", "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "b = a.astype('float64', copy=True)\n", "b" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a[0,0] = 10\n", "print(a)\n", "print(b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Access elements" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.arange(6)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a[0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a[-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Slices" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a[1:4]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.array([[1, 2, 3, 4, 5, 6],\n", " [10, 20, 30, 40, 50, 60],\n", " [100, 200, 300, 400, 500, 600]])\n", "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a[0,1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a[1, :]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a[1, ::2]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a[:, 1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a[0:2, 2:4]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a[1:, 1:]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a[:-1, :-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Ellipsis" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\"The ellipsis is used to slice high-dimensional data structures.\n", "\n", "It's designed to mean at this point, insert as many full slices (:) to extend the multi-dimensional slice to all dimensions.\"\n", "\n", "https://stackoverflow.com/questions/118370/how-do-you-use-the-ellipsis-slicing-syntax-in-python" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.arange(2**3).reshape(2, 2, 2)\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To select all first elements in the last (3rd) dimension" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a[..., 0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "is equivalent to" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a[:, :, 0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To select all first elements in the first (1st) dimension" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a[0, ...]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "is equivalent to" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a[0, :, :]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Filter" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.array([[1, 2, 3, 4, 5, 6],\n", " [10, 20, 30, 40, 50, 60],\n", " [100, 200, 300, 400, 500, 600]])\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Boolean matrix whose i,jth element is (a_ij > 5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "(a>5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Find the indices where (a > 5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.nonzero(a>5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Set or copy a with elements greater than 5 zeroed out" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a * (a<=5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a[a>5] = 0\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Select indices satisfying multiple conditions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Short version" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.array([[-1, 7, 3], [-11, -5, 20]])\n", "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a[(a > -10) & (a < 10)] = 0\n", "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a[(a < -10) | (a > 10)] = 1\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Detailed version" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.array([[-1, 7, 3], [-11, -5, 20]])\n", "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m1 = (a > -10)\n", "m2 = (a < 10)\n", "\n", "print(m1)\n", "print(m2)\n", "print(m1 & m2)\n", "\n", "a[m1 & m2] = 0\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Concatenate" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Append 1D arrays" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.array([])\n", "a = np.append(a, 3)\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Performance test" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It's probably not a good idea to use `np.append` to often as it makes a copy of the array each time it is called..." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%timeit\n", "\n", "a = np.array([])\n", "for i in range(10000):\n", " a = np.append(a, i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lists use a different data structure that makes them more efficient for repeated additions..." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%timeit\n", "\n", "l = []\n", "for i in range(10000):\n", " l.append(i)\n", "\n", "a = np.array(l)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this case, the better option is probably the following:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%timeit\n", "\n", "a = np.array([i for i in range(10000)])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Concatenate 1D arrays" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.zeros(3)\n", "b = np.ones(3)\n", "print(\"a:\", a)\n", "print(\"b:\", b)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.concatenate([a, b])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.hstack([a, b])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Concatenate 2D arrays" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.zeros([2, 3])\n", "b = np.ones([2, 3])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### On the first dimension" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using vstack:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.vstack([a, b])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "hide" ] }, "outputs": [], "source": [ "np.vstack([a, b]).shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using concatenate:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.concatenate([a, b], axis=0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "hide" ] }, "outputs": [], "source": [ "np.concatenate([a, b], axis=0).shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### On the second dimension" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using hstack:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.hstack([a, b])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "hide" ] }, "outputs": [], "source": [ "np.hstack([a, b]).shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using concatenate:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.concatenate([a, b], axis=1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "hide" ] }, "outputs": [], "source": [ "np.concatenate([a, b], axis=1).shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Join a sequence of arrays along a *new* axis" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `axis` parameter specifies the index of the new axis in the dimensions\n", "of the result." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.zeros([2, 3])\n", "b = np.ones([2, 3])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Along axis 0" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.stack([a, b], axis=0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.stack([a, b], axis=0).shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Along axis 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.stack([a, b], axis=1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.stack([a, b], axis=1).shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Along axis 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.stack([a, b], axis=2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.stack([a, b], axis=2).shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Tile" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.array([[1, 2, 3], [4, 5, 6]])\n", "np.tile(a, (2, 3))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reshape or transpose" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.array([[1, 2, 3], [4, 5, 6]])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Transpose" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a.T" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Flatten" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a.flatten()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Reshape" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.arange(6)\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Row vector to column vector" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a.reshape([-1, 1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Vector to matrix" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a.reshape([2, 3])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a.reshape([3, 2])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Repeat" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.arange(3)\n", "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.repeat(a, 5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.arange(3).reshape([-1, 1])\n", "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.repeat(a, 5, axis=0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.array([[1, 3, 5],[2, 4, 6]])\n", "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.repeat(a, 5, axis=0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sort" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Return the indices that would sort an array" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.array([8, 5, 1])\n", "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a.argsort()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Sort an array by the $n^{\\text{th}}$ column" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "hide" ] }, "source": [ "Ref.: https://stackoverflow.com/questions/2828059/sorting-arrays-in-numpy-by-column" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.array([[4, 4, 2],\n", " [8, 5, 1],\n", " [7, 0, 0],\n", " [3, 1, 1],\n", " [3, 0, 5]])\n", "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n = 0 # the column sorted by" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a[a[:,n].argsort()]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n = 1 # the column sorted by" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a[a[:,n].argsort()]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n = 2 # the column sorted by" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a[a[:,n].argsort()]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Aggregation / reduction" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.array([[1, 2, 3], [4, 5, 6]])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Change the `axis` value in the following functions to aggregate along a given axis." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.sum(a, axis=None)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.cumsum(a, axis=None)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.diff(a.ravel())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.mean(a, axis=None)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.var(a, axis=None)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.std(a, axis=None)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.median(a, axis=None)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.min(a, axis=None)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.max(a, axis=None)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.prod(a, axis=None)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.cumprod(a, axis=None)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Compute the histogram of a set of data (with a specific binning)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.array([1, 1, 3, 2, 2, 2])\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "All but the last (righthand-most) bin is half-open. In other words,\n", "if `bins` is:\n", "\n", " [1, 2, 3, 4]\n", "\n", "then the first bin is ``[1, 2)`` (including 1, but excluding 2) and\n", "the second ``[2, 3)``. The last bin, however, is ``[3, 4]``, which\n", "*includes* 4." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bins = np.array([1, 2, 3, 4])\n", "bins" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "hist, bins_ = np.histogram(a, bins=bins)\n", "hist" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Linear algebra" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Dot product of two arrays" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.array([1, 2, 3])\n", "b = np.array([10, 20, 30])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.dot(a, b)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a.dot(b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Compute the (multiplicative) inverse of a matrix" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.random.normal(size=(3, 3))\n", "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.linalg.inv(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Compute the eigenvalues and right eigenvectors of a square array" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.random.normal(size=(3, 3))\n", "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.linalg.eig(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Singular Value Decomposition" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.random.normal(size=(3, 3))\n", "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "U, s, V = np.linalg.svd(a)\n", "print(U, s, V)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Solve a linear matrix equation, or system of linear scalar equations" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.array([[3, 1], [1, 2]])\n", "b = np.array([9, 8])\n", "np.linalg.solve(a, b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Diagonals" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Extract the diagonal:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n", "np.diag(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Make a diagonal matrix:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "d = np.array([1, 2, 3])\n", "np.diag(d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Trace" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n", "np.trace(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Upper and lower triangles of an array" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.triu(a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.tril(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Data types" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "See http://docs.scipy.org/doc/numpy-1.10.1/user/basics.types.html" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Get type" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.arange(-2., 2., 0.5)\n", "a.dtype" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Size in memory (in bytes)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.arange(-2., 2., 0.5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Per item:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a.itemsize" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Full array:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a.nbytes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Init" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.zeros(3)\n", "a.dtype" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.zeros(3, dtype=np.bool)\n", "a.dtype" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.zeros(3, dtype=np.int)\n", "a.dtype" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.zeros(3, dtype=np.int8)\n", "a.dtype" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.zeros(3, dtype=np.uint8)\n", "a.dtype" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Conversions" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.arange(-2., 2., 0.5)\n", "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a.astype(np.bool)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a.astype(np.int)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a.astype(np.int8)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a.astype(np.uint8)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Masked arrays" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Without masked array" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.array([[np.nan, 2, 3], [1, np.nan, 6]])\n", "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a.min()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.nanmin(a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a.max()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.nanmax(a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a.mean()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.nanmean(a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### With masked array" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ma = np.ma.masked_where(np.isnan(a), a)\n", "ma" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ma.min()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ma.max()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ma.mean()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ma.shape" ] } ], "metadata": { "anaconda-cloud": {}, "celltoolbar": "Tags", "kernelspec": { "display_name": "Python 3", "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.7.1" } }, "nbformat": 4, "nbformat_minor": 2 }