Get startedGet started for free

Plotting the predictor insight graph

1. Plotting the predictor insight graph

With the predictor insight graph table available, it only takes some plotting skills to show the final predictor insight graph that you can use to interpret your model.

2. The predictor insight graph

Recall that the predictor insight graph shows the link between a predictor and the target. For instance, the predictor insight graph of the predictor gender shows that females are more likely to donate. Additionally, this graph also shows the size of the different groups. In the gender case, there are more males in the donor base. You will learn to construct this graph in two steps: First you will plot the target incidence line, and secondly, you will learn how to plot the bars that show the group sizes as well. The values needed to construct the predictor insight graph are given in the so called predictor insight graph table that you learned to construct in the previous video.

3. Plotting the target incidence

To make the plot, you need to import the matplotlib pyplot module. Assume that the predictor insight graph table is loaded in the variable pig_table. You can simply plot the target incidence line using the plot command on the incidence column. As you can see, this graph needs some editing in order to be interpretable. First, we make sure that the graph shows the names of the groups on the horizontal axis. Therefore, we use the xticks function with a list having the indices of the groups and the group names as arguments. Secondly, we center the groups by adding a margin to the left and right side of the plot. Finally, we add a label "Incidence" to the vertical axis and a label 'Income' to the horizontal axis. We make sure that this label is nicely aligned by adjusting the appropriate arguments.

4. Plotting the sizes

In order to add the graphs with the sizes, we only need to change three lines code. First, we add the argument secondary_y to the incidence graph. This indicates that we want the target incidence values to be plotted on the right hand side axis. Finally, we plot the bars using the plot method with the appropriate arguments. The kind argument indicates that you want to plot bars instead of the default line. The width indicates the size of the bars. We change the color to light gray and remove the border around the bar so that the target incidence line is most visible in the plot.

5. Plotting the sizes

This results in the plot as shown on the right hand side. It clearly shows that donors with a higher income are more likely to donate.

6. Let's practice!

Now let's try some examples.