{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "*This problem sheet contains some extra material which is not graded! You don't have to turn this problem sheet in.*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction to Data Science\n", "## Lab 5: Magic commands" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Part A: Assessing runtime of code snippets" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this part we learn how to assess different ways to implement the same operations." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We generate two one-dimensional random arrays `x` and `y` using two different ways:\n", "- `x` should be generated by directly calling `np.random.rand()` with the correct shape\n", "- `y` should be generated by first creating a list with random variables using \n", "\n", " [np.random.rand() for _ in range(n)]\n", " \n", " and converting this list into an array by wrapping the function `np.array()` around\n", "\n", "Both arrays should contain `n = 1000000` elements." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "48362b272bc8689b4df8fe647f35159c", "grade": false, "grade_id": "cell-005110c5a9c4632b", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "import numpy as np\n", "n = 1000000\n", "\n", "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, we use the magic command `%timeit` to measure the **mean execution time** of the two different ways to generate `x` and `y`.\n", "\n", "Simply prefix the `%timeit` command to assess the runtime, e.g.\n", "\n", " %timeit np.random.rand(n)\n", "\n", "*Note*: Learn more about magic commands [here](https://ipython.readthedocs.io/en/stable/interactive/magics.html)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "2be2e6520af3d19cdf92ae89ef4b2ab0", "grade": false, "grade_id": "cell-80a7316c73157c25", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "print('Direct method:')\n", "\n", "# YOUR CODE HERE\n", "raise NotImplementedError()\n", "\n", "print('Indirect method:')\n", "\n", "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Task**: Compare the runtime of three different ways to add the arrays `x` and `y`:\n", "- `x + y`\n", "- `np.array([x[i] + y[i] for i in range(n)])`\n", "- `np.add(x,y)`\n", "\n", "What do you observe?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "42023f7f7ca4e2938c65e5e8fff6e4b2", "grade": false, "grade_id": "cell-e4dae46aae27e6ac", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "print('Direct way:')\n", "\n", "# YOUR CODE HERE\n", "raise NotImplementedError()\n", "print('Numpy function np.add:')\n", "\n", "# YOUR CODE HERE\n", "raise NotImplementedError()\n", "print('Loop over elements:')\n", "\n", "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Part B: Further useful magic commands" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Try to find out what the following magic commands will do:\n", "- `%who_ls`(Add description here!)\n", "- `%whos` (Add description here!)\n", "- `%time` (Add description here!)\n", "- `%system` in connection with system commands, e.g., `%system ls ..`\n", "\n", "*Note*: Magic commands follow their own syntax, e.g., you cannot call\n", "\n", " print(%who_ls)\n", " \n", "However, you can do the following\n", " \n", " a = %who_ls\n", " print(a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "828e4b4e8fb986728d827216f14a2d00", "grade": false, "grade_id": "cell-9acd4c7e2c40bf5b", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE\n", "raise NotImplementedError()" ] } ], "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.6.7" } }, "nbformat": 4, "nbformat_minor": 2 }