How it works
Accuracy is calculated as: (correct predictions) / (total predictions). If a model gets 90 out of 100 predictions right, accuracy is 90%.
The formula looks simple, but it has a trap: on imbalanced datasets, accuracy can be high even with a bad model. Imagine a fraud detector where 99% of transactions are legitimate. A model that always says "legitimate" has 99% accuracy, but it does not detect any fraud.
That is why accuracy is used together with other metrics: precision, recall, F1-score and confusion matrix. Each one reveals a different aspect of performance.
Practical example
A spam or not-spam email classification model is tested on 1000 emails (500 spam, 500 not spam). It gets 450 spam and 480 not spam correct, for a total of 930 correct. Accuracy = 93%.
But if the dataset had 950 not spam and 50 spam, and the model got all not spam correct and no spam correct, accuracy would be 95%, even though the model is useless for detecting spam. That is the problem of imbalance.
What it is used for
It serves as an initial and intuitive performance metric. It is useful when classes are balanced and the cost of error is similar for all classes. In imbalanced cases, it should be complemented by other metrics.
Difference between accuracy and precision
Accuracy measures total correct (correct / total). Precision measures, among positive predictions, how many are truly positive (true positives / (true positives + false positives)). Accuracy looks at the whole; precision focuses on the correct predictions for the positive class.