From 48a2f6853da15812091c8a760113875e46030c93 Mon Sep 17 00:00:00 2001 From: Mirko Birbaumer <mirko.birbaumer@hslu.ch> Date: Fri, 16 Sep 2022 10:14:13 +0000 Subject: [PATCH] Adapted Numpy Pandas Intro Notebook --- .../Preliminaries_Numpy_Pandas.ipynb | 520 ++++++++++++++++++ 1 file changed, 520 insertions(+) create mode 100644 notebooks/Block_0/Examples script/Preliminaries_Numpy_Pandas.ipynb diff --git a/notebooks/Block_0/Examples script/Preliminaries_Numpy_Pandas.ipynb b/notebooks/Block_0/Examples script/Preliminaries_Numpy_Pandas.ipynb new file mode 100644 index 0000000..0fcbd39 --- /dev/null +++ b/notebooks/Block_0/Examples script/Preliminaries_Numpy_Pandas.ipynb @@ -0,0 +1,520 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 3.2 Funktions, Conditionals, and Iteration in Python\n", + "\n", + "Let us create a Python function, and call it from a loop." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def HelloWorldXY(x, y):\n", + " if (x < 10):\n", + " print(\"Hello World, x was < 10\")\n", + " elif (x < 20):\n", + " print(\"Hello World, x was >= 10 but < 20\")\n", + " else:\n", + " print(\"Hello World, x was >= 20\")\n", + " return x + y\n", + "\n", + "print(HelloWorldXY(1,2))\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now let us call the function `HelloWorldXY()` from a loop:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "--- Now running with i: 8\n" + ] + }, + { + "ename": "NameError", + "evalue": "name 'HelloWorldXY' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m<ipython-input-1-7ea2350df544>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m8\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m25\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;31m# i=8, 13, 18, 23 (start, stop, step)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"\\n--- Now running with i: {}\"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mr\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mHelloWorldXY\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Result from HelloWorld: {}\"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mr\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mNameError\u001b[0m: name 'HelloWorldXY' is not defined" + ] + } + ], + "source": [ + "for i in range(8, 25, 5): # i=8, 13, 18, 23 (start, stop, step)\n", + " print(\"\\n--- Now running with i: {}\".format(i))\n", + " r = HelloWorldXY(i,i)\n", + " print(\"Result from HelloWorld: {}\".format(r))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you want a loop starting at 0 to 2 (exclusive) you could do any of the following:" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Iterate over the items. `range(2)` is like a list [0,1].\n", + "0\n", + "1\n", + "Iterate over an actual list.\n", + "0\n", + "1\n", + "While works\n", + "0\n", + "1\n", + "Python supports standard key words like continue and break\n", + "Entered while\n", + "while broken\n" + ] + } + ], + "source": [ + "print(\"Iterate over the items. `range(2)` is like a list [0,1].\")\n", + "for i in range(2):\n", + " print(i)\n", + "\n", + "print(\"Iterate over an actual list.\")\n", + "for i in [0,1]:\n", + " print(i)\n", + "\n", + "print(\"While works\")\n", + "i = 0\n", + "while i < 2:\n", + " print(i)\n", + " i += 1\n", + " \n", + "print(\"Python supports standard key words like continue and break\")\n", + "while True:\n", + " print(\"Entered while\")\n", + " break\n", + "print(\"while broken\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 3.3 Data in Numpy" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Shape scaler () \n", + "Shape vector (3,) \n", + "Shape matrix (3, 3) \n", + "Shape tensor (3, 3, 2, 1)\n", + "Type scalar or array <class 'numpy.ndarray'> \n", + "Type after addition with integer <class 'numpy.int64'>\n", + "v[1:] = [ 2 10] \n", + "m[1:][2:] = \n", + " [[5 6]\n", + " [8 9]]\n", + "[ 1 2 10] [[ 1 2 10]] [[ 1 2 10]]\n", + "(3,) (1, 3) (1, 3)\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "\n", + "# Scalar\n", + "s = np.array(5)\n", + "# Vector\n", + "v = np.array([1, 2, 10])\n", + "# Matrix\n", + "m = np.array([[1,2,3], \n", + " [4,5,6], \n", + " [7,8,9]])\n", + "# Tensor:\n", + "t = np.array([[[[1],[2]], [[3],[4]], [[5],[6]]],\n", + " [[[7],[8]], [[9],[10]], [[11],[12]]],\n", + " [[[13],[14]], [[15],[16]], [[17],[17]]]])\n", + "\n", + "# Shape\n", + "print(\"Shape scaler\", s.shape, \"\\nShape vector\", v.shape, \"\\nShape matrix\", m.shape, \"\\nShape tensor\", t.shape)\n", + "\n", + "# Type\n", + "print(\"Type scalar or array\", type(s), \"\\nType after addition with integer\", type(s + 3))\n", + "\n", + "# Slicing\n", + "print(\"v[1:] = \", v[1:], \"\\nm[1:][2:] = \\n\", m[1:,1:])\n", + "\n", + "# Reshape arrays\n", + "x = v.reshape(1, 3)\n", + "y = v[None, :]\n", + "print(v, x, y)\n", + "print(v.shape, x.shape, y.shape)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 3.4 Element-wise Operations" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[6, 7, 8, 9, 10]\n", + "[ 6 7 8 9 10]\n", + "[30 35 40 45 50] \n", + " [30 35 40 45 50] \n", + "\n", + "a =\n", + " [[1 3]\n", + " [5 7]] \n", + "b =\n", + " [[2 4]\n", + " [6 8]]\n", + "a + b =\n", + " [[ 3 7]\n", + " [11 15]]\n", + "a * b =\n", + " [[ 2 12]\n", + " [30 56]]\n" + ] + }, + { + "ename": "ValueError", + "evalue": "operands could not be broadcast together with shapes (2,2) (5,) ", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m<ipython-input-4-7ddd6b5f4e75>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 24\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"a * b =\\n\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 25\u001b[0m \u001b[0;31m# Shape mismatch:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 26\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"a * values =\\n\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0mvalues\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mValueError\u001b[0m: operands could not be broadcast together with shapes (2,2) (5,) " + ] + } + ], + "source": [ + "# The Python way:\n", + "values = [1, 2, 3, 4, 5]\n", + "for i in range(len(values)):\n", + " values[i] += 5\n", + " \n", + "print(values)\n", + "\n", + "# The Numpy way:\n", + "values = np.array([1, 2, 3, 4, 5])\n", + "values += 5\n", + "\n", + "print(values)\n", + "\n", + "# Multiplication\n", + "x = np.multiply(values, 5)\n", + "y = values * 5\n", + "print(x, \"\\n\", y, \"\\n\")\n", + "\n", + "# Element wise matrix operations\n", + "a = np.array([[1,3],[5,7]])\n", + "b = np.array([[2,4],[6,8]])\n", + "print(\"a =\\n\", a, \"\\nb =\\n\", b)\n", + "print(\"a + b =\\n\", a + b)\n", + "print(\"a * b =\\n\", a * b)\n", + "# Shape mismatch:\n", + "print(\"a * values =\\n\", a * values)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Numpy Matrix Multiplication\n", + "Recap element-wise multiplication:" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "m =\n", + " [[1 2 3]\n", + " [4 5 6]] \n", + "n =\n", + " [[0.25 0.5 0.75]\n", + " [1. 1.25 1.5 ]]\n", + "x =\n", + " [[0.25 1. 2.25]\n", + " [4. 6.25 9. ]] \n", + "y =\n", + " [[0.25 1. 2.25]\n", + " [4. 6.25 9. ]]\n" + ] + } + ], + "source": [ + "# Elementwise recap:\n", + "m = np.array([[1,2,3],[4,5,6]])\n", + "# Scalar multiplication\n", + "n = m * 0.25\n", + "# Python Elementwise matrix multiplication\n", + "x = m * n\n", + "# Numpy Elementwise matrix multiplication\n", + "y = np.multiply(m, n)\n", + "\n", + "print(\"m =\\n\", m, \"\\nn =\\n\", n)\n", + "print(\"x =\\n\", x, \"\\ny =\\n\", y)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Matrix Product:" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a =\n", + " [[1 2 3 4]\n", + " [5 6 7 8]] \n", + "a.shape =\n", + " (2, 4) \n", + "b =\n", + " [[ 1 2 3]\n", + " [ 4 5 6]\n", + " [ 7 8 9]\n", + " [10 11 12]] \n", + "b.shape =\n", + " (4, 3)\n", + "c = \n", + " [[ 70 80 90]\n", + " [158 184 210]] \n", + "c.shape =\n", + " (2, 3)\n", + "d = \n", + " [[ 70 80 90]\n", + " [158 184 210]] \n", + "d.shape =\n", + " (2, 3)\n" + ] + } + ], + "source": [ + "\"\"\" Using np.matmul \"\"\"\n", + "a = np.array([[1,2,3,4],[5,6,7,8]])\n", + "b = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])\n", + "\n", + "print(\"a =\\n\", a, \"\\na.shape =\\n\", a.shape, \"\\nb =\\n\", b, \"\\nb.shape =\\n\", b.shape)\n", + "\n", + "# Matrix product\n", + "c = np.matmul(a, b)\n", + "print(\"c = \\n\", c, \"\\nc.shape =\\n\", c.shape)\n", + "\n", + "# Dimension mismatch:\n", + "# print(np.matmul(b, a))\n", + "\"\"\" Using np.dot \"\"\"\n", + "d = np.dot(a, b)\n", + "print(\"d = \\n\", d, \"\\nd.shape =\\n\", d.shape)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Transpose" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "m = \n", + " [[ 1 2 3 4]\n", + " [ 5 6 7 8]\n", + " [ 9 10 11 12]] \n", + "m.T = \n", + " [[ 1 5 9]\n", + " [ 2 6 10]\n", + " [ 3 7 11]\n", + " [ 4 8 12]]\n", + "m = \n", + " [[ 1 2 3 4]\n", + " [ 5 6 7 200]\n", + " [ 9 10 11 12]] \n", + "m_t = \n", + " [[ 1 5 9]\n", + " [ 2 6 10]\n", + " [ 3 7 11]\n", + " [ 4 200 12]]\n", + "entries [3][1], [1][3], respectively are edited in both matrices\n" + ] + } + ], + "source": [ + "m = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])\n", + "print(\"m = \\n\", m,\"\\nm.T = \\n\", m.T)\n", + "\n", + "# note how the transposed matrix is not a copy of the original:\n", + "m_t = m.T\n", + "m_t[3][1] = 200\n", + "print(\"m = \\n\", m, \"\\nm_t = \\n\", m_t)\n", + "print(\"entries [3][1], [1][3], respectively are edited in both matrices\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## A real use case" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[-0.27 0.45 0.64 0.31]] (1, 4)\n", + "[[ 0.02 0.001 -0.03 0.036]\n", + " [ 0.04 -0.003 0.025 0.009]\n", + " [ 0.012 -0.045 0.28 -0.067]] (3, 4)\n", + "Matrix multiplication gives:\n", + " [[-0.01299 0.00664 0.13494]] \n", + "or, equivalently:\n", + " [[-0.01299]\n", + " [ 0.00664]\n", + " [ 0.13494]]\n" + ] + } + ], + "source": [ + "inputs = np.array([[-0.27, 0.45, 0.64, 0.31]])\n", + "print(inputs, inputs.shape)\n", + "\n", + "weights = np.array([[0.02, 0.001, -0.03, 0.036], \n", + " [0.04, -0.003, 0.025, 0.009], \n", + " [0.012, -0.045, 0.28, -0.067]])\n", + "print(weights, weights.shape)\n", + "\n", + "print(\"Matrix multiplication gives:\\n\", np.matmul(inputs, weights.T), \"\\nor, equivalently:\\n\", np.matmul(weights, inputs.T))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Some more useful Numpy methods" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Showing some basic math on arrays\n", + "Max: 4\n", + "Average: 2.0\n", + "Max index: 2\n", + "\n", + "Use numpy to create a [3,3] dimension array with random number\n", + "[[0.92371879 0.58999086 0.76979433]\n", + " [0.48733651 0.44698554 0.91494542]\n", + " [0.59130531 0.69632003 0.32785335]]\n" + ] + } + ], + "source": [ + "print(\"\\nShowing some basic math on arrays\")\n", + "\n", + "b = np.array([0,1,4,3,2])\n", + "print(\"Max: {}\".format(np.max(b)))\n", + "print(\"Average: {}\".format(np.average(b)))\n", + "print(\"Max index: {}\".format(np.argmax(b)))\n", + "\n", + "print(\"\\nUse numpy to create a [3,3] dimension array with random number\")\n", + "c = np.random.rand(3, 3)\n", + "print(c)" + ] + } + ], + "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.6" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} -- GitLab