Loading content...
In machine learning classification tasks, evaluating model performance is essential to understanding how well our models generalize to unseen data. Among the various evaluation metrics available, accuracy stands as one of the most intuitive and widely-used measures for classification problems.
Accuracy represents the proportion of predictions that match the true labels. It answers a simple yet powerful question: "Out of all the predictions made, how many were correct?"
Given two arrays of equal length:
The accuracy is calculated using the formula:
$$\text{Accuracy} = \frac{\text{Number of Correct Predictions}}{\text{Total Number of Predictions}} = \frac{\sum_{i=1}^{n} \mathbb{1}(y_{actual_i} = y_{predicted_i})}{n}$$
Where ๐(condition) is the indicator function that returns 1 if the condition is true and 0 otherwise, and n is the total number of samples.
Understanding Accuracy Values:
Your Task: Write a Python function that computes the classification accuracy given two numpy arrays containing the actual and predicted labels. The function should:
y_actual = np.array([1, 0, 1, 1, 0, 1])
y_predicted = np.array([1, 0, 0, 1, 0, 1])0.8333Comparing element by element:
โข Position 0: actual=1, predicted=1 โ (correct) โข Position 1: actual=0, predicted=0 โ (correct) โข Position 2: actual=1, predicted=0 โ (incorrect) โข Position 3: actual=1, predicted=1 โ (correct) โข Position 4: actual=0, predicted=0 โ (correct) โข Position 5: actual=1, predicted=1 โ (correct)
Correct predictions: 5 out of 6 Accuracy = 5/6 โ 0.8333
y_actual = np.array([1, 0, 1, 0, 1])
y_predicted = np.array([1, 0, 1, 0, 1])1.0Every predicted label matches the actual label exactly. This represents a perfect classifier with 100% accuracy.
All 5 predictions are correct: 5/5 = 1.0
y_actual = np.array([1, 1, 1, 1])
y_predicted = np.array([0, 0, 0, 0])0.0The model predicted class 0 for every sample, but all actual labels are class 1. This represents the worst possible performance where no predictions are correct.
Correct predictions: 0 out of 4 Accuracy = 0/4 = 0.0
Constraints