{ "metadata": { "name": "", "signature": "sha256:1c6f86b2d7f6f50df32c6c2f1d9c5a7aadb96ccb35f03832bea220ed3618c164" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Interact Exercises" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%matplotlib inline" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "from matplotlib import pyplot as plt\n", "import numpy as np" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.html.widgets import *\n", "from IPython.display import display" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "String sorting" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write a `sort_string` function that takes a string as its input and prints a new string consisting of the original one, sorted. Add a `reverse` keyword argument with a default of `False` to allow for the sorting to be done in reverse.\n", "\n", "Then, use `interact` to create a user interface for exploring your `sort_string` function." ] }, { "cell_type": "code", "collapsed": false, "input": [ "%load soln/string_sorting.py" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Plotting with parameters" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write a `plot_sin` function that plots $sin(ax+b)$ over the interval $[0,4\\pi]$.\n", "\n", "Then use `interact` to create a user interface for exploring your function:\n", "\n", "* `a` should be a floating point number over the interval $[0.0,5.0]$.\n", "* `b` should be a floating point number over the interval $[-5.0,5.0]$." ] }, { "cell_type": "code", "collapsed": false, "input": [ "%load soln/param_plot_1.py" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "In matplotlib, the line style and color can be set with a third argument to `plot`. Examples of this argument:\n", "\n", "* dashed red: `r--`\n", "* blue circles: `bo`\n", "* dotted black: `k.`\n", "\n", "Add a `style` argument to your `plot_sin` function that allows you to set the line style of the plot.\n", "\n", "Use `interact` to create a UI for `plot_sin` that has a drop down menu for selecting the line style between a **dotted red** line and a **dashed black line**. This time use `interact` as a decorator." ] }, { "cell_type": "code", "collapsed": false, "input": [ "%load soln/param_plot_2.py" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 8 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Simple data explorer" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this exercise, you will use interact to build a UI for exploring correlations between different features in the [Iris dataset](http://en.wikipedia.org/wiki/Iris_flower_data_set) in [sklearn]http://scikit-learn.org/stable/(http://scikit-learn.org/stable/). This data contains 4 different measurements (called features in this content) of 150 different iris flowers of three different species.\n", "\n", "Load the dataset:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from sklearn.datasets import load_iris\n", "iris_data = load_iris()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The actual data is stored as a NumPy array under the `data` attribute:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "iris_data.data.shape" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 11, "text": [ "(150, 4)" ] } ], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can see the meanings of the 4 columns of data by looking at the `feature_names` attribute:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "iris_data.feature_names" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 12, "text": [ "['sepal length (cm)',\n", " 'sepal width (cm)',\n", " 'petal length (cm)',\n", " 'petal width (cm)']" ] } ], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write a `plot_iris` function that creates a scatter plot (using `plt.scatter`) of two columns of this dataset. Your function should have the following signature:\n", "\n", "```python\n", "def plot_iris(a, col1, col2):\n", " ...\n", "```\n", "\n", "where `a` is the NumPy array of data and `col1/col2` are the two columns to use for the scatter plot.\n", "\n", "Use `interact` to build a UI to explore the iris dataset using your `plot_iris` function. You will need to use the `fixed` function when passing the dataset to the function." ] }, { "cell_type": "code", "collapsed": false, "input": [ "%load soln/data_explorer.py" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }