Related Data Science Links
Learn Eval Metrics Data Science Tutorial, validate concepts with Eval Metrics Data Science MCQ Questions, and prepare interviews through Eval Metrics Data Science Interview Questions and Answers.
Evaluation
Model Performance
Metrics
scikit-learn
Model Evaluation Metrics
Learn the most important metrics to evaluate classification and regression models, with simple Python examples.
Classification Metrics
- Accuracy: fraction of correct predictions.
- Precision: of predicted positives, how many are correct.
- Recall: of actual positives, how many are found.
- F1-score: harmonic mean of precision and recall.
Confusion Matrix & Metrics
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import (
accuracy_score,
precision_score,
recall_score,
f1_score,
confusion_matrix,
classification_report
)
iris = load_iris()
X, y = iris.data, iris.target
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=42, stratify=y
)
clf = LogisticRegression(max_iter=1000)
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
print("Macro Precision:", precision_score(y_test, y_pred, average="macro"))
print("Macro Recall:", recall_score(y_test, y_pred, average="macro"))
print("Macro F1:", f1_score(y_test, y_pred, average="macro"))
print("\nConfusion matrix:\n", confusion_matrix(y_test, y_pred))
print("\nClassification report:\n", classification_report(y_test, y_pred, target_names=iris.target_names))
Regression Metrics
- MAE: Mean Absolute Error.
- MSE: Mean Squared Error.
- RMSE: Root Mean Squared Error.
- R²: coefficient of determination (1 is best).
Regression Metrics Example
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
import numpy as np
X = np.array([[500], [750], [1000], [1250], [1500], [1750], [2000]])
y = np.array([100, 150, 200, 250, 300, 320, 350])
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=42
)
reg = LinearRegression()
reg.fit(X_train, y_train)
y_pred = reg.predict(X_test)
mae = mean_absolute_error(y_test, y_pred)
mse = mean_squared_error(y_test, y_pred)
rmse = np.sqrt(mse)
r2 = r2_score(y_test, y_pred)
print("MAE:", mae)
print("MSE:", mse)
print("RMSE:", rmse)
print("R²:", r2)