Bird
Raised Fist0
ML Pythonml~8 mins

Regression evaluation (MSE, RMSE, R²) in ML Python - Model Metrics & Evaluation

Choose your learning style10 modes available
Metrics & Evaluation - Regression evaluation (MSE, RMSE, R²)
Which metric matters for regression and WHY

In regression, we want to see how close our predicted numbers are to the real numbers.

Mean Squared Error (MSE) measures the average of the squares of the errors. It tells us how big the mistakes are, giving more weight to big errors.

Root Mean Squared Error (RMSE) is the square root of MSE. It shows error in the same units as the target, making it easier to understand.

R-squared (R²) tells us how much of the variation in the real data our model explains. It ranges from 0 to 1, where 1 means perfect prediction.

We use these metrics because they help us understand both the size of errors and how well the model fits the data.

Confusion matrix equivalent for regression

Regression does not use a confusion matrix because it predicts continuous values, not categories.

Instead, we look at error values like MSE and RMSE, and the R² score.

    Example:
    True values:      [3, 5, 2, 7]
    Predicted values: [2.5, 5.1, 2.2, 6.8]

    Errors:          [-0.5, 0.1, 0.2, -0.2]
    Squared errors:  [0.25, 0.01, 0.04, 0.04]

    MSE = (0.25 + 0.01 + 0.04 + 0.04) / 4 = 0.085
    RMSE = sqrt(0.085) ≈ 0.291
    
Tradeoff: MSE/RMSE vs R²

MSE and RMSE focus on how big the errors are. Lower values mean better predictions.

R² shows how well the model explains the data variation. Higher values (closer to 1) mean better fit.

Sometimes a model can have a low RMSE but a low R² if it predicts near the mean but misses trends.

For example, if you predict house prices, RMSE tells you average price error, while R² tells you if your model captures price changes well.

What good vs bad metric values look like

Good MSE/RMSE: Small numbers close to zero mean predictions are close to real values.

Bad MSE/RMSE: Large numbers mean big errors in predictions.

Good R²: Values near 1 mean the model explains most of the data variation.

Bad R²: Values near 0 or negative mean the model does not explain the data well or is worse than guessing the mean.

Example:

  • MSE = 0.01, RMSE = 0.1, R² = 0.95 -> Good model
  • MSE = 10, RMSE = 3.16, R² = 0.2 -> Poor model
Common pitfalls in regression metrics
  • Ignoring units: MSE is in squared units, which can be hard to interpret. RMSE fixes this.
  • Overfitting: A model may have very low error on training data but perform poorly on new data.
  • Negative R²: Means the model is worse than just predicting the average.
  • Outliers: Big errors from outliers can inflate MSE and RMSE.
  • Comparing models: Always compare metrics on the same dataset and scale.
Self-check question

Your model has an RMSE of 5 on house prices that range from 100 to 500 thousand dollars, and an R² of 0.85. Is this good?

Answer: Yes, an RMSE of 5 (thousand dollars) means on average your predictions are off by $5,000, which is small compared to the price range. An R² of 0.85 means your model explains 85% of the variation in prices, which is strong. So overall, this is a good model.

Key Result
MSE and RMSE measure average prediction error size, while R² shows how well the model explains data variation.

Practice

(1/5)
1. What does the Mean Squared Error (MSE) measure in regression evaluation?
easy
A. The total number of data points
B. The average of the squared differences between predicted and actual values
C. The correlation between features
D. The percentage of correct predictions

Solution

  1. Step 1: Understand MSE definition

    MSE calculates the average of squared differences between predicted and actual values to measure error size.
  2. Step 2: Compare options to definition

    Only The average of the squared differences between predicted and actual values correctly describes MSE as average squared error; others describe unrelated concepts.
  3. Final Answer:

    The average of the squared differences between predicted and actual values -> Option B
  4. Quick Check:

    MSE = average squared error [OK]
Hint: MSE = average squared error between prediction and truth [OK]
Common Mistakes:
  • Confusing MSE with accuracy
  • Thinking MSE measures correlation
  • Mixing MSE with count of data points
2. Which of the following Python code snippets correctly calculates the Root Mean Squared Error (RMSE) given true values y_true and predicted values y_pred?
easy
A. rmse = np.sum((y_true - y_pred)**2)
B. rmse = np.mean(np.abs(y_true - y_pred))
C. import numpy as np rmse = np.sqrt(np.mean((y_true - y_pred)**2))
D. rmse = np.sqrt(np.sum(y_true - y_pred))

Solution

  1. Step 1: Recall RMSE formula

    RMSE is the square root of the mean of squared differences between true and predicted values.
  2. Step 2: Match code to formula

    import numpy as np rmse = np.sqrt(np.mean((y_true - y_pred)**2)) correctly computes squared differences, averages them, then takes square root. Others miss steps or use wrong operations.
  3. Final Answer:

    import numpy as np rmse = np.sqrt(np.mean((y_true - y_pred)**2)) -> Option C
  4. Quick Check:

    RMSE = sqrt(mean squared error) [OK]
Hint: RMSE = sqrt of mean squared error, use np.sqrt(np.mean(...)) [OK]
Common Mistakes:
  • Using sum instead of mean
  • Taking sqrt before mean
  • Using absolute difference instead of squared
3. Given the true values y_true = [3, -0.5, 2, 7] and predicted values y_pred = [2.5, 0.0, 2, 8], what is the R² score?
medium
A. 1.0
B. 0.865
C. 0.75
D. 0.948

Solution

  1. Step 1: Calculate R² score formula

    R² = 1 - (sum of squared residuals / total sum of squares). Calculate residuals and totals:
  2. Step 2: Compute sums

    Residuals squared: (3-2.5)^2 + (-0.5-0)^2 + (2-2)^2 + (7-8)^2 = 0.25 + 0.25 + 0 + 1 = 1.5
    Total sum squares: sum((y_true - mean(y_true))^2) with mean= (3-0.5+2+7)/4=2.875
    Totals: (3-2.875)^2 + (-0.5-2.875)^2 + (2-2.875)^2 + (7-2.875)^2 = 0.015625 + 11.390625 + 0.765625 + 17.015625 = 29.1875
  3. Step 3: Calculate R²

    R² = 1 - (1.5 / 29.1875) ≈ 1 - 0.0514 = 0.9486
  4. Final Answer:

    0.948 -> Option D
  5. Quick Check:

    R² ≈ 0.95 means good fit [OK]
Hint: R² = 1 - (SS_res / SS_tot), calculate sums carefully [OK]
Common Mistakes:
  • Using sum of residuals instead of squared residuals
  • Forgetting to subtract mean in total sum squares
  • Mixing up numerator and denominator
4. You have this Python code to compute MSE but it gives wrong results:
def mse(y_true, y_pred):
    return sum(y_true - y_pred)**2 / len(y_true)

What is the error?
medium
A. The subtraction is done element-wise but sum is outside, causing wrong squaring
B. The function should use absolute values instead of squares
C. The length should be squared, not the sum
D. The function is missing import statements

Solution

  1. Step 1: Analyze the code operation order

    The code does sum(y_true - y_pred) first, then squares the total sum, which is incorrect for MSE.
  2. Step 2: Correct MSE calculation

    MSE requires squaring each difference first, then averaging. The code should square element-wise before summing.
  3. Final Answer:

    The subtraction is done element-wise but sum is outside, causing wrong squaring -> Option A
  4. Quick Check:

    Square differences before sum for MSE [OK]
Hint: Square each difference before summing for MSE [OK]
Common Mistakes:
  • Squaring sum instead of sum of squares
  • Using absolute difference instead of squared
  • Ignoring element-wise operations
5. You trained two regression models on different datasets. Model A has MSE=4 and R²=0.8, Model B has MSE=2 and R²=0.6. Which model is better and why?
hard
A. Model B is better because it has lower MSE, indicating smaller average error
B. Model A is better because it has higher R², showing better fit to data
C. Both models are equally good because MSE and R² contradict
D. Cannot decide without RMSE values

Solution

  1. Step 1: Understand MSE and R² meanings

    MSE measures average squared error (lower is better). R² measures proportion of variance explained (higher is better).
  2. Step 2: Compare models on error and fit

    Model B has lower MSE (2 < 4), so predictions are closer on average. Model A has higher R² but larger error, meaning it fits variance better but with bigger errors.
  3. Step 3: Prioritize error for prediction quality

    Lower error (MSE) usually means better prediction accuracy, so Model B is better despite lower R².
  4. Final Answer:

    Model B is better because it has lower MSE, indicating smaller average error -> Option A
  5. Quick Check:

    Lower MSE means better prediction accuracy [OK]
Hint: Lower MSE beats higher R² for prediction accuracy [OK]
Common Mistakes:
  • Choosing model with higher R² ignoring error size
  • Thinking MSE and R² always agree
  • Ignoring what metric matters for task
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard