Loading

Quipoin Menu

Learn • Practice • Grow

python-for-ai / Evaluating a Model
tutorial

Evaluating a Model

After training a model, you need to know how well it performs. Scikit-learn provides metrics to evaluate regression and classification models.

Regression Metrics (for Linear Regression)

from sklearn.metrics import mean_squared_error, r2_score

mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)

print(f"Mean Squared Error: {mse:.2f}")
print(f"R² Score: {r2:.2f}")
  • Mean Squared Error (MSE): Average squared difference between predictions and true values. Lower is better.
  • R² Score: How much variance is explained by the model. Ranges from -∞ to 1 (1 is perfect).

Using the Model's Built‑in Score

Many Scikit-learn models have a .score() method. For regression, it returns R².
r2 = model.score(X_test, y_test)
print(f"R² Score: {r2:.2f}")

Example: Classification Metrics (Iris Dataset)

For classification, common metrics include accuracy, precision, recall.
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

iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=42)

clf = LogisticRegression(max_iter=200)
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy:.2f}")

Why Evaluation Matters

A model might perform well on training data but poorly on new data (overfitting). Evaluation on a separate test set gives an honest estimate of real‑world performance.


Two Minute Drill
  • Regression metrics: MSE (lower better), R² (closer to 1 better).
  • Classification metrics: accuracy, precision, recall.
  • Use model.score(X_test, y_test) for quick evaluation.
  • Never evaluate on training data – it gives overly optimistic results.

Need more clarification?

Drop us an email at career@quipoinfotech.com