{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Numpy\n", "\n", "\n", "[Creating arrays](#carr)\n", "\n", "[Creating Arrays with Ones, Zeros and Empty](#caro)\n", "\n", "[Copying arrays](#cop)\n", "\n", "[Structured arrays](#sa)\n", "\n", "[Identical arrays](#ia)\n", "\n", "[Numerical operation on numpy arrays](#non)\n", "\n", "[Broadcasting](#bd)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The advantages of Core Python:\n", "\n", "high-level number objects: integers, floating point\n", "containers: lists with cheap insertion and append methods, dictionaries with fast lookup\n", "Advantages of using Numpy with Python:\n", "\n", "array oriented computing\n", "efficiently implemented multi-dimensional arrays\n", "designed for scientific computation" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[20.1 20.8 21.9 22.5 22.7 22.3 21.8 21.2 20.9 20.1]\n" ] } ], "source": [ "import numpy as np\n", "cvalues = [20.1, 20.8, 21.9, 22.5, 22.7, 22.3, 21.8, 21.2, 20.9, 20.1]\n", "\n", "C = np.array(cvalues)\n", "print(C)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[68.18, 69.44, 71.42, 72.5, 72.86, 72.14, 71.24000000000001, 70.16, 69.62, 68.18]\n" ] } ], "source": [ "## Python way\n", "\n", "fvalues = [ x*9/5 + 32 for x in cvalues] \n", "print(fvalues)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX4AAAD8CAYAAABw1c+bAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAAIABJREFUeJzt3Xd8VuX5x/HPlU0mIyFAEggrQJiBgAwB60D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S6hd4TPGLyEgR2SoiWY7bS73W6RbW82Yi4isia0TkS9tZbBORpiIyR0S2OP6MDLKdySYRucfx92SjiMwWkSDbmRqaRxS/iPgCLwKXAInARMdCct7qdAvrebO7qF5nSsEMYKExpivQGy9+X0QkBrgTSDbG9AB8gQl2UzU8jyh+qlf+zDLG7DTGlAEfUL2InFf6hYX1vJKIxAKXAq/bzmKbiIQDw4A3AIwxZcaYo3ZTWecHNBERPyCY06wu4Ek8pfh1MbjT+K+F9bzVc8AD/GcA0Zt1AAqAtxynvl4XkRDboWwxxuwFnqV6CHUfUGiM+dpuqobnKcXv9GJw3uQMF9bzKCJyGXDAGJNhO4uL8KN6CZWXjDFJwHFqeJaGt3A8H2Q00B5oA4SIyHV2UzU8Tyl+pxeD8xanWVjPGw0BrhCR3VSfAjxfRN63G8mqPcAeY8y//g9wDv9ZS8sbXQjsMsYUGGPKgXnAYMuZGpynFP8qoLOItBeRAKovznxuOZM1joX13gAyjTHTbeexyRjzsDEm1hgTT/Wfi++MMR5/RHc6xpj9QK6IdHFsugDYbDGSbTnAQBEJdvy9uQAvuNhd61o97sAYUyEidwCLqL4q/6YxZpPlWDYNAa4HNojIWse2acaYBRYzKdfxO2CW4yBpJ3CT5TzWGGNSRWQO1SsHVwBr8IIpXp3cVUopL+Mpp3qUUko5SYtfKaW8jBa/Ukp5GS1+pZTyMlr8SinlZbT4lVLKy2jxK6WUl9HiV0opL/P/NkGHDoA8tZIAAAAASUVORK5CYII=\n", "text/plain": [ "
" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "\n", "plt.plot(C)\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The main benefits of using numpy arrays should be smaller memory consumption and better runtime behaviour" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "120\n", "96\n" ] } ], "source": [ "# We will create the numpy array of the previous diagram and calculate the memory usage:\n", "from sys import getsizeof as size\n", "a = np.array([24, 12, 57])\n", "print(size(a))\n", "\n", "# We get the memory usage for the general array information by creating an empty array:\n", "\n", "e = np.array([])\n", "print(size(e))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can see that the difference between the empty array \"e\" and the array \"a\" with three integers consists in 24 Bytes. This means that an arbitrary integer array of length \"n\" in numpy needs 96 + n * 8 Bytes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "\n", "### Creating arrays" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3 4 5 6 7 8 9]\n", "range(1, 10)\n", "[1, 2, 3, 4, 5, 6, 7, 8, 9]\n", "[ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.]\n", "[ 0.5 1.3 2.1 2.9 3.7 4.5 5.3 6.1 6.9 7.7 8.5 9.3 10.1]\n" ] } ], "source": [ "## arange\n", "\n", "# arange([start,] stop[, step], [, dtype=None])\n", "\n", "\n", "a = np.arange(1, 10)\n", "print(a)\n", "\n", "x = range(1, 10)\n", "print(x) # x is an iterator\n", "print(list(x))\n", "\n", "# further arange examples:\n", "x = np.arange(10.4)\n", "print(x)\n", "x = np.arange(0.5, 10.4, 0.8)\n", "print(x)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 1. 1.18367347 1.36734694 1.55102041 1.73469388 1.91836735\n", " 2.10204082 2.28571429 2.46938776 2.65306122 2.83673469 3.02040816\n", " 3.20408163 3.3877551 3.57142857 3.75510204 3.93877551 4.12244898\n", " 4.30612245 4.48979592 4.67346939 4.85714286 5.04081633 5.2244898\n", " 5.40816327 5.59183673 5.7755102 5.95918367 6.14285714 6.32653061\n", " 6.51020408 6.69387755 6.87755102 7.06122449 7.24489796 7.42857143\n", " 7.6122449 7.79591837 7.97959184 8.16326531 8.34693878 8.53061224\n", " 8.71428571 8.89795918 9.08163265 9.26530612 9.44897959 9.63265306\n", " 9.81632653 10. ]\n", "[ 1. 2.5 4. 5.5 7. 8.5 10. ]\n", "[1. 2.28571429 3.57142857 4.85714286 6.14285714 7.42857143\n", " 8.71428571]\n" ] } ], "source": [ "## linspace\n", "\n", "## linspace(start, stop, num=50, endpoint=True, retstep=False)\n", "\n", "import numpy as np\n", "\n", "# 50 values between 1 and 10:\n", "print(np.linspace(1, 10))\n", "# 7 values between 1 and 10:\n", "print(np.linspace(1, 10, 7))\n", "# excluding the endpoint:\n", "print(np.linspace(1, 10, 7, endpoint=False))" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.1836734693877551\n", "0.47368421052631576\n", "0.45\n" ] } ], "source": [ "# If the optional parameter 'retstep' is set, the function will also return the value of the spacing between adjacent values. So, the function will return a tuple ('samples', 'step'):\n", "\n", "import numpy as np\n", "\n", "samples, spacing = np.linspace(1, 10, retstep=True)\n", "print(spacing)\n", "samples, spacing = np.linspace(1, 10, 20, endpoint=True, retstep=True)\n", "print(spacing)\n", "samples, spacing = np.linspace(1, 10, 20, endpoint=False, retstep=True)\n", "print(spacing)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x: 42\n", "The type of x: \n", "The dimension of x: 0\n" ] } ], "source": [ "# Zero D arrays\n", "\n", "import numpy as np\n", "x = np.array(42)\n", "print(\"x: \", x)\n", "print(\"The type of x: \", type(x))\n", "print(\"The dimension of x:\", np.ndim(x))" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "F: [ 1 1 2 3 5 8 13 21]\n", "V: [ 3.4 6.9 99.8 12.8]\n", "Type of F: int64\n", "Type of V: float64\n", "Dimension of F: 1\n", "Dimension of V: 1\n" ] } ], "source": [ "# 1 -D \n", "\n", "# We have already encountered a 1-dimenional array - better known to some as vectors - in our initial example. What we have not mentioned so far, but what you may have assumed, is the fact that numpy arrays are containers of items of the same type, e.g. only integers. The homogenous type of the array can be determined with the attribute \"dtype\", as we can learn from the following example:\n", "\n", "F = np.array([1, 1, 2, 3, 5, 8, 13, 21])\n", "V = np.array([3.4, 6.9, 99.8, 12.8])\n", "print(\"F: \", F)\n", "print(\"V: \", V)\n", "print(\"Type of F: \", F.dtype)\n", "print(\"Type of V: \", V.dtype)\n", "print(\"Dimension of F: \", np.ndim(F))\n", "print(\"Dimension of V: \", np.ndim(V))" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 3.4 8.7 9.9]\n", " [ 1.1 -7.8 -0.7]\n", " [ 4.1 12.3 4.8]]\n", "2\n" ] } ], "source": [ "## 2 and n-d arrays\n", "\n", "A = np.array([ [3.4, 8.7, 9.9], \n", " [1.1, -7.8, -0.7],\n", " [4.1, 12.3, 4.8]])\n", "print(A)\n", "print(A.ndim)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[[111 112]\n", " [121 122]]\n", "\n", " [[211 212]\n", " [221 222]]\n", "\n", " [[311 312]\n", " [321 322]]]\n", "3\n" ] } ], "source": [ "B = np.array([ [[111, 112], [121, 122]],\n", " [[211, 212], [221, 222]],\n", " [[311, 312], [321, 322]] ])\n", "print(B)\n", "print(B.ndim)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(6, 3)\n", "(6, 3)\n" ] } ], "source": [ "### shape\n", "\n", "# The function \"shape\" returns the shape of an array. The shape is a tuple of integers. These numbers denote the lengths of the corresponding array dimension. In other words: The \"shape\" of an array is a tuple with the number of elements per axis (dimension). In our example, the shape is equal to (6, 3), i.e. we have 6 lines and 3 columns.\n", "\n", "x = np.array([ [67, 63, 87],\n", " [77, 69, 59],\n", " [85, 87, 99],\n", " [79, 72, 71],\n", " [63, 89, 93],\n", " [68, 92, 78]])\n", "\n", "print(np.shape(x))\n", "\n", "# There is also an equivalent array property:\n", "\n", "print(x.shape)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[67 63 87 77 69 59]\n", " [85 87 99 79 72 71]\n", " [63 89 93 68 92 78]]\n", "[[67 63 87 77 69 59 85 87 99]\n", " [79 72 71 63 89 93 68 92 78]]\n" ] } ], "source": [ "# The shape of an array tells us also something about the order in which the indices are processed, i.e. first rows, then columns and after that the further dimensions.\n", "# \"shape\" can also be used to change the shape of an array.\n", "\n", "x.shape = (3, 6)\n", "print(x)\n", "\n", "x.shape = (2, 9)\n", "print(x)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "()\n", "(4, 2, 3)\n" ] } ], "source": [ "x = np.array(11)\n", "print(np.shape(x))\n", "\n", "B = np.array([ [[111, 112, 113], [121, 122, 123]],\n", " [[211, 212, 213], [221, 222, 223]],\n", " [[311, 312, 313], [321, 322, 323]],\n", " [[411, 412, 413], [421, 422, 423]] ])\n", "\n", "print(B.shape)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "21\n" ] } ], "source": [ "## Indexing and Slicing\n", "\n", "F = np.array([1, 1, 2, 3, 5, 8, 13, 21])\n", "# print the first element of F\n", "print(F[0])\n", "# print the last element of F\n", "print(F[-1])" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.1\n" ] } ], "source": [ "## Indexing multidimensional arrays:\n", "\n", "A = np.array([ [3.4, 8.7, 9.9], \n", " [1.1, -7.8, -0.7],\n", " [4.1, 12.3, 4.8]])\n", "\n", "print(A[1][0])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# The general syntax for a one-dimensional array A looks like this:\n", "\n", "A[start:stop:step]" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2 3 4]\n", "[0 1 2 3]\n", "[6 7 8 9]\n", "[0 1 2 3 4 5 6 7 8 9]\n" ] } ], "source": [ "S = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])\n", "\n", "print(S[2:5])\n", "print(S[:4])\n", "print(S[6:])\n", "print(S[:])" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[13 14 15]\n", " [23 24 25]\n", " [33 34 35]]\n" ] } ], "source": [ "A = np.array([\n", "[11, 12, 13, 14, 15],\n", "[21, 22, 23, 24, 25],\n", "[31, 32, 33, 34, 35],\n", "[41, 42, 43, 44, 45],\n", "[51, 52, 53, 54, 55]])\n", "\n", "print(A[:3, 2:])" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[41 42 43 44 45]\n", " [51 52 53 54 55]]\n" ] } ], "source": [ "print(A[3:, :])" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0 1 2 3 4 5 6]\n", " [ 7 8 9 10 11 12 13]\n", " [14 15 16 17 18 19 20]\n", " [21 22 23 24 25 26 27]]\n", "[[ 0 3 6]\n", " [14 17 20]]\n" ] } ], "source": [ "# The following two examples use the third parameter \"step\". The reshape function is used to construct the two-dimensional array. We will explain reshape in the following subchapter:\n", "\n", "X = np.arange(28).reshape(4, 7)\n", "print(X)\n", "\n", "print(X[::2, ::3]) ## From rows get 0 and 3rd row indicated by ::2 and column every 3rd column indicated by ::3" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [], "source": [ "A = np.array(\n", " [ [ [45, 12, 4], [45, 13, 5], [46, 12, 6] ], \n", " [ [46, 14, 4], [45, 14, 5], [46, 11, 5] ], \n", " [ [47, 13, 2], [48, 15, 5], [52, 15, 1] ] ])" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[[46, 14, 4],\n", " [45, 14, 5],\n", " [46, 11, 5]],\n", "\n", " [[47, 13, 2],\n", " [48, 15, 5],\n", " [52, 15, 1]]])" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[1:3, 0:3, :] ## each comma indicates dim --> x,y,z" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[[46, 14],\n", " [45, 14],\n", " [46, 11]],\n", "\n", " [[47, 13],\n", " [48, 15],\n", " [52, 15]]])" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[1:3, 0:3, :2] ## So the 3rd argument in 3d indicates the count of values you need in each array \n", "\n", "## so the above gives only the first 2 values in each array whereas the cell above since its only \" : \" all values in\n", "\n", "## each row will be given and thats essentially the 3rd dimension.\n", "\n", "## The first 2 are row and column (so 1:3 and 0:3 says give 1 to 3rd row and 0 to 3rd column and )" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[[46, 14, 4]],\n", "\n", " [[47, 13, 2]]])" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[1:3, :1] \n", "\n", "## so as expected, the first comma indicates the row indices, so 1:3, 2nd indicates, column so :1 and since no last dim mentioned\n", "\n", "## all values in each are returned" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[[46],\n", " [45]],\n", "\n", " [[47],\n", " [48]]])" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[1:3, 0:2, :1] ## since last is :1, it gives only the first value in each array" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Whereas slicings on lists and tuples create new objects, a slicing operation on an array creates a view on the original array. So we get an another possibility to access the array,\n", "or better a part of the array. From this follows that if we modify a view, the original array will be modified as well." ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 0 1 22 23 4 5 6 7 8 9]\n" ] } ], "source": [ "A = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])\n", "\n", "S = A[2:6]\n", "S[0] = 22\n", "S[1] = 23\n", "print(A)" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n" ] } ], "source": [ "# Doing the similar thing with lists, we can see that we get a copy:\n", "\n", "lst = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n", "lst2 = lst[2:6]\n", "\n", "lst2[0] = 22\n", "lst2[1] = 23\n", "\n", "print(lst)" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[22, 23, 4, 5]" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lst2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "### Creating Arrays with Ones, Zeros and Empty" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1. 1. 1.]\n", " [1. 1. 1.]]\n", "[[1 1 1 1]\n", " [1 1 1 1]\n", " [1 1 1 1]]\n", "[[0. 0. 0. 0.]\n", " [0. 0. 0. 0.]]\n" ] } ], "source": [ "# There are two ways of initializing Arrays with Zeros or Ones. \n", "# The method ones(t) takes a tuple t with the shape of the array and fills the array accordingly with ones.\n", "# By default it will be filled with Ones of type float. \n", "# If you need integer Ones, you have to set the optional parameter dtype to int:\n", "\n", "\n", "import numpy as np\n", "\n", "E = np.ones((2,3))\n", "print(E)\n", "\n", "F = np.ones((3,4),dtype=int)\n", "print(F)\n", "\n", "\n", "Z = np.zeros((2,4))\n", "print(Z)" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 1 1 1 1]\n", "[0 0 0 0 0]\n" ] } ], "source": [ "# There is another interesting way to create an array with Ones or with Zeros, \n", "# if it has to have the same shape as another existing array 'a'. \n", "# Numpy supplies for this purpose the methods ones_like(a) and zeros_like(a).\n", "\n", "x = np.array([2,5,18,14,4])\n", "E = np.ones_like(x)\n", "print(E)\n", "\n", "Z = np.zeros_like(x)\n", "print(Z)" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0., 0., 0., 0.],\n", " [0., 0., 0., 0.]])" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# There is also a way of creating an array with the empty function. It creates and returns a reference to a new array of given shape and type, without initializing the entries. Sometimes the entries are zeros, but you shouldn't be mislead. Usually, they are arbitrary values.\n", "\n", "np.empty((2, 4))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "### Copying arrays" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1001 22 12]\n", " [ 44 53 66]]\n", "[[42 22 12]\n", " [44 53 66]]\n" ] } ], "source": [ "x = np.array([[42,22,12],[44,53,66]], order='F')\n", "y = x.copy()\n", "\n", "x[0,0] = 1001\n", "\n", "print(x)\n", "print(y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "### Identity Arrays" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 1, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 1, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 1, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 1, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 1, 0, 0]])" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# In linear algebra, the identity matrix, or unit matrix, of size n is the n × n square matrix with ones on the main diagonal and zeros elsewhere.\n", "\n", "# There are two ways in Numpy to create identity arrays:\n", "\n", "# identy\n", "# eye\n", "\n", "np.identity(4)" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 1, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 1, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 1, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 1, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 1, 0, 0]])" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.eye(5, 8, k=1, dtype=int)" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[11, 12, 13, 14],\n", " [21, 22, 23, 24],\n", " [31, 32, 33, 34]])" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = np.array([ [11, 12, 13, 14], [21, 22, 23, 24], [31, 32, 33, 34]])\n", "m" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[14, 13, 12, 11],\n", " [24, 23, 22, 21],\n", " [34, 33, 32, 31]])" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "## the elements of each row are in reverse order.\n", "\n", "m[::,::-1] ## same as m[:, ::-1]" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[34, 33, 32, 31],\n", " [24, 23, 22, 21],\n", " [14, 13, 12, 11]])" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "## columns and rows are in reverse order\n", "\n", "m[::-1,::-1]" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[22, 23]])" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "## Cut of the first and last row and the first and last column.\n", "\n", "m[1:-1,1:-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "## Structured Arrays" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[(393,) (337,) (256,)]\n", "\n", "The internal representation:\n", "array([(393,), (337,), (256,)], dtype=[('density', '\n", "\n", "### Numerical Operation on Numpy arrays" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 4. 5. 9.9 5.3 8.9 2.11 12.3 14.9 ]\n" ] } ], "source": [ "## using scalars\n", "\n", "import numpy as np\n", "lst = [2,3, 7.9, 3.3, 6.9, 0.11, 10.3, 12.9]\n", "v = np.array(lst)\n", "v = v + 2\n", "print(v)" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 8.8 11. 21.78 11.66 19.58 4.642 27.06 32.78 ]\n" ] } ], "source": [ "# Multiplication, Subtraction, Division and exponentiation are as easy as the previous addition:\n", "\n", "print(v * 2.2)" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[4, 5, 9.9, 5.3, 8.9, 2.11, 12.3, 14.9]\n" ] } ], "source": [ "## Same with a list and not array\n", "\n", "res = [ val + 2 for val in lst]\n", "print(res)" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.28 µs ± 373 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n", "333 µs ± 12.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n" ] } ], "source": [ "## Measuring the speed of both\n", "\n", "v = np.random.randint(0, 100, 1000)\n", "\n", "%timeit v + 1\n", "\n", "lst = list(v)\n", "\n", "%timeit [ val + 2 for val in lst]" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Adding to arrays: \n", "[[12. 13. 14.]\n", " [22. 23. 24.]\n", " [32. 33. 34.]]\n", "\n", "Multiplying two arrays: \n", "[[22. 24. 26.]\n", " [42. 44. 46.]\n", " [62. 64. 66.]]\n" ] } ], "source": [ "## Arithmetic operation with 2 arrays\n", "\n", "import numpy as np\n", "A = np.array([ [11, 12, 13], [21, 22, 23], [31, 32, 33] ])\n", "\n", "B = np.ones((3,3))\n", "print(\"Adding to arrays: \")\n", "\n", "print(A + B)\n", "print(\"\\nMultiplying two arrays: \")\n", "print(A * (B + 1))" ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[36., 36., 36.],\n", " [66., 66., 66.],\n", " [96., 96., 96.]])" ] }, "execution_count": 94, "metadata": {}, "output_type": "execute_result" } ], "source": [ "## Matrix multiplication\n", "\n", "np.dot(A, B)" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "12\n", "1\n", "12\n", "-14\n" ] } ], "source": [ "print(np.dot(3, 4))\n", "x = np.array([3])\n", "y = np.array([4])\n", "\n", "print(x.ndim)\n", "print(np.dot(x, y))\n", "\n", "x = np.array([3, -2])\n", "y = np.array([-4, 1])\n", "\n", "print(np.dot(x, y))" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True 3\n", "[[ 7 7 17 4]\n", " [ 9 9 19 0]]\n" ] } ], "source": [ "# Let's go to the two-dimensional use case:\n", "\n", "A = np.array([ [1, 2, 3], \n", " [3, 2, 1] ])\n", "B = np.array([ [2, 3, 4, -2], \n", " [1, -1, 2, 3],\n", " [1, 2, 3, 0] ])\n", "\n", "\n", "print(A.shape[-1] == B.shape[-2], A.shape[1]) \n", "print(np.dot(A, B))\n", "\n", "# We can learn from the previous example that the number of columns of the first two-dimension array have to be the same as the number of the lines of the second two-dimensional array." ] }, { "cell_type": "code", "execution_count": 97, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The shapes:\n", "(3, 3, 3)\n", "(3, 3, 3)\n", "(3, 3, 3, 3)\n", "\n", "The Result R:\n", "[[[[14 19 15]\n", " [15 15 9]\n", " [13 9 18]]\n", "\n", " [[18 24 20]\n", " [20 20 12]\n", " [18 12 22]]\n", "\n", " [[15 18 22]\n", " [22 13 12]\n", " [21 9 14]]]\n", "\n", "\n", " [[[16 21 19]\n", " [19 16 11]\n", " [17 10 19]]\n", "\n", " [[25 32 32]\n", " [32 23 18]\n", " [29 15 28]]\n", "\n", " [[13 18 12]\n", " [12 18 8]\n", " [11 10 17]]]\n", "\n", "\n", " [[[11 14 14]\n", " [14 11 8]\n", " [13 7 12]]\n", "\n", " [[17 23 19]\n", " [19 16 11]\n", " [16 10 22]]\n", "\n", " [[19 25 23]\n", " [23 17 13]\n", " [20 11 23]]]]\n" ] } ], "source": [ "## Dot product for 3d\n", "\n", "\n", "import numpy as np\n", "X = np.array( [[[3, 1, 2],\n", " [4, 2, 2],\n", " [2, 4, 1]],\n", "\n", " [[3, 2, 2],\n", " [4, 4, 3],\n", " [4, 1, 1]],\n", "\n", " [[2, 2, 1],\n", " [3, 1, 3],\n", " [3, 2, 3]]])\n", "\n", "Y = np.array( [[[2, 3, 1],\n", " [2, 2, 4],\n", " [3, 4, 4]],\n", " \n", " [[1, 4, 1],\n", " [4, 1, 2],\n", " [4, 1, 2]],\n", " \n", " [[1, 2, 3],\n", " [4, 1, 1],\n", " [3, 1, 4]]])\n", "\n", "\n", "R = np.dot(X, Y)\n", "\n", "print(\"The shapes:\")\n", "print(X.shape)\n", "print(Y.shape)\n", "print(R.shape)\n", "\n", "print(\"\\nThe Result R:\")\n", "print(R)" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "X.shape: (4, 2, 3) X.ndim: 3\n", "Y.shape: (2, 3, 5) Y.ndim: 3\n", "R.shape: (4, 2, 2, 5) R.ndim: 4\n", "\n", "The result array R:\n", "\n", "[[[[ 14 19 5 8 1]\n", " [ 15 15 10 16 3]]\n", "\n", " [[ 18 24 8 10 2]\n", " [ 20 20 14 22 2]]]\n", "\n", "\n", " [[[ 1 1 -1 -1 -2]\n", " [ 3 -3 -3 1 -2]]\n", "\n", " [[ -6 -7 -1 0 3]\n", " [-11 1 2 -8 5]]]\n", "\n", "\n", " [[[ 16 21 7 8 1]\n", " [ 19 16 11 20 0]]\n", "\n", " [[ 25 32 12 11 1]\n", " [ 32 23 16 33 -4]]]\n", "\n", "\n", " [[[ 11 14 6 5 1]\n", " [ 14 11 8 15 -2]]\n", "\n", " [[ 17 23 5 9 0]\n", " [ 19 16 10 19 3]]]]\n" ] } ], "source": [ "# To demonstrate how the dot product in the three-dimensional case works, we will use different arrays with non-symmetrical shapes in the following example:\n", "\n", "import numpy as np\n", "X = np.array(\n", " [[[3, 1, 2],\n", " [4, 2, 2]],\n", "\n", " [[-1, 0, 1],\n", " [1, -1, -2]],\n", " \n", " [[3, 2, 2],\n", " [4, 4, 3]],\n", "\n", " [[2, 2, 1],\n", " [3, 1, 3]]])\n", "\n", "Y = np.array(\n", " [[[2, 3, 1, 2, 1],\n", " [2, 2, 2, 0, 0],\n", " [3, 4, 0, 1, -1]],\n", "\n", " [[1, 4, 3, 2, 2],\n", " [4, 1, 1, 4, -3],\n", " [4, 1, 0, 3, 0]]])\n", "\n", "\n", "R = np.dot(X, Y)\n", "\n", "print(\"X.shape: \", X.shape, \" X.ndim: \", X.ndim)\n", "print(\"Y.shape: \", Y.shape, \" Y.ndim: \", Y.ndim)\n", "print(\"R.shape: \", R.shape, \"R.ndim: \", R.ndim)\n", "\n", "\n", "print(\"\\nThe result array R:\\n\")\n", "print(R)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Matrices vs. Two-Dimensional Arrays\n", "\n", "Some may have taken two-dimensional arrays of Numpy as matrices. This is principially all right, because they behave in most aspects like our mathematical idea of a matrix. We even saw that we can perform matrix multiplication on them. Yet, there is a subtle difference. There are \"real\" matrices in Numpy. They are a subset of the two-dimensional arrays. We can turn a two-dimensional array into a matrix by applying the \"mat\" function. The main difference shows, if you multiply two two-dimensional arrays or two matrices. We get real matrix multiplication by multiplying two matrices, but the two-dimensional arrays will be only multiplied component-wise:" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 3 4 3]\n", " [ 2 4 6]\n", " [-3 -6 -9]]\n" ] } ], "source": [ "import numpy as np\n", "\n", "A = np.array([ [1, 2, 3], [2, 2, 2], [3, 3, 3] ])\n", "B = np.array([ [3, 2, 1], [1, 2, 3], [-1, -2, -3] ])\n", "\n", "R = A * B\n", "print(R)" ] }, { "cell_type": "code", "execution_count": 103, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3],\n", " [2, 2, 2],\n", " [3, 3, 3]])" ] }, "execution_count": 103, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 3, 2, 1],\n", " [ 1, 2, 3],\n", " [-1, -2, -3]])" ] }, "execution_count": 104, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 2 0 -2]\n", " [ 6 4 2]\n", " [ 9 6 3]]\n" ] } ], "source": [ "MA = np.mat(A)\n", "MB = np.mat(B)\n", "\n", "R = MA * MB\n", "print(R)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Comparison Operators\n", "\n", "We are used to comparison operators from Python, when we apply them to integers, floats or strings for example. We use them to test for True or False. If we compare two arrays, we don't get a simple True or False as a return value. The comparisons are performed elementswise. This means that we get a Boolean array as a return value:" ] }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ True, False, True],\n", " [False, True, False],\n", " [ True, True, False]])" ] }, "execution_count": 105, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "\n", "A = np.array([ [11, 12, 13], [21, 22, 23], [31, 32, 33] ])\n", "B = np.array([ [11, 102, 13], [201, 22, 203], [31, 32, 303] ])\n", "\n", "A == B" ] }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n", "True\n" ] } ], "source": [ "print(np.array_equal(A, B))\n", "print(np.array_equal(A, A))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Logical Operators\n", "\n", "We can also apply the logical 'or' and the logical 'and' to arrays elementwise. We can use the functions 'logical_or' and 'logical_and' to this purpose." ] }, { "cell_type": "code", "execution_count": 109, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ True True]\n", " [ True False]]\n", "[[ True False]\n", " [False False]]\n" ] } ], "source": [ "a = np.array([[True, True], [False, False]])\n", "b = np.array([[True, False], [True, False]])\n", "\n", "print(np.logical_or(a, b))\n", "\n", "print(np.logical_and(a, b))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "### Broadcasting\n", "\n", "Numpy provides a powerful mechanism, called Broadcasting, which allows to perform arithmetic operations on arrays of different shapes. This means that we have a smaller array and a larger array, and we transform or apply the smaller array multiple times to perform some operation on the larger array. In other words: Under certain conditions, the smaller array is \"broadcasted\" in a way that it has the same shape as the larger array.\n", "\n", "With the aid of broadcasting we can avoid loops in our Python program. The looping occurs implicitly in the Numpy implementations, i.e. in C. We also avoid creating unnecessary copies of our data." ] }, { "cell_type": "code", "execution_count": 111, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Multiplication with broadcasting: \n", "[[11 24 39]\n", " [21 44 69]\n", " [31 64 99]]\n", "... and now addition with broadcasting: \n", "[[12 14 16]\n", " [22 24 26]\n", " [32 34 36]]\n" ] } ], "source": [ "import numpy as np\n", "\n", "A = np.array([ [11, 12, 13], [21, 22, 23], [31, 32, 33] ])\n", "B = np.array([1, 2, 3])\n", "\n", "print(\"Multiplication with broadcasting: \")\n", "print(A * B)\n", "\n", "print(\"... and now addition with broadcasting: \")\n", "print(A + B)" ] }, { "cell_type": "code", "execution_count": 112, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2 3]\n", " [1 2 3]\n", " [1 2 3]]\n" ] } ], "source": [ "## B is treated as if it were constructed like this:\n", "\n", "B = np.array([[1, 2, 3],] * 3)\n", "print(B)" ] }, { "cell_type": "code", "execution_count": 113, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1],\n", " [2],\n", " [3]])" ] }, "execution_count": 113, "metadata": {}, "output_type": "execute_result" } ], "source": [ "### Another way\n", "\n", "#For this example, we need to know how to turn a row vector into a column vector:\n", "\n", "B = np.array([1, 2, 3])\n", "\n", "B[:, np.newaxis]" ] }, { "cell_type": "code", "execution_count": 114, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[11, 12, 13],\n", " [42, 44, 46],\n", " [93, 96, 99]])" ] }, "execution_count": 114, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Now we are capable of doing the multipliplication using broadcasting:\n", "\n", "A * B[:, np.newaxis]" ] }, { "cell_type": "code", "execution_count": 116, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 1, 1],\n", " [2, 2, 2],\n", " [3, 3, 3]])" ] }, "execution_count": 116, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# B is treated as if it were construed like this:\n", "\n", "np.array([[1, 2, 3],] * 3).transpose()" ] }, { "cell_type": "code", "execution_count": 117, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[10],\n", " [20],\n", " [30]])" ] }, "execution_count": 117, "metadata": {}, "output_type": "execute_result" } ], "source": [ "## One more method\n", "\n", "A = np.array([10, 20, 30])\n", "B = np.array([1, 2, 3])\n", "A[:, np.newaxis]" ] }, { "cell_type": "code", "execution_count": 118, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[10, 20, 30],\n", " [20, 40, 60],\n", " [30, 60, 90]])" ] }, "execution_count": 118, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[:, np.newaxis] * B" ] }, { "cell_type": "code", "execution_count": 119, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Multiplication: \n", "[[11 24 39]\n", " [21 44 69]\n", " [31 64 99]]\n", "... and now addition again: \n", "[[12 14 16]\n", " [22 24 26]\n", " [32 34 36]]\n" ] } ], "source": [ "### Another way to do it.\n", "\n", "# Doing it without broadcasting:\n", "\n", "import numpy as np\n", "\n", "A = np.array([ [11, 12, 13], [21, 22, 23], [31, 32, 33] ])\n", "B = np.array([1, 2, 3])\n", "\n", "B = B[np.newaxis, :]\n", "B = np.concatenate((B, B, B))\n", "\n", "print(\"Multiplication: \")\n", "print(A * B)\n", "print(\"... and now addition again: \")\n", "print(A + B)" ] }, { "cell_type": "code", "execution_count": 120, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2 3]\n", " [1 2 3]\n", " [1 2 3]]\n", "Multiplication: \n", "[[11 24 39]\n", " [21 44 69]\n", " [31 64 99]]\n", "... and now addition again: \n", "[[12 14 16]\n", " [22 24 26]\n", " [32 34 36]]\n" ] } ], "source": [ "import numpy as np\n", "\n", "A = np.array([ [11, 12, 13], [21, 22, 23], [31, 32, 33] ])\n", "B = np.tile(np.array([1, 2, 3]), (3, 1))\n", "\n", "print(B)\n", "\n", "print(\"Multiplication: \")\n", "print(A * B)\n", "print(\"... and now addition again: \")\n", "print(A + B)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3-dimensional broadcasting" ] }, { "cell_type": "code", "execution_count": 121, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[[ 9, 16, 49],\n", " [ 5, 0, 1],\n", " [ 2, 2, 15]],\n", "\n", " [[ 3, 0, -7],\n", " [ 8, 0, -4],\n", " [ 5, 4, 3]],\n", "\n", " [[ 6, 4, 21],\n", " [ 1, 0, -4],\n", " [ 5, -4, 12]]])" ] }, "execution_count": 121, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = np.array([ [[3, 4, 7], [5, 0, -1] , [2, 1, 5]],\n", " [[1, 0, -1], [8, 2, 4], [5, 2, 1]],\n", " [[2, 1, 3], [1, 9, 4], [5, -2, 4]]])\n", "\n", "B = np.array([ [[3, 4, 7], [1, 0, -1], [1, 2, 3]] ])\n", "\n", "B * A" ] }, { "cell_type": "code", "execution_count": 123, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1, 3)\n", "(3, 3)\n", "(3, 1, 3)\n", "[[[1 1 1]]\n", "\n", " [[2 2 2]]\n", "\n", " [[3 3 3]]]\n", "[[[ 3 4 7]\n", " [ 5 0 -1]\n", " [ 2 1 5]]\n", "\n", " [[ 2 0 -2]\n", " [16 4 8]\n", " [10 4 2]]\n", "\n", " [[ 6 3 9]\n", " [ 3 27 12]\n", " [15 -6 12]]]\n" ] } ], "source": [ "B = np.array([1, 2, 3])\n", "B = B[np.newaxis, :]\n", "print(B.shape)\n", "\n", "B = np.concatenate((B, B, B)).transpose()\n", "print(B.shape)\n", "B = B[:, np.newaxis]\n", "\n", "print(B.shape)\n", "print(B)\n", "print(A * B)" ] } ], "metadata": { "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.3" } }, "nbformat": 4, "nbformat_minor": 2 }