#!/usr/bin/env python # coding: utf-8 # # Interact Exercises # In[ ]: get_ipython().run_line_magic('matplotlib', 'inline') # In[ ]: from matplotlib import pyplot as plt import numpy as np # In[ ]: from ipywidgets import * from IPython.display import display # ## String sorting # 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. # # Then, use `interact` to create a user interface for exploring your `sort_string` function. # In[ ]: get_ipython().run_line_magic('load', 'soln/string_sorting.py') # ## Plotting with parameters # Write a `plot_sin` function that plots $sin(ax+b)$ over the interval $[0,4\pi]$. # # Then use `interact` to create a user interface for exploring your function: # # * `a` should be a floating point number over the interval $[0.0,5.0]$. # * `b` should be a floating point number over the interval $[-5.0,5.0]$. # In[ ]: get_ipython().run_line_magic('load', 'soln/param_plot_1.py') # In matplotlib, the line style and color can be set with a third argument to `plot`. Examples of this argument: # # * dashed red: `r--` # * blue circles: `bo` # * dotted black: `k.` # # Add a `style` argument to your `plot_sin` function that allows you to set the line style of the plot. # # 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. # In[ ]: get_ipython().run_line_magic('load', 'soln/param_plot_2.py') # ## Simple data explorer # 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. # # Load the dataset: # In[ ]: from sklearn.datasets import load_iris iris_data = load_iris() # The actual data is stored as a NumPy array under the `data` attribute: # In[ ]: iris_data.data.shape # You can see the meanings of the 4 columns of data by looking at the `feature_names` attribute: # In[ ]: iris_data.feature_names # 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: # # ```python # def plot_iris(a, col1, col2): # ... # ``` # # where `a` is the NumPy array of data and `col1/col2` are the two columns to use for the scatter plot. # # 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. # In[ ]: get_ipython().run_line_magic('load', 'soln/data_explorer.py')