Building a Machine Learning Model on Cancer Data
By: Rukshan Pramoditha
Tags: Machine Learning, Logistic Regression, Cancer, Data Science, Python

Analysing Data and Building a Machine Learning Model

1. Training Using One Feature

Plotting a Scatter Plot

Let me now use logistic regression to try to predict if a tumour is cancerous. To get started, let me use only the first feature of the dataset: mean radius. The following code plots a scatter plot showing if a tumour is malignant or benign based on the mean radius.

The output is:

As you can see, this is a good opportunity to use logistic regression to predict if a tumour is cancerous.

Finding the Intercept and Coefficient

Scikit-learn comes with the LogisticRegression class that allows you to apply logistic regression to train a model. Thus, in this example, I’m going to train a model using the first feature (mean radius) of the data set.

Intercept (alpha) = 8.19393897

Coefficient of the first feature or predictor x (beta) = -0.54291739

P is the probability of the outcome occurring.

e is the base of the natural logarithm.

x is the value of the predictor.

Once the model is trained, what we are most interested in at this point is the intercept and coefficient. Knowing these two values allows us to plot the sigmoid curve that tries to fit the points on the chart.

Plotting the Sigmoid Curve

Making Predictions

Using the trained model, let me try to make some predictions. Let me try to predict the result if the mean radius is 20.

Interpretation: As you can see from the output, the predict _ proba() function in the first statement returns a two-dimensional array. The result of 0.93489354 indicates the probability that the prediction is 0 (malignant) while the result of 0.06510646 indicates the probability that the prediction is 1. Based on the default threshold of 0.5, the prediction is that the tumour is malignant (value of 0), since its predicted probability (0.93489354) of 0 (malignant) is more than 0.5. The predict() function in the second statement returns the class that the result lies in (which in this case can be a 0 or 1). The result of 0 indicates that the prediction is that the tumour is malignant. Let me try another example with a mean radius of 8 this time.

As you can see from the result, the prediction is that the tumour is benign which is not harmful and non-cancerous.

2. Training the Model Using All Features

Splitting the Data Set into Training and Test Sets

Previously, I specifically trained the model using one feature: mean radius. Let me now try to train the model using all of the features and then see how well it can accurately perform the prediction.

Instead of training the model using all of the rows in the data set, I’m going to split it into two sets, one for training and one for testing. To do so, I use the train _ test _ split() function. This function allows me to split my data into random train and test subsets. The following code splits the data set into a 75 per cent training and 25 per cent testing set.

Training the Model

Once the data set is split, it is now time to train the model. The following code trains the model using logistic regression.

Finding the Intercept and Coefficients

In this example, I am training it with all of the 30 features in the data set. When the training is done, let me print out the intercept and model coefficients.

Because I have trained the model using 30 features, there are 30 coefficients.

Making Predictions

The output is:

3. Determining the Effectiveness of the Machine Learning Algorithm

Getting the Confusion Matrix

While it is useful to print out the predictions together with the original diagnosis from the test set, it does not give you a clear picture of how good the model is in predicting if a tumour is cancerous. A more scientific way would be to use the confusion matrix. The confusion matrix shows the number of actual and predicted labels and how many of them are classified correctly. We can use Pandas’s crosstab() function to print out the confusion matrix.

The columns represent the actual diagnosis (0 for malignant and 1 for benign). The rows represent the prediction. Each individual box represents one of the following.

True Positive (TP): The model correctly predicts the outcome as positive. In this example, the number of TP (87) indicates the number of correct predictions that a tumour is benign.

True Negative (TN): The model correctly predicts the outcome as negative. In this example, tumours were correctly predicted to be malignant.

False Positive (FP): The model incorrectly predicted the outcome as positive, but the actual result is negative. In this example, it means that the tumour is actually malignant, but the model predicted the tumour to be benign.

False Negative (FN): The model incorrectly predicted the outcome as negative, but the actual result is positive. In this example, it means that the tumour is actually benign, but the model predicted the tumour to be malignant.

This set of numbers is known as the confusion matrix.

Computing Accuracy, Recall, Precision and Other Metrics

Based on the confusion matrix, we can calculate the following metrics.

Accuracy

Accuracy: This is defined as the sum of all correct predictions divided by the total number of predictions, or mathematically:

This metric is easy to understand. After all, if the model correctly predicts 99 out of 100 samples, the accuracy is 0.99. Accuracy works best with evenly distributed data points, but it works really badly for a skewed data set.

To get the accuracy of our model, we can use the score() function of the model.

Interpretation: Our model correctly predicts 94.4 out of 100.

Precision

Precision: This metric is concerned with the number of correct positive predictions. You can think of precision as “of those predicted to be positive, how many were actually predicted correctly?”

Recall (also known as True Positive Rate)

Recall: This metric is concerned with the number of correctly predicted positive events. You can think of recall as “of those positive events, how many were predicted correctly?”

To get the precision and recall of our model, we use the classification _ report() function of the metrics module.

The output is:

Interpretation: If a malignant tumour is represented as negative and a benign tumour is represented as positive, then:

Case 1: If the precision or recall is high, it means that more patients with benign tumours are diagnosed correctly, which indicates that the algorithm is good.

Case 2: If the precision is low, it means that more patients with malignant tumours are diagnosed as benign.

Case 3: If the recall is low, it means that more patients with benign tumours are diagnosed as malignant.

For cases like diagnosing cancer, it’s important to consider both the precision and recall metrics when evaluating the effectiveness of an ML algorithm.

Plotting the ROC and Finding the Area Under the Curve (AUC)

One way to examine the effectiveness of an algorithm would be to plot a curve known as the Receiver Operating Characteristic (ROC) Curve. The ROC curve is created by plotting the TPR (True Positive Rate) against the FPR (False Positive Rate) at various threshold settings. To plot the ROC, we can use matplotlib to plot a line chart using the values stored in the fpr and tpr variables. We can use the auc() function to find the area under the ROC.

The output is:

AUC = 0.99

Interpretation: The area under an ROC curve is a measure of the usefulness of a test in general, where a greater area means a more useful test and the areas under ROC curves are used to compare the usefulness of tests. Generally, aim for the algorithm with the highest AUC.

This project was done by Rukshan Pramoditha, the Author of Data Science 365 Blog.

Dependencies

You may refer to the following resources to learn the theory and concepts used in this project.

Technologies used in this project