Interactive

Interactive autogenerates User Interface (UI) controls for function elements and then calls the function with those elements when you manipulate the controls interactively. To see how this works, we are going to explore the common Iris dataset, which is preloaded into scikit-learn:

  1. First, import the necessary packages, as shown in the following screenshot:

In the preceding screenshot, we can see the following:

    • interactive, widgets, and fixed from ipywidgets to create our interactive data exploration tool
    • matplotlib for data visualization
    • datasets from sklearn to load in the Iris dataset

  1. The Iris dataset contains measurements for three species of iris plants (Iris Setosa, Iris Versicolour, and Iris Virginica), along with four different features (sepal length, sepal width, petal length, and petal width). As shown in the following screenshot, we will define a Python function to create a scatter plot with one feature on the x axis and another on the axis:

In the preceding screenshot, we can see the following:

    • The plot_iris_data function takes in the preloaded iris dataset and we can see the integer indices of the two features we want to compare, feature_x and feature_y.
    • There's also the extent of the values to be plotted, x_limits and y_limits, and colorby Boolean, which is True if we would like to color the data points by their species. 

If we were using this function to plot the data in a regular notebook cell, we would need to rerun the cell with a different selection. By developing the widgets in the previous section, we could explore the data interactively without rerunning the cell.

  1. Next, as shown in the following screenshot, we will call interactive with the desired function as the first argument. The subsequent arguments correspond to the arguments for the plot_iris_data function. To pass an argument that is not interactive, wrap it in the fixed() function that's been imported from ipywidgets. For the interactive arguments, we pass the name of the widget we defined in the previous section (feature_y and y_limits were not defined but follow on from feature_x and x_limits, respectively):

  1. After plotting our initial data, we will see that some of the points seem to extend out of the region being plotted. As shown in the following screenshot, we will use the FloatRangeSlider widgets we incorporated to adjust the plot's limits and center the data in the field of view:

  1. Now that the data has been centered, we want to explore the relationship between the different species of iris. By selecting the ToggleButton widget we incorporated, points are colored by their species and we can begin to see how the relationship between petal length and sepal width varies for each iris, as shown in the following screenshot:

  1. Finally, we can use the Dropdown widgets to explore the relationship between other features in the dataset, as shown in the following screenshot:  

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset