summaryrefslogtreecommitdiff
path: root/PVCM/cama/en/ma1 np01 Numpy Introduction.ipynb
diff options
context:
space:
mode:
authormartial.simon <martial.simon@epita.fr>2025-04-13 19:54:19 +0200
committermartial.simon <martial.simon@epita.fr>2025-04-13 19:54:19 +0200
commit66c3bbfa94d8a41e58adf154be25e6d86fee8e30 (patch)
tree9c5e998f324f2f60c1717759144da3f996c5ae1a /PVCM/cama/en/ma1 np01 Numpy Introduction.ipynb
init: initial commit
Diffstat (limited to 'PVCM/cama/en/ma1 np01 Numpy Introduction.ipynb')
-rw-r--r--PVCM/cama/en/ma1 np01 Numpy Introduction.ipynb932
1 files changed, 932 insertions, 0 deletions
diff --git a/PVCM/cama/en/ma1 np01 Numpy Introduction.ipynb b/PVCM/cama/en/ma1 np01 Numpy Introduction.ipynb
new file mode 100644
index 0000000..bf30835
--- /dev/null
+++ b/PVCM/cama/en/ma1 np01 Numpy Introduction.ipynb
@@ -0,0 +1,932 @@
+{
+ "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.12.3"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 4,
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "lang": "en"
+ },
+ "source": [
+ "# NumPy - N-dimensional Array manipulations library\n",
+ "\n",
+ "[NumPy](https://docs.scipy.org/doc/numpy/user/index.html) is a toolkit for scientific program development. It\n",
+ "provides multidimensional arrays that are simple to manipulate and **efficient** (which Python lists are not).\n",
+ "\n",
+ "Numpy's syntax is also used in other languages like R or Julia. In AI, TensorFlow, PyTorch and JAX use also this syntax."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "import numpy as np # np is the convention"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "lang": "en"
+ },
+ "source": [
+ "Unlike lists, __tables have all their values of the same type__.\n",
+ "\n",
+ "## Creating an array"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "[[1 2 3]\n",
+ " [4 5 7]]\n",
+ "shape: (2, 3)\n",
+ "type: <class 'numpy.int64'>\n"
+ ]
+ }
+ ],
+ "source": [
+ "a = np.array([[1,2,3], [4,5,7]]) # 2D array built from a list\n",
+ "print(a)\n",
+ "print(\"shape: \",a.shape)\n",
+ "print(\"type: \",type(a[0,0]))"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "lang": "en"
+ },
+ "source": [
+ "#### dtype: the choice of weapons\n",
+ "\n",
+ "When doing scientific calculation it is important to choose the type of tables to optimize memory, errors, speed.\n",
+ "\n",
+ "NumPy offers the following types:\n",
+ "\n",
+ "<table border=\"1\" class=\"docutils\">\n",
+ "<colgroup>\n",
+ "<col width=\"15%\" />\n",
+ "<col width=\"85%\" />\n",
+ "</colgroup>\n",
+ "<thead valign=\"bottom\">\n",
+ "<tr class=\"row-odd\"><th class=\"head\">Data type</th>\n",
+ "<th class=\"head\">Description</th>\n",
+ "</tr>\n",
+ "</thead>\n",
+ "<tbody valign=\"top\">\n",
+ "<tr class=\"row-even\"><td>bool</td>\n",
+ "<td>Boolean (True or False) stored as a byte</td>\n",
+ "</tr>\n",
+ "<tr class=\"row-odd\"><td>int</td>\n",
+ "<td>Platform integer (usually either <tt class=\"docutils literal\"><span class=\"pre\">int32</span></tt> or <tt class=\"docutils literal\"><span class=\"pre\">int64</span></tt>) </td>\n",
+ "</tr>\n",
+ "<tr class=\"row-even\"><td>int8</td>\n",
+ "<td>Byte (-128 to 127) </td>\n",
+ "</tr>\n",
+ "<tr class=\"row-odd\"><td>int16</td>\n",
+ "<td>Integer (-32768 to 32767) </td>\n",
+ "</tr>\n",
+ "<tr class=\"row-even\"><td>int32</td>\n",
+ "<td>Integer (-2147483648 to 2147483647) </td>\n",
+ "</tr>\n",
+ "<tr class=\"row-odd\"><td>int64</td>\n",
+ "<td>Integer (9223372036854775808 to 9223372036854775807) </td>\n",
+ "</tr>\n",
+ "<tr class=\"row-even\"><td>uint8</td>\n",
+ "<td>Unsigned integer (0 to 255) </td>\n",
+ "</tr>\n",
+ "<tr class=\"row-odd\"><td>uint16</td>\n",
+ "<td>Unsigned integer (0 to 65535) </td>\n",
+ "</tr>\n",
+ "<tr class=\"row-even\"><td>uint32</td>\n",
+ "<td>Unsigned integer (0 to 4294967295) </td>\n",
+ "</tr>\n",
+ "<tr class=\"row-odd\"><td>uint64</td>\n",
+ "<td>Unsigned integer (0 to 18446744073709551615) </td>\n",
+ "</tr>\n",
+ "<tr class=\"row-even\"><td>float</td>\n",
+ "<td>Shorthand for <tt class=\"docutils literal\"><span class=\"pre\">float64</span></tt>.</td>\n",
+ "</tr>\n",
+ "<tr class=\"row-odd\"><td>float16</td>\n",
+ "<td>Half precision float: sign bit, 5 bits exponent,\n",
+ "10-bit mantissa</td>\n",
+ "</tr>\n",
+ "<tr class=\"row-even\"><td>float32</td>\n",
+ "<td>Single precision float: sign bit, 8 bits exponent,\n",
+ "23-bit mantissa</td>\n",
+ "</tr>\n",
+ "<tr class=\"row-odd\"><td>float64</td>\n",
+ "<td>Double precision float: sign bit, 11 bits exponent,\n",
+ "52-bit mantissa</td>\n",
+ "</tr>\n",
+ "<tr class=\"row-even\"><td>complex</td>\n",
+ "<td>Shorthand for <tt class=\"docutils literal\"><span class=\"pre\">complex128</span></tt>.</td>\n",
+ "</tr>\n",
+ "<tr class=\"row-odd\"><td>complex64</td>\n",
+ "<td>Complex number, represented by two 32-bit floats (real\n",
+ "and imaginary components) </td>\n",
+ "</tr>\n",
+ "<tr class=\"row-even\"><td>complex128</td>\n",
+ "<td>Complex number, represented by two 64-bit floats (real\n",
+ "and imaginary components) </td>\n",
+ "</tr>\n",
+ "</tbody>\n",
+ "</table>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "x = [0 1 2 3] uint8\n",
+ "x = [254 1 2 3] uint8\n",
+ "y = [254. 1. 2. 3.] float32\n"
+ ]
+ }
+ ],
+ "source": [
+ "x = np.arange(4, dtype= np.uint8) # arange is the numpy version of range to produce an array\n",
+ "print(\"x =\", x, x.dtype)\n",
+ "\n",
+ "x[0] = -2 # 0 - 2 = max -1 for unsigned int\n",
+ "print(\"x =\", x, x.dtype)\n",
+ "\n",
+ "y = x.astype('float32') # conversion\n",
+ "print(\"y =\", y, y.dtype)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "lang": "en"
+ },
+ "source": [
+ "We can know the memory size (in bytes) occupied by an element of the array:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "1\n",
+ "4\n"
+ ]
+ },
+ {
+ "data": {
+ "text/plain": [
+ "16"
+ ]
+ },
+ "execution_count": 4,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "print(x[0].itemsize)\n",
+ "print(y[0].itemsize)\n",
+ "y.nbytes"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "lang": "en"
+ },
+ "source": [
+ "#### Predefined methods\n",
+ "\n",
+ "There is also the possibility to create an empty or predefined array of the dimension of your choice using\n",
+ "[predefined creation methods](https://docs.scipy.org/doc/numpy/reference/routines.array-creation.html):"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Empty float:\n",
+ " [[2.42350504e-316 0.00000000e+000]\n",
+ " [4.94065646e-324 nan]]\n",
+ "Float zeros:\n",
+ " [[0. 0.]\n",
+ " [0. 0.]]\n",
+ "Complex ones:\n",
+ " [[1.+0.j 1.+0.j 1.+0.j]\n",
+ " [1.+0.j 1.+0.j 1.+0.j]]\n",
+ "Full of 3.2:\n",
+ " [[3.2 3.2]\n",
+ " [3.2 3.2]]\n",
+ "La matrice suivante est affichée partiellement car trop grande : \n",
+ "[[1. 0. 0. ... 0. 0. 0.]\n",
+ " [0. 1. 0. ... 0. 0. 0.]\n",
+ " [0. 0. 1. ... 0. 0. 0.]\n",
+ " ...\n",
+ " [0. 0. 0. ... 1. 0. 0.]\n",
+ " [0. 0. 0. ... 0. 1. 0.]\n",
+ " [0. 0. 0. ... 0. 0. 1.]]\n"
+ ]
+ }
+ ],
+ "source": [
+ "a = np.empty((2,2), dtype=float) # empty do not set any value, it is faster\n",
+ "print(\"Empty float:\\n\", a)\n",
+ "\n",
+ "print(\"Float zeros:\\n\", np.zeros((2,2), dtype=float)) # matrix filled with 0\n",
+ "\n",
+ "print(\"Complex ones:\\n\", np.ones((2,3), dtype=complex)) # matrix filled with 1\n",
+ "\n",
+ "print(\"Full of 3.2:\\n\", np.full((2,2), 3.2))\n",
+ "\n",
+ "print(\"La matrice suivante est affichée partiellement car trop grande : \")\n",
+ "print(np.identity(1000)) # identity matrix"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "lang": "en"
+ },
+ "source": [
+ "#### With random values\n",
+ "\n",
+ "The sub-library [`np.random`](https://docs.scipy.org/doc/numpy/reference/routines.random.html) provides methods for generating arrays of random values."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Random integers < 10:\n",
+ " [[7 5 7 2]\n",
+ " [6 7 0 8]\n",
+ " [6 3 8 3]]\n",
+ "Random reals between 0 and 1 :\n",
+ " [[0.45228105 0.39674551 0.02142443 0.14342572]\n",
+ " [0.95223097 0.85798264 0.85777533 0.28784271]\n",
+ " [0.5142878 0.36823048 0.87704756 0.44096786]]\n"
+ ]
+ }
+ ],
+ "source": [
+ "print(\"Random integers < 10:\\n\", np.random.randint(10, size=(3,4))) # can also choose a min\n",
+ "\n",
+ "print(\"Random reals between 0 and 1 :\\n\", np.random.random(size=(3,4)))"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "lang": "en"
+ },
+ "source": [
+ "We can choose the law of distribution (uniform law by default)."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "array([[1.44216124, 2.5586318 , 4.28770816],\n",
+ " [2.16814785, 1.82332334, 4.53031302]])"
+ ]
+ },
+ "execution_count": 7,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "loc = 3\n",
+ "scale = 1.5\n",
+ "np.random.normal(loc, scale, size=(2,3)) # Gauss distribution"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "lang": "en"
+ },
+ "source": [
+ "### By redefining its shape\n",
+ "\n",
+ "A classic case for testing is to create a small multidimensional array with different values.\n",
+ "For this the simplest is to put 0,1,2,...,N in the boxes of our form table (3,4) for example.\n",
+ "This is done with `arange` which we have already seen to generate the values and `reshape` to have the desired form:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 8,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "array([[ 0, 1, 2, 3],\n",
+ " [ 4, 5, 6, 7],\n",
+ " [ 8, 9, 10, 11]])"
+ ]
+ },
+ "execution_count": 8,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "arr = np.arange(3*4).reshape((3,4))\n",
+ "arr"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 9,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "4"
+ ]
+ },
+ "execution_count": 9,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "arr[1, 0]"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "lang": "en"
+ },
+ "source": [
+ "Attention, the numbering of the boxes of a table is carried out with nested loops. It is always the last dimension which varies most quickly. In 3D this means that going from one element to the next varies the last index (along z). This does not correspond to the human way of filling a cube (we tend to stack 2D arrays). In \n",
+ "regular use this is not a problem, it is\n",
+ "only weird when printing tests to check.\n",
+ "\n",
+ "```\n",
+ " human Numpy\n",
+ "   ┌─┬─┐ ┌─┬─┐\n",
+ "┌─┬─┐│5│ ┌─┬─┐│3│\n",
+ "│0│1│┼─┤ │0│2│┼─┤\n",
+ "├─┼─┤│7│ ├─┼─┤│7│\n",
+ "│2│3│┴─┘ │4│6│┴─┘\n",
+ "└─┴─┘ └─┴─┘\n",
+ "```"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 10,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "array([[[0, 1],\n",
+ " [2, 3]],\n",
+ "\n",
+ " [[4, 5],\n",
+ " [6, 7]]])"
+ ]
+ },
+ "execution_count": 10,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "A = np.arange(8).reshape(2,2,2) # a[0,1,0] is 2 and a[0,1,1] is 3\n",
+ "A"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "lang": "en"
+ },
+ "source": [
+ "The reverse of reshape is `flatten()` which reshape a multi-dimensional array into a 1-dimensional array. We can also use `flat` to have a 1D view on the array without reshaping it."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 11,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "5\n",
+ "(2, 2, 2)\n"
+ ]
+ }
+ ],
+ "source": [
+ "print(A.flat[5])\n",
+ "print(A.shape)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "lang": "en"
+ },
+ "source": [
+ "### Mixing values\n",
+ "\n",
+ "If we want to work with the values of an array taken in a random order then we can mix\n",
+ "the table with `np.random.permutation()`. Attention the permutation is carried out on the elements of the first level of the array, `A[:]`, i.e. arrays\n",
+ "if the array is multi-dimensional (an array of arrays)."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 12,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "array([[ 4, 5, 6, 7],\n",
+ " [ 8, 9, 10, 11],\n",
+ " [ 0, 1, 2, 3]])"
+ ]
+ },
+ "execution_count": 12,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "data = np.arange(12).reshape(3,4)\n",
+ "np.random.permutation(data) # permutes lines only"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "lang": "en"
+ },
+ "source": [
+ "If you want to mix all the values ​​you have to flatten the table and give it back its shape:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 13,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "array([[10, 3, 11, 8],\n",
+ " [ 4, 1, 2, 7],\n",
+ " [ 0, 9, 6, 5]])"
+ ]
+ },
+ "execution_count": 13,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "data = np.random.permutation(data.flatten()).reshape(data.shape) # it works because flatten returns a copy\n",
+ "data"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "lang": "en"
+ },
+ "source": [
+ "#### With its own function\n",
+ "\n",
+ "We can also create an array with a function that gives the value of the array for each index (i, j):"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 14,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "array([[ 0, -1, -2, -3],\n",
+ " [ 2, 1, 0, -1],\n",
+ " [ 4, 3, 2, 1]])"
+ ]
+ },
+ "execution_count": 14,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "def f(i,j):\n",
+ " return 2*i - j\n",
+ "\n",
+ "np.fromfunction(f, shape=(3,4), dtype=int)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "lang": "en"
+ },
+ "source": [
+ "## Basic Operations\n",
+ "\n",
+ "Numpy makes it possible to apply the usual operations to all the elements of the array:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 15,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "A + 1:\n",
+ " [[2 3]\n",
+ " [4 5]] \n",
+ "\n",
+ "2 A + Id:\n",
+ " [[3. 4.]\n",
+ " [6. 9.]] \n",
+ "\n",
+ "A * A (element-wise product):\n",
+ " [[ 1 4]\n",
+ " [ 9 16]] \n",
+ "\n",
+ "A @ A (matrix or dot product):\n",
+ " [[ 7 10]\n",
+ " [15 22]]\n"
+ ]
+ }
+ ],
+ "source": [
+ "A = np.array([[1,2], [3,4]])\n",
+ "\n",
+ "print(\"A + 1:\\n\", A + 1, '\\n')\n",
+ "print(\"2 A + Id:\\n\", 2 * A + np.identity(2), '\\n')\n",
+ "print(u\"A * A (element-wise product):\\n\", A * A, '\\n') # or np.square(A)\n",
+ "print(u\"A @ A (matrix or dot product):\\n\", A @ A) # or A.dot(A)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "lang": "en"
+ },
+ "source": [
+ "You can transpose an array with `.T`. It does nothing with a 1D array, to make a horizontal vector or a vertical vector, you have to write it in 2D:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 16,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "[[1 3 5]] \n",
+ "\n",
+ " [[1]\n",
+ " [3]\n",
+ " [5]] \n",
+ "\n",
+ "Guess what v + v.T means:\n",
+ " [[ 2 4 6]\n",
+ " [ 4 6 8]\n",
+ " [ 6 8 10]]\n"
+ ]
+ }
+ ],
+ "source": [
+ "v = np.array([[1,3,5]])\n",
+ "print(v, '\\n\\n', v.T, '\\n')\n",
+ "print(\"Guess what v + v.T means:\\n\", v + v.T)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "lang": "en"
+ },
+ "source": [
+ "Trigonometric functions and other usual mathematical functions are also available."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 17,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "array([[ 0.841, 0.909],\n",
+ " [ 0.141, -0.757]])"
+ ]
+ },
+ "execution_count": 17,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "np.set_printoptions(precision=3) # set printing precision for reals\n",
+ "np.sin(A)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "lang": "en"
+ },
+ "source": [
+ "Finally Numpy offers a set of methods to perform calculations on array elements:\n",
+ "\n",
+ "* `sum()` to sum all elements\n",
+ "* `mean()` to get the average of the elements and `average()` to get the weighted average,\n",
+ "* `prod()` to multiply all elements,\n",
+ "* `min()` and `max()` to get the minimum value and the maximum value,\n",
+ "* `argmin()` and `argmax()` to have the array indices of the minimum and maximum value,\n",
+ "* `cumsum()` and `cumprod()` for cumulative addition and multiplication,\n",
+ "* `diff()` to get the gap with the next element (useful to calculate a derivative).\n",
+ "\n",
+ "Each `A.sum()` method also exists as a `np.sum(A)` function."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 18,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "3"
+ ]
+ },
+ "execution_count": 18,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "A.argmax()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 19,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "array([1, 1, 1])"
+ ]
+ },
+ "execution_count": 19,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "np.diff(A.flatten())"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "lang": "en"
+ },
+ "source": [
+ "## Browse an array\n",
+ "\n",
+ "The natural way to browse all the elements of a multi-dimensional array is to make a loop for each dimension:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 20,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ " 0 1\n",
+ " 2 3\n",
+ " 4 5\n"
+ ]
+ }
+ ],
+ "source": [
+ "a = np.arange(6).reshape(3,2)\n",
+ "for ligne in a:\n",
+ " for element in ligne:\n",
+ " print(\" \", element, end=\"\") # end=\"\" avoid the return after each print\n",
+ " print()"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "lang": "en"
+ },
+ "source": [
+ "We saw in the manipulation of the form of an array that we can make it flatter, but rather than using `flatten()` which makes an array in 1 dimension, we prefer to use `flat` which gives an [iterator](https://en.wikipedia.org/wiki/Iterator) to iterate through all the elements of the array."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 21,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "0\n",
+ "1\n",
+ "2\n",
+ "3\n",
+ "4\n",
+ "5\n"
+ ]
+ }
+ ],
+ "source": [
+ "for v in a.flat:\n",
+ " print(v)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "lang": "en"
+ },
+ "source": [
+ "It is also possible to make a loop on the indices but it is clearly less powerful."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 22,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "0\n",
+ "1\n",
+ "2\n",
+ "3\n",
+ "4\n",
+ "5\n"
+ ]
+ }
+ ],
+ "source": [
+ "for i in range(len(a)):\n",
+ " for j in range(len(a[i])):\n",
+ " print(a[i,j])"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "lang": "en"
+ },
+ "source": [
+ "## Think vector\n",
+ "\n",
+ "Making loops is often our way of thinking but it is not effective. It is better to\n",
+ "to work directly on the whole array. So rather than making the loop\n",
+ "\n",
+ "```\n",
+ "for i in range (len (x)):\n",
+ "   z[i] = x[i] + y[i]\n",
+ "```\n",
+ "\n",
+ "we will do\n",
+ "\n",
+ "```\n",
+ "z = x + y\n",
+ "```\n",
+ "   \n",
+ "Not only is it more readable but it is also faster."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 23,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "45.5 ms ± 494 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n",
+ "35.3 ms ± 139 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n",
+ "36.9 µs ± 156 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n"
+ ]
+ }
+ ],
+ "source": [
+ "def double_loop(a):\n",
+ " for i in range(a.shape[0]):\n",
+ " for j in range(a.shape[1]):\n",
+ " a[i,j] = np.sqrt(a[i,j]) # change in-place\n",
+ "\n",
+ "def iterate(a):\n",
+ " for x in a.flat:\n",
+ " x = np.sqrt(x) # modification not saved in a, x is a local var\n",
+ "\n",
+ "b = np.random.random(size=(200,200))\n",
+ "a = b.copy() # we need a copy to be sure to use the same data each time\n",
+ "%timeit double_loop(a)\n",
+ "\n",
+ "a = b.copy() \n",
+ "%timeit iterate(a)\n",
+ "\n",
+ "a = b.copy()\n",
+ "%timeit np.sqrt(a) # vectorial operation, 1000 times faster"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ }
+ ]
+} \ No newline at end of file