From 85ed9896d15a012c3b572ea01886e46a36155690 Mon Sep 17 00:00:00 2001 From: Jennifer Rinker <rink@win.dtu.dk> Date: Mon, 25 Sep 2017 16:39:39 +0200 Subject: [PATCH] initial upload of demo notebook --- demo_notebook.ipynb | 433 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 433 insertions(+) create mode 100644 demo_notebook.ipynb diff --git a/demo_notebook.ipynb b/demo_notebook.ipynb new file mode 100644 index 0000000..6db1613 --- /dev/null +++ b/demo_notebook.ipynb @@ -0,0 +1,433 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Jupyter notebooks are extremely cool ways to demo your code or work interactively. They are separated into cells, just like Mathematica. You enter insert mode by clicking \"enter\" when a cell is selected, and you exit by hitting the escape key. You evaluate a cell using shift-enter." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Cell types" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You can control the type of cell in Cell -> Cell Type, and choose whether your cell is\n", + "- text\n", + "- code \n", + "\n", + "Text cells can be formatted following Markdown conventions." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Random cool things" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "x = 'Hello, world!'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Note that the output of a cell in the last line is automatically printed if it's a variable or unassigned output:" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "8" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x = 4\n", + "2 * x" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "There are a bunch of \"magic\" commands that can really up your game." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "application/json": { + "cell": { + "!": "OSMagics", + "HTML": "Other", + "SVG": "Other", + "bash": "Other", + "capture": "ExecutionMagics", + "cmd": "Other", + "debug": "ExecutionMagics", + "file": "Other", + "html": "DisplayMagics", + "javascript": "DisplayMagics", + "js": "DisplayMagics", + "latex": "DisplayMagics", + "markdown": "DisplayMagics", + "perl": "Other", + "prun": "ExecutionMagics", + "pypy": "Other", + "python": "Other", + "python2": "Other", + "python3": "Other", + "ruby": "Other", + "script": "ScriptMagics", + "sh": "Other", + "svg": "DisplayMagics", + "sx": "OSMagics", + "system": "OSMagics", + "time": "ExecutionMagics", + "timeit": "ExecutionMagics", + "writefile": "OSMagics" + }, + "line": { + "alias": "OSMagics", + "alias_magic": "BasicMagics", + "autocall": "AutoMagics", + "automagic": "AutoMagics", + "autosave": "KernelMagics", + "bookmark": "OSMagics", + "cd": "OSMagics", + "clear": "KernelMagics", + "cls": "KernelMagics", + "colors": "BasicMagics", + "config": "ConfigMagics", + "connect_info": "KernelMagics", + "copy": "Other", + "ddir": "Other", + "debug": "ExecutionMagics", + "dhist": "OSMagics", + "dirs": "OSMagics", + "doctest_mode": "BasicMagics", + "echo": "Other", + "ed": "Other", + "edit": "KernelMagics", + "env": "OSMagics", + "gui": "BasicMagics", + "hist": "Other", + "history": "HistoryMagics", + "killbgscripts": "ScriptMagics", + "ldir": "Other", + "less": "KernelMagics", + "load": "CodeMagics", + "load_ext": "ExtensionMagics", + "loadpy": "CodeMagics", + "logoff": "LoggingMagics", + "logon": "LoggingMagics", + "logstart": "LoggingMagics", + "logstate": "LoggingMagics", + "logstop": "LoggingMagics", + "ls": "Other", + "lsmagic": "BasicMagics", + "macro": "ExecutionMagics", + "magic": "BasicMagics", + "matplotlib": "PylabMagics", + "mkdir": "Other", + "more": "KernelMagics", + "notebook": "BasicMagics", + "page": "BasicMagics", + "pastebin": "CodeMagics", + "pdb": "ExecutionMagics", + "pdef": "NamespaceMagics", + "pdoc": "NamespaceMagics", + "pfile": "NamespaceMagics", + "pinfo": "NamespaceMagics", + "pinfo2": "NamespaceMagics", + "pip": "BasicMagics", + "popd": "OSMagics", + "pprint": "BasicMagics", + "precision": "BasicMagics", + "profile": "BasicMagics", + "prun": "ExecutionMagics", + "psearch": "NamespaceMagics", + "psource": "NamespaceMagics", + "pushd": "OSMagics", + "pwd": "OSMagics", + "pycat": "OSMagics", + "pylab": "PylabMagics", + "qtconsole": "KernelMagics", + "quickref": "BasicMagics", + "recall": "HistoryMagics", + "rehashx": "OSMagics", + "reload_ext": "ExtensionMagics", + "ren": "Other", + "rep": "Other", + "rerun": "HistoryMagics", + "reset": "NamespaceMagics", + "reset_selective": "NamespaceMagics", + "rmdir": "Other", + "run": "ExecutionMagics", + "save": "CodeMagics", + "sc": "OSMagics", + "set_env": "OSMagics", + "store": "StoreMagics", + "sx": "OSMagics", + "system": "OSMagics", + "tb": "ExecutionMagics", + "time": "ExecutionMagics", + "timeit": "ExecutionMagics", + "unalias": "OSMagics", + "unload_ext": "ExtensionMagics", + "who": "NamespaceMagics", + "who_ls": "NamespaceMagics", + "whos": "NamespaceMagics", + "xdel": "NamespaceMagics", + "xmode": "BasicMagics" + } + }, + "text/plain": [ + "Available line magics:\n", + "%alias %alias_magic %autocall %automagic %autosave %bookmark %cd %clear %cls %colors %config %connect_info %copy %ddir %debug %dhist %dirs %doctest_mode %echo %ed %edit %env %gui %hist %history %killbgscripts %ldir %less %load %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %ls %lsmagic %macro %magic %matplotlib %mkdir %more %notebook %page %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %popd %pprint %precision %profile %prun %psearch %psource %pushd %pwd %pycat %pylab %qtconsole %quickref %recall %rehashx %reload_ext %ren %rep %rerun %reset %reset_selective %rmdir %run %save %sc %set_env %store %sx %system %tb %time %timeit %unalias %unload_ext %who %who_ls %whos %xdel %xmode\n", + "\n", + "Available cell magics:\n", + "%%! %%HTML %%SVG %%bash %%capture %%cmd %%debug %%file %%html %%javascript %%js %%latex %%markdown %%perl %%prun %%pypy %%python %%python2 %%python3 %%ruby %%script %%sh %%svg %%sx %%system %%time %%timeit %%writefile\n", + "\n", + "Automagic is ON, % prefix IS NOT needed for line magics." + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "%lsmagic" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "For example, let's test how long a silly function takes..." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "import numpy as np\n", + "def silly_function():\n", + " np.random.randint(0, high=100, size=int(1e6))" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.7 ms ± 19.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n" + ] + } + ], + "source": [ + "%%timeit\n", + "silly_function()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Exercise" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Define $x$ and $y$ below such that $x$ is a numpy array from 1 to 9 (inclusive) and $y=1/x$. Then, run your cell and the next cell and look at the resulting plot. Does it make sense? Run your mouse over the plot. Notice anything cool?\n", + "\n", + "__HINT__: Google \"numpy arange function\" to figure out how to define $x$." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "import numpy as np\n", + "# x = ???\n", + "# y = ???" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "import plotly\n", + "from plotly.graph_objs import Scatter, Layout\n", + "\n", + "plotly.offline.init_notebook_mode(connected=True)\n", + "\n", + "plotly.offline.iplot({\n", + " \"data\": [Scatter(x=x, y=y)],\n", + " \"layout\": Layout(title=\"Hello, world!\")\n", + "})" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Solution" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "import numpy as np\n", + "x = np.arange(1, 10)\n", + "y = 1/x" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "<script>requirejs.config({paths: { 'plotly': ['https://cdn.plot.ly/plotly-latest.min']},});if(!window.Plotly) {{require(['plotly'],function(plotly) {window.Plotly=plotly;});}}</script>" + ], + "text/vnd.plotly.v1+html": [ + "<script>requirejs.config({paths: { 'plotly': ['https://cdn.plot.ly/plotly-latest.min']},});if(!window.Plotly) {{require(['plotly'],function(plotly) {window.Plotly=plotly;});}}</script>" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/vnd.plotly.v1+json": { + "data": [ + { + "type": "scatter", + "x": [ + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9 + ], + "y": [ + 1, + 0.5, + 0.3333333333333333, + 0.25, + 0.2, + 0.16666666666666666, + 0.14285714285714285, + 0.125, + 0.1111111111111111 + ] + } + ], + "layout": { + "title": "Hello, world!" + } + }, + "text/html": [ + "<div id=\"c43ec9be-ef5d-4689-b6ab-37f4d26f7e7a\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"c43ec9be-ef5d-4689-b6ab-37f4d26f7e7a\", [{\"type\": \"scatter\", \"x\": [1, 2, 3, 4, 5, 6, 7, 8, 9], \"y\": [1.0, 0.5, 0.3333333333333333, 0.25, 0.2, 0.16666666666666666, 0.14285714285714285, 0.125, 0.1111111111111111]}], {\"title\": \"Hello, world!\"}, {\"showLink\": true, \"linkText\": \"Export to plot.ly\"})});</script>" + ], + "text/vnd.plotly.v1+html": [ + "<div id=\"c43ec9be-ef5d-4689-b6ab-37f4d26f7e7a\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"c43ec9be-ef5d-4689-b6ab-37f4d26f7e7a\", [{\"type\": \"scatter\", \"x\": [1, 2, 3, 4, 5, 6, 7, 8, 9], \"y\": [1.0, 0.5, 0.3333333333333333, 0.25, 0.2, 0.16666666666666666, 0.14285714285714285, 0.125, 0.1111111111111111]}], {\"title\": \"Hello, world!\"}, {\"showLink\": true, \"linkText\": \"Export to plot.ly\"})});</script>" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "import plotly\n", + "from plotly.graph_objs import Scatter, Layout\n", + "\n", + "plotly.offline.init_notebook_mode(connected=True)\n", + "\n", + "plotly.offline.iplot({\n", + " \"data\": [Scatter(x=x, y=y)],\n", + " \"layout\": Layout(title=\"Hello, world!\")\n", + "})" + ] + } + ], + "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.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} -- GitLab