{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Jupyter notebooks\n", "Important shortcuts:\n", "\n", "- run and move to next cell: shift + return\n", "- run and stay on same cell: alt + return\n", "- insert cell below: ctrl + m, then b (or then a for \"above\")\n", "\n", "Two modes:\n", "- insert mode\n", "- edit mode" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Getting Help\n", "- Shortcuts: press h in edit mode\n", "- press tab inside method calls (press tab again to see more):\n", "![tab help](images/tab-help.png)\n", "- use \"?\" and run cell" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "np.bincount()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "np.bincount?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise:\n", "Use the help to find out what the options to ``np.unique`` are.\n", "Use np.unique to convert the array ``['one', 'two', 'three', 'one', 'two', 'three']`` into the array ``[0, 2, 1, 0, 2, 1]``." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "ar = ['one', 'two', 'three', 'one', 'two', 'three']\n", "# your solution here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plotting with matplotlib\n", "Need to use either \n", "```\n", "% matplotlib inline\n", "```\n", "or\n", "```\n", "% matplotlib notebook\n", "```\n", "Only one in each notebook!\n", "using ``inline`` will just sent ``png`` images to browser, using ``notebook`` will provide\n", "interactivity and allow updating old figures.\n", "With ``notebook`` you need to make sure to create a new figure before plotting, otherwise the last one will be updated!" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%matplotlib notebook\n", "import matplotlib.pyplot as plt\n", "\n", "X = np.random.normal(size=(12, 2))\n", "plt.scatter(X[:, 0], X[:, 1])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "plt.plot(X[:, 0])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# create a new figure\n", "plt.figure()\n", "plt.plot(X[:, 0])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise\n", "Create a new figure and plot a sin wave. You can use ``np.linspace`` to create equally spaced numbers in a given range." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Best practices for data analysis in Jupyter\n", "- use standard imports\n", "- don't ``import *``\n", "- be mindful of the state in the notebook!" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x = 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x = x + 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "print(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Avoid cells you can't run again:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "data = {'a': [1, 2, 3], 'b': [999, 1, 2]}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "column_a = data.pop('a')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "print(column_a)\n", "print(data)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Not mutating variables helps" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x = 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x2 = x + 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "print(x2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise\n", "Rewrite the code for the ``data`` dict above so that you don't mutate ``data``, but that the ``print`` stays the same." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# solution Here" ] } ], "metadata": { "kernelspec": { "display_name": "Python [default]", "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 }