In this exercise we will build some models with three algorithms (k-nearest neighbors, logistic regression and decision tree) and compare them with the lift curve and a cumulative accuracy profile (CAP). Lift curves and CAP measure how much a model is good at discriminating events (e.g. "no" vs. "yes", "it happens" vs. "it doesn't happen"), based on the probabilities predicted by the model. So, in this case, the prediction is not the class each data point belongs to (e.g. "no" and "yes", "0" and "1"), but the probabilities of each data point belonging to each class.

We will use the train and test datasets we prepared earlier.
First, let's import the relevant modules and data:

Let's remember the names of the features:

Now lwe will prepare a pipeline to train and test the three models. First, we will define a dictionary which will contain the names of the three algorithms and the corresponding python commands. Then, we will define a function to compute the lift curve and the CAP. Subsequently, we will define another function to visualize the lift curve and the CAP.

Now let's run the pipeline:

Before calculating the lift, the population of the test dataset is ordered in descending order from the highest predicted probability to the lowest.
We are interested in the values of the first quantiles (the y axis of the charts, labelled "bins"). The quantile 1 represents the 10% of the population of the test dataset with the highest predicted probabilities. In this quantile, with the decision tree we have obtained a lift equal to 3.91 (that is to say "the model is 3.91 times better than the random model") and value of captured target equal to 0.39 (which means that the 39% of the real events have been cumulated in this quantile). These values are higher than the corresponding values obtained with the K-NN algorithm and the logistic regression. Moreover, if we compare the profiles of the two charts among the three methods, we can see that the decision tree tends to have higher values in the first quantiles. All this suggests that the decision tree is the candidate classifier in this case.