Classification and Prediction
Classification predicts a categorical class label (safe or risky loan). Prediction (numeric prediction or regression) predicts a continuous value (the amount of a loan a customer can repay). Both build a model from training data and apply it to new data.
Issues Regarding Classification and Prediction
Data preparation issues
- Data cleaning: reduce noise and handle missing values.
- Relevance analysis: remove irrelevant or redundant attributes (feature selection).
- Data transformation: generalise and normalise the data.
Comparing methods (evaluation criteria)
- Accuracy: how well the model predicts new data.
- Speed: time to build the model and time to use it.
- Robustness: tolerance of noise and missing values.
- Scalability: ability to handle very large data.
- Interpretability: how easy the model is to understand.
Evaluating a Classifier
A confusion matrix counts true positives (TP), false negatives (FN), false positives (FP) and true negatives (TN). From it:
- Accuracy = (TP + TN) / total.
- Precision = TP / (TP + FP) and recall (sensitivity) = TP / (TP + FN).
- Specificity = TN / (TN + FP) and F1 = 2TP / (2TP + FP + FN).
Methods to estimate accuracy: holdout (for example 2/3 training and 1/3 test), random sub-sampling, k-fold cross-validation (the data is split into k parts, each used once as the test set) and bootstrap. A model that fits the training data too well but fails on new data has overfitting; one that is too simple has underfitting.
Definition: Overfitting. A model that fits the training data, including its noise, so closely that it performs poorly on new data.
Table 3.1: Confusion matrix terms
| Term | Meaning | Formula |
|---|---|---|
| Accuracy | Correct predictions | (TP + TN) / total |
| Precision | Correct among predicted positive | TP / (TP + FP) |
| Recall | Positives found | TP / (TP + FN) |
| Specificity | Negatives found | TN / (TN + FP) |
| F1 | Balance of precision and recall | 2TP / (2TP + FP + FN) |
Example 3.1: Measures from a confusion matrix
Problem: A classifier is tested on 100 records and gives TP = 40, FN = 10, FP = 5 and TN = 45. Find the accuracy, precision, recall, specificity and F1.
Solution:
- Accuracy = (40 + 45) / 100 = 0.85.
- Precision = 40 / (40 + 5) = 0.889.
- Recall = 40 / (40 + 10) = 0.80.
- Specificity = 45 / (45 + 5) = 0.90.
- F1 = 2 x 40 / (2 x 40 + 5 + 10) = 80 / 95 = 0.842.
Result: Accuracy 85 percent, precision 88.9 percent, recall 80 percent, specificity 90 percent and F1 0.842.
TP, FN, FP, TN = 40, 10, 5, 45
total = TP + FN + FP + TN
print("Accuracy :", (TP + TN) / total)
print("Precision :", round(TP / (TP + FP), 3))
print("Recall :", TP / (TP + FN))
print("Specificity:", TN / (TN + FP))
print("F1 :", round(2 * TP / (2 * TP + FP + FN), 3))
Output
Accuracy : 0.85
Precision : 0.889
Recall : 0.8
Specificity: 0.9
F1 : 0.842
Common mistakes
- Measuring accuracy on the training data only.
- Trusting accuracy alone on unbalanced classes; 95 percent accuracy is easy if 95 percent of cases are one class.
- Using the test set while tuning the model.
- Confusing precision (about predictions) with recall (about actual positives).
Key takeaways
- Classification predicts labels; prediction predicts numbers.
- Criteria: accuracy, speed, robustness, scalability, interpretability.
- Accuracy = (TP + TN) / total; F1 = 2TP / (2TP + FP + FN).
- Use holdout or k-fold cross-validation to estimate accuracy.
Practice questions
- What is the difference between classification and prediction?
- What is recall?
- What is k-fold cross-validation?
Answers to the practice questions
- Classification gives a category; prediction gives a continuous value.
- TP divided by (TP + FN), the share of actual positives found.
- Splitting data into k parts and using each once as the test set.
Sources
- Official BCA syllabus Data Warehousing and Mining, course code 25BCA406DS03, Unit III
