{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Get me at http://tinyurl.com/sklcds\n", "========================================" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The IPython Notebook!\n", "========================\n", "Press shift + enter to run a cell.\n", "\n", "You can go back to previous cells, change them and re-run them." ] }, { "cell_type": "code", "collapsed": false, "input": [ "print(\"Hello World\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "X = 112" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print(X)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "range(10)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ " IPython notebook allows tab-completion and shows docstrings (by pressing tab [shift-tab in latest versions] after the opening parantheses), or using ?\n", " " ] }, { "cell_type": "code", "collapsed": false, "input": [ "range?" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Cells can be arbitrary long or short, and can define functions that will be available in other cells." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def fib(n):\n", " if n in [0, 1]:\n", " return n\n", " return fib(n - 1) + fib(n - 2)\n", "\n", "for x in range(5):\n", " print(fib(x))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Numpy\n", "======\n", "Numpy array are the most common numeric data type.\n", "\n", "As in other environments, it is very beneficial to vectorize your code (array-based computing) to make use of fast C and Fortran implementations." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np\n", "np.ones(10)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "np.ones((10, 10))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "np.arange(10)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Numpy allows *broadcasting* over rows, leading to practical short-hand notations." ] }, { "cell_type": "code", "collapsed": false, "input": [ "X = np.ones((10, 10)) + np.array([3, 5, 1, 10, 6, 12, 98, 1, 0, 3])\n", "print(X)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Most libraries in Python use object oriented interfaces." ] }, { "cell_type": "code", "collapsed": false, "input": [ "X.mean(axis=0)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Numpy has all standard array functions, linear algebra, and *fancy indexing*." ] }, { "cell_type": "code", "collapsed": false, "input": [ "X[:3, 1:4]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "X[:, ::2]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "X = np.random.randint(10, size=(32, 103))\n", "X" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "X[np.random.randint(32, size=10)]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "X[np.random.randint(32, size=10)].shape" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Matplotlib\n", "=============\n", "For all of your plotting needs!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Enable in-line plotting (can be done in config file)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "import numpy as np" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "plt.plot(np.random.uniform(size=10))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "plt.bar(np.arange(10), np.random.uniform(size=10))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "plt.hist(np.random.normal(size=1000))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "x, y = np.random.uniform(size=(2, 10))\n", "plt.scatter(x, y, marker=\"x\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print(np.eye(5))\n", "plt.matshow(np.eye(5))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tasks\n", "======\n", "\n", "1. Plot the function ``f(x) = x ** 2`` using lines. How can you get a smooth plot?\n", "2. Visualize a two-dimensional gaussian distribution from samples. How does your approach look with very many samples?" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }