Menu

Model Evaluation Metrics

Model Evaluation Metrics

A model is only useful if you can measure how well it works. This topic covers the main metrics for regression and classification and shows you how to choose the one that matches your business goal.

Metrics for Regression

Regression metrics measure how far predictions are from the true numbers. Mean Absolute Error, or MAE, is the average size of the errors. It is easy to understand because it uses the same units as your target.

Mean Squared Error, or MSE, squares the errors before averaging, so large mistakes are punished more. Root Mean Squared Error, or RMSE, takes the square root to return to the original units.

from sklearn.metrics import mean_absolute_error, r2_score
print(mean_absolute_error(y_test, predictions))
print(r2_score(y_test, predictions))

R-squared shows how much of the variation in the target the model explains. A value near 1 is strong, while a value near 0 means the model adds little. Use several metrics together for a fuller view.

Metrics for Classification

Accuracy is the share of correct predictions. It is simple, but it can be misleading when classes are unbalanced. If 95 percent of customers stay, a model that always predicts "stay" gets 95 percent accuracy but is useless.

Because of this, you need additional metrics. Precision, recall, and F1-score give a clearer view of how the model handles each class.

from sklearn.metrics import accuracy_score, classification_report
print(accuracy_score(y_test, predictions))
print(classification_report(y_test, predictions))

Choose the metric that fits the problem. In fraud detection, catching real fraud matters more than overall accuracy. In spam filtering, wrongly blocking good emails may be the bigger concern.

Confusion Matrix, Precision, and Recall

A confusion matrix is a table that shows the true and predicted classes. It has four parts: true positives, true negatives, false positives, and false negatives. It shows exactly where the model makes mistakes.

Precision answers, "Of all the cases the model called positive, how many were truly positive?" Recall answers, "Of all the truly positive cases, how many did the model find?"

from sklearn.metrics import confusion_matrix
print(confusion_matrix(y_test, predictions))

There is often a trade-off between the two. Raising precision may lower recall and the other way around. The F1-score combines both into one number. Decide which error costs more in your situation.

ROC Curve and AUC

The ROC curve shows how a classifier performs at different decision thresholds. It plots the true positive rate against the false positive rate. A curve that bends toward the top left corner means a better model.

AUC stands for Area Under the Curve. It summarizes the ROC curve into a single number between 0 and 1. An AUC of 0.5 means random guessing, and 1.0 means perfect separation.

from sklearn.metrics import roc_auc_score
probs = model.predict_proba(X_test)[:, 1]
print(roc_auc_score(y_test, probs))

AUC is helpful because it does not depend on one fixed threshold. It is a good way to compare models, especially when classes are unbalanced.


Data Science Lifecycle Tutorial

WR

Wesline Rinisha



Learn via Video Course

undefined Logo

NaN hrs



Get in Touch For Details