{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Interact Exercises" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from matplotlib import pyplot as plt\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from ipywidgets import *\n", "from IPython.display import display" ] }, { "cell_type": "markdown", "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", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%load soln/string_sorting.py" ] }, { "cell_type": "markdown", "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", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%load soln/param_plot_1.py" ] }, { "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", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%load soln/param_plot_2.py" ] }, { "cell_type": "markdown", "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", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from sklearn.datasets import load_iris\n", "iris_data = load_iris()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The actual data is stored as a NumPy array under the `data` attribute:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "iris_data.data.shape" ] }, { "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", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "iris_data.feature_names" ] }, { "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", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%load soln/data_explorer.py" ] } ], "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.4.3" } }, "nbformat": 4, "nbformat_minor": 0 }