Machine learning > Fundamentals of Machine Learning > Performance Metrics > Accuracy
Understanding Accuracy in Machine Learning
Accuracy is a fundamental performance metric in machine learning, particularly for classification tasks. It represents the ratio of correctly predicted instances to the total number of instances. While simple to understand and calculate, accuracy can be misleading in imbalanced datasets. This tutorial will explore the concept of accuracy, its calculation, limitations, and alternatives.
Definition of Accuracy
Accuracy is defined as: Accuracy = (Number of Correct Predictions) / (Total Number of Predictions) It measures the overall correctness of a classification model. A higher accuracy generally indicates a better-performing model, but this is not always the case, especially in imbalanced datasets.
Calculating Accuracy: A Simple Example
This Python code demonstrates the calculation of accuracy given a list of predictions and the corresponding actual values. The calculate_accuracy
function iterates through the predictions, comparing each one to its actual value. It increments the correct_predictions
counter for each match and then divides this count by the total number of predictions to compute the accuracy. The example showcases a simple case where the calculated accuracy is printed to two decimal places.
def calculate_accuracy(predictions, actual_values):
correct_predictions = 0
total_predictions = len(predictions)
for i in range(total_predictions):
if predictions[i] == actual_values[i]:
correct_predictions += 1
accuracy = correct_predictions / total_predictions
return accuracy
# Example Usage
predictions = [1, 0, 1, 1, 0]
actual_values = [1, 0, 0, 1, 0]
accuracy = calculate_accuracy(predictions, actual_values)
print(f"Accuracy: {accuracy:.2f}")
Using scikit-learn for Accuracy
The scikit-learn library provides a convenient function, accuracy_score
, for calculating accuracy. This example demonstrates how to use this function, which takes the actual values and predictions as input and returns the accuracy. It's a more concise and efficient way to calculate accuracy compared to writing a custom function.
from sklearn.metrics import accuracy_score
# Example Usage
actual_values = [1, 0, 0, 1, 0]
predictions = [1, 0, 1, 1, 0]
accuracy = accuracy_score(actual_values, predictions)
print(f"Accuracy: {accuracy:.2f}")
Limitations of Accuracy: Imbalanced Datasets
Accuracy can be a misleading metric when dealing with imbalanced datasets. In an imbalanced dataset, one class has significantly more instances than the other(s). For example, in a fraud detection dataset, the number of non-fraudulent transactions will be much larger than the number of fraudulent transactions. Consider a dataset where 95% of the instances belong to class A and 5% belong to class B. A classifier that always predicts class A would achieve an accuracy of 95%, which might seem good. However, it would fail to identify any instances of class B, making it useless for applications where identifying class B is crucial.
Alternatives to Accuracy
When dealing with imbalanced datasets, consider using alternative performance metrics that provide a more balanced view of the model's performance. Some alternatives include:
Real-Life Use Case Section
Scenario: Medical Diagnosis (Rare Disease Detection) Imagine a machine learning model designed to detect a rare disease. The dataset is highly imbalanced, with 99.9% of patients being healthy and only 0.1% having the disease. Problem with Accuracy: A model that always predicts 'healthy' would achieve 99.9% accuracy. This seems excellent, but the model is completely useless as it fails to identify any patients with the disease. Better Metrics: Metrics like recall and F1-score are much more informative in this case. Recall would measure the proportion of actual disease cases that the model correctly identifies. F1-score provides a balanced view, considering both the precision (how many of the 'disease' predictions are correct) and recall. In this context, maximizing recall is crucial to avoid missing patients who need treatment, even if it means accepting some false positives (incorrectly identifying healthy patients as having the disease).
Best Practices
When using accuracy as a performance metric:
Interview Tip
When discussing accuracy in a machine learning interview, demonstrate your understanding of its limitations, especially in the context of imbalanced datasets. Be prepared to discuss alternative metrics and explain why they might be more appropriate in certain scenarios. You should also be able to discuss strategies for addressing class imbalance, such as oversampling, undersampling, or using cost-sensitive learning algorithms.
When to Use Accuracy
Accuracy is most appropriate when:
Pros of Accuracy
Cons of Accuracy
FAQ
-
What is the difference between accuracy and precision?
Accuracy measures the overall correctness of the model (correct predictions / total predictions), while precision measures how many of the positive predictions were actually correct (true positives / predicted positives). Precision focuses on the quality of the positive predictions, while accuracy provides an overall view of the model's performance.
-
Why is accuracy not a good metric for imbalanced datasets?
In imbalanced datasets, a model can achieve high accuracy by simply predicting the majority class most of the time. This model would be ineffective at identifying the minority class, which is often the class of interest. Alternative metrics like precision, recall, and F1-score provide a more balanced view of the model's performance in such cases.
-
How can I improve accuracy in a machine learning model?
Improving accuracy depends on the specific problem and dataset. Some strategies include:
- Feature Engineering: Creating new features or transforming existing ones to provide more information to the model.
- Model Selection: Choosing a different model that is better suited to the data.
- Hyperparameter Tuning: Optimizing the hyperparameters of the model to improve its performance.
- Data Preprocessing: Cleaning and preparing the data to reduce noise and improve consistency.
- Addressing Class Imbalance: Using techniques like oversampling, undersampling, or cost-sensitive learning to handle imbalanced datasets.