Model Evaluation with Scikit‑Learn

Evaluating a model is crucial to understand its performance and to make improvements. Scikit‑learn offers a rich set of tools for assessing both regression and classification models.

1. Regression Metrics

from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score

y_pred = model.predict(X_test)
print("MAE:", mean_absolute_error(y_test, y_pred))
print("MSE:", mean_squared_error(y_test, y_pred))
print("R² :", r2_score(y_test, y_pred))

2. Classification Metrics

from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix, roc_auc_score

y_pred = clf.predict(X_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
print("Precision:", precision_score(y_test, y_pred))
print("Recall:", recall_score(y_test, y_pred))
print("F1:", f1_score(y_test, y_pred))
print("ROC AUC:", roc_auc_score(y_test, clf.predict_proba(X_test)[:,1]))
print(confusion_matrix(y_test, y_pred))

3. Cross‑Validation

Use cross_val_score to get a more reliable estimate of model performance.

from sklearn.model_selection import cross_val_score

scores = cross_val_score(model, X, y, cv=5, scoring='neg_mean_squared_error')
print("CV MSE:", -scores.mean())

4. Interactive Confusion Matrix

5. ROC Curve