{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Before you turn this problem in, make sure everything runs as expected. First, **restart the kernel** (in the menubar, select Kernel$\\rightarrow$Restart) and then **run all cells** (in the menubar, select Cell$\\rightarrow$Run All).\n", "\n", "Make sure you fill in any place that says `YOUR CODE HERE` or \"YOUR ANSWER HERE\", as well as your name below." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "NAME = \"\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction to Data Science\n", "## Lab 1: Brief introduction to Python and Jupyter" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This course assumes that you are comfortable with the basic functions of Jupyter and Python." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Part A: Introduction Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With the command `a = [1, 2, 3]` you can set up a list." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "4babbadc40ca925b551e495c06cf20c4", "grade": false, "grade_id": "cell-ef61d944d6baafc5", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# Type the command a = [1, 2, 3] and hit Shift + Enter\n", "# to execute this code cell and jump to the next cell.\n", "\n", "# YOUR CODE HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With the function type, we can find out that `a` is of type *list*." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "ce950d4e1b28decdda441227a4d9ed28", "grade": false, "grade_id": "cell-5dff3198c0f31109", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# Type the command \n", "#\n", "# type(a)\n", "#\n", "# and hit Shift + Enter to execute this code cell\n", "# and jump to the next cell.\n", "\n", "# YOUR CODE HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By the way, comments can be set in various ways.\n", "The can start either with the hashtag sign **#**, i.e., \n", " \n", " # This is your code\n", " \n", "or, for longer comments, start and end with three quotation marks **\"\"\"**, i.e.\n", " \n", " \"\"\" This comment\n", " \n", " be\n", " across\n", " multiple\n", " lines...\n", " \n", " ... and may also include empty lines.\n", " \"\"\"\n", "\n", "They should become a vital part of your code, and often help to increase the readability of your programs." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The expression `a[1]` accesses the **second** element in the list `a`. In contrast to other programming languages such as *MATLAB*, python starts its enumeration with index `0`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "8fb31838671e5b66224812a06a20ba5a", "grade": false, "grade_id": "cell-adf6ca5dad89aca9", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# Type the command \n", "#\n", "# a[1]\n", "#\n", "# and hit Shift + Enter to execute this code cell\n", "# and jump to the next cell.\n", "\n", "# YOUR CODE HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Similar to `type()`, `len()` is a Python function that can be applied to different objects. It returns the length of the object.\n", "Find out the length of the list `a`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "c163d08736051d94a0eef1f003d456b6", "grade": false, "grade_id": "cell-604e067848d71be3", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, set up a list `b` with elements `[4, 5, 6]`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "9258cf9368e65e88dda36deccf2bdb67", "grade": false, "grade_id": "cell-a2bfdf28dbcbca46", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Try the command `a + b`. What happens if you multiply the list `a` by a scalar?\n", "\n", "If you have multiple commands that yield an output, only the last one is displayed. You can show it by putting the command inside the `print()` function." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "9a443756e7ee40f15555ec4333e85c61", "grade": false, "grade_id": "cell-d44e98983a6dec33", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To find out what else we can do with a list, we may type `a.` and press the **Tab** key. In Jupyter and other programming environments, a listing will be displayed with the available methods of the\n", "class *list*.\n", "You can, for example, append the number 1 to the list `a` by evaluating `a.append(1)`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "9a90dd47e1de2d45dc4a996851b04f27", "grade": false, "grade_id": "cell-41fe2e77a4dabeee", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# Type the command \n", "#\n", "# a.append(1)\n", "#\n", "# and hit Shift + Enter to execute this code cell\n", "# and jump to the next cell.\n", "\n", "# YOUR CODE HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can always type a question mark **?** in front or behind of a function, e.g., `? a.append`, in order to see the built-in documentation of a function.\n", "Depending on how extensively the function/method is commented this\n", "can be more or less helpful.\n", "Now, you should sort the list `a` in descending order.\n", "\n", "**Task**: Find a suitable function by typing `a.` and use the **Tab** key to get a listing of the various methods available for a list.\n", "Then use the question mark to find out the function's behaviour." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "4268346a23cae2656ad635ec5d90a1cf", "grade": false, "grade_id": "cell-8222ee4d50c8ab9c", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We conclude, that the method has two optional inputs, namely `key` with default value `None` and `reverse` with default value `False`.\n", "You can close the help dialogue by typing **q**.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Task**: Now sort the list in descending order. Since this function has no output, you may type `print(a)` in a new line to check whether your sorting has been successful." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "1ecf0a2d18a5f289a6028a28f8739beb", "grade": false, "grade_id": "cell-a392ea066e962016", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Task**: Insert the number `5` at the second position of `a`.\n", "To achieve this, look for a suitable method and become comfortable with it using `?`.\n", "\n", "Finally, you should have `a = [3, 5, 2, 1, 1]`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "092280ab3298613ba365595d1b460b36", "grade": false, "grade_id": "cell-6198c8ff5d9bb816", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plain python without additional modules is not capable of handling advanced mathematical problems.\n", "Packages extend the functionalities of python in different ways.\n", "We will introduce some of them in the upcoming exercises." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Part B: Introduction Numpy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As already indicated, packages are extremly important in `Python`.\n", "Undoubtedly, one of the most useful package for scientific computing is numpy, which is imported by typing\n", " \n", " import numpy as np\n", "\n", "Please familiarize yourself with one of the many tutorials, e.g.,\n", "\n", "https://docs.scipy.org/doc/numpy/user/quickstart.html\n", "\n", "\n", "For those who have previously worked with MATLAB, the following overview is also worth a short look\n", "\n", "https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html.\n", "\n", "**Short note**: It is not common to import all functions of a package via `from numpy import ∗`.\n", "Instead, one uses `import numpy` or `import numpy as np`, which provides access to the package’s functions via `numpy.ndarray` or `np.ndarray`, resp." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Execute the following code block to initialize the matrix\n", "\n", "$$\n", "A = \\begin{pmatrix} 1 & -1 \\\\ -1 & 1\\end{pmatrix}\n", "$$\n", "\n", "as an array.\n", "\n", "*Note*: Numpy also supports the data type `matrix` which is no longer recommended, even for linear algebra. Instead use arrays as we'll do throughout our labs." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "A = np.array([[1, -1], [-1, 1]])\n", "print(A)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Task**: Create the following matrix\n", "\n", "$$\n", "B = \\begin{pmatrix}\n", "5 & 6 & 7 & 8 \\\\\n", "1 & 2 & 3 & 4\n", "\\end{pmatrix}\n", "$$\n", "\n", "as an `array` through direct input using the function `np.array`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "aff71cdd3388ed4cff86a6c057248a55", "grade": false, "grade_id": "cell-148bd14a392de2b3", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Task**: Create the matrix \n", "\n", "$$\n", "C = \\begin{pmatrix}\n", "1 & 2 & 3 & 4 \\\\\n", "5 & 6 & 7 & 8\n", "\\end{pmatrix}\n", "$$\n", "\n", "using the commands `np.arange` and `np.reshape`. Again, you can always use the question mark `? np.arange` to get immediate help." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "672e55e42647dbff8deb19b1a6c9a623", "grade": false, "grade_id": "cell-e705355bab2be952", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Data science often requires not only matrix products but also element-wise products, also called Hadamard product. Since we are typically using arrays instead of matrices, the product sign `*` is used for the entrywise product.\n", "\n", "**Task**: Check this by computing `B * C`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "063566aef9f9c75ca1c1203fa326225a", "grade": false, "grade_id": "cell-941447700b956618", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Fortunately, the matrix product is also important. In order to compute the matrix product $A \\cdot B$ you can either type `A.dot(B)` or use the `@` operator.\n", "\n", "**Task**: Compute the matrix product $A \\cdot B$ by executing `A @ B`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "5d19a68f487db55c2d361afb872581b7", "grade": false, "grade_id": "cell-e6094007b2b0ca46", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The transpose of an array can be computed by the method `.T`.\n", "\n", "\n", "**Task**: Use this to compute $D = B \\cdot C^\\top$." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "1e5aeacddb57f0538da273ed0dc9d966", "grade": false, "grade_id": "cell-122aed4e0d6c83d8", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What happens if you execute `D ** 2`?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "1d04a3c04b625e3ade14b575485e98de", "grade": false, "grade_id": "cell-c615c2d03a6ce63c", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The command\n", "\n", " np.random.randint(5, size=(2, 4))\n", " \n", "generates a `2 × 4` array of random integers between 0 and 4.\n", "Now, create a `40 × 4` array `X` of standard-normal distributed random variables using `np.random.randn`.\n", "Compute the columnwise mean and variance of `X` using the commands `np.mean` and `np.var` (or the corresponding methods).\n", "Since you generate random numbers, the result will be slightly different each time you run your code.\n", "For debugging and testing purposes you should always initially set the seed of the random number generator to a fixed value by typing\n", "\n", " np.random.seed(0)\n", " \n", "**Task**: Set the random number seed to 0 and repeat the generation of `X` as well as the computation of the *mean* and the *variance*. Compare your results with those of your colleagues." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "7ccaaae5ab9f0c9c99ba9b3f5298de9b", "grade": false, "grade_id": "cell-025766ec0d808ecf", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Part C: Introduction Plots" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This exercise introduces to you some important plot routines.\n", "The package `matplotlib` is one of the most popular modules in this context.\n", "It provides similar functionality as *MATLAB*, and is fairly user-friendly.\n", "You can import the core module `pyplot` by\n", " \n", " import matplotlib.pyplot as plt\n", " \n", "You can find more information and a nice tutorial under\n", "https://realpython.com/python-matplotlib-guide/\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Task**: Create a vector `x` (here as a numpy array)\n", "\n", " x = np.linspace(-5,5,101)\n", " \n", "that partitions the interval $[−5, 5]$ into 101 equidistant points. Compute $y = x^2$ as well as $z = x^3$.\n", "The commands\n", "\n", " plt.plot(x,y)\n", " plt.show()\n", " \n", "create a simple plot of the function $y = f(x) = x^2$.\n", "In contrast to MATLAB, python does not create a new figure each time you call `plt.plot`.\n", "Python tries to plot everthing into one figure until one finally calls plt.show().\n", "\n", "**Task**: Try to plot both functions $y(x)$ and $z(x)$\n", "in one figure.\n", "\n", "You can beautify the plot by setting axes labels as well as choose a suitable title. Test this by including the lines\n", "\n", " plt.xlabel('x')\n", " plt.ylabel('f(x)')\n", " plt.title('Example functions')\n", "\n", "into your code cell.\n", "It is important to do this before you finally call `plt.show()`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "92760ef66f59dfe40cc957f3d3f6918e", "grade": false, "grade_id": "cell-da73f07b588384aa", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we want to plot the bivariate function\n", "\n", "$$\n", "f(x,y) = e^{−(x^2 +y^2)}.\n", "$$\n", "\n", "**Task**: Execute the following code and try to figure out what it is doing." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from mpl_toolkits.mplot3d import Axes3D\n", "from matplotlib import cm\n", "\n", "# Generate a 1 dimensional array ...\n", "z = np.linspace(-3, 3, 201)\n", "\n", "# ... and use this to create a mesh grid\n", "x, y = np.meshgrid(z, z)\n", "\n", "# Now, we can compute the function values f(x,y)\n", "f = np.exp(-(np.power(x,2) + np.power(y,2)))\n", "\n", "# The following command opens a new figure\n", "fig = plt.figure()\n", "\n", "# The next command determines the 3d type\n", "ax = fig.gca(projection='3d')\n", "\n", "# Generate a surface plot of the function\n", "ax.plot_surface(x,y,f, cmap=cm.coolwarm)\n", "\n", "# And finally show the plot (this is actually not necessary in Jupyter)\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Task**: Create a contour plot of the function\n", "\n", "$$\n", "g(x,y) = \\sin(3 \\,x)\\, \\cos(2 \\, y)\n", "$$\n", "\n", "using the function `plt.contour` or `plt.contourf`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "6d2b126be7a5a58fd56d7113c6c80fe4", "grade": false, "grade_id": "cell-4dbdb8c867345b71", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE" ] } ], "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": 1 }