We use evaluation metrics to check how well a machine learning model is doing. RMSE tells us how close predictions are to actual values, and precision@k shows how many of the top k predictions are correct.
Evaluation metrics (RMSE, precision@k) in ML Python
RMSE = sqrt(mean((y_true - y_pred)**2)) precision@k = (Number of relevant items in top k predictions) / k
RMSE measures average error size in the same units as the target.
Precision@k focuses on the top k results, useful for ranking or recommendation tasks.
from sklearn.metrics import mean_squared_error import numpy as np y_true = [3, -0.5, 2, 7] y_pred = [2.5, 0.0, 2, 8] rmse = np.sqrt(mean_squared_error(y_true, y_pred)) print(rmse)
def precision_at_k(y_true, y_pred, k): top_k = y_pred[:k] relevant = sum([1 for item in top_k if item in y_true]) return relevant / k # Example relevant_items = ['apple', 'banana', 'orange'] predicted_items = ['banana', 'apple', 'grape', 'orange'] print(precision_at_k(relevant_items, predicted_items, 3))
This program calculates RMSE for numeric predictions and precision@3 for recommended items, showing how close predictions are and how many top recommendations are correct.
from sklearn.metrics import mean_squared_error import numpy as np def precision_at_k(y_true, y_pred, k): top_k = y_pred[:k] relevant = sum([1 for item in top_k if item in y_true]) return relevant / k # RMSE example actual = [10, 20, 30, 40, 50] predicted = [12, 18, 33, 37, 52] rmse_value = np.sqrt(mean_squared_error(actual, predicted)) # precision@k example true_items = ['cat', 'dog', 'rabbit'] predicted_items = ['dog', 'rabbit', 'horse', 'cat', 'mouse'] precision_value = precision_at_k(true_items, predicted_items, 3) print(f"RMSE: {rmse_value:.2f}") print(f"Precision@3: {precision_value:.2f}")
RMSE is sensitive to large errors because it squares the differences.
Precision@k does not consider the order beyond the top k items or how many relevant items exist beyond k.
Always choose metrics that fit your problem type: regression or ranking.
RMSE measures average prediction error size for numbers.
Precision@k measures accuracy of top k predictions in ranking tasks.
Use these metrics to understand and improve your model's performance.
Practice
Solution
Step 1: Understand RMSE definition
RMSE calculates the square root of the average squared differences between predicted and actual values, showing error size.Step 2: Identify what RMSE measures
It measures how far predictions are from actual continuous values on average, not classification accuracy or speed.Final Answer:
The average size of prediction errors for continuous values -> Option AQuick Check:
RMSE = average error size [OK]
- Confusing RMSE with accuracy
- Thinking RMSE measures classification counts
- Assuming RMSE measures speed
Solution
Step 1: Recall RMSE formula
RMSE is the square root of the average squared differences between true and predicted values.Step 2: Match formula options
\(\sqrt{\frac{1}{n} \sum_{i=1}^n (y_i - \hat{y}_i)^2}\) matches the RMSE formula exactly; others represent MAE, mean error, or precision.Final Answer:
\(\sqrt{\frac{1}{n} \sum_{i=1}^n (y_i - \hat{y}_i)^2}\) -> Option DQuick Check:
RMSE = sqrt(mean squared error) [OK]
- Confusing RMSE with MAE (mean absolute error)
- Forgetting to square errors before averaging
- Mixing precision formula with RMSE
Solution
Step 1: Identify top 3 predictions by score
Top 3 scores are 0.9 (index 0), 0.8 (index 2), and 0.4 (index 3).Step 2: Count true positives in top 3
True labels at these indices are 1 (index 0), 1 (index 2), 1 (index 3). Total true positives = 3.Step 3: Calculate precision@3
Precision@3 = true positives / 3 = 3/3 = 1.0.Final Answer:
1.0 -> Option AQuick Check:
Precision@3 = 3/3 = 1.0 [OK]
- Counting all positives, not just top k
- Mixing precision@k with recall
- Using predicted labels instead of scores for ranking
Solution
Step 1: Understand precision@5 meaning
Precision@5 measures how many relevant items appear in the top 5 predictions.Step 2: Identify how to increase precision@5
To increase precision@5, the model must rank more relevant items higher within the top 5.Step 3: Evaluate options
Improve the model to rank more relevant items in the top 5 improves ranking of relevant items; A changes dataset size but not ranking; C reduces true positives, lowering precision; D changes metric but not model quality.Final Answer:
Improve the model to rank more relevant items in the top 5 -> Option BQuick Check:
Better ranking = higher precision@5 [OK]
- Changing dataset size instead of model quality
- Reducing true positives mistakenly
- Confusing RMSE with precision@k
Solution
Step 1: Calculate squared errors for each prediction
(3.0-2.5)^2=0.25, (-0.5-0.0)^2=0.25, (2.0-2.1)^2=0.01, (7.5-7.8)^2=0.09. Sum=0.60.Step 2: Compute mean squared error and RMSE
Mean squared error = 0.60/4 = 0.15; RMSE = \(\sqrt{0.15}\) \(\approx\) 0.39.Step 3: Compare RMSE with 0.5
RMSE \(\approx\) 0.39 < 0.5, so average error is less than 0.5.Final Answer:
RMSE = 0.39, yes the error is less than 0.5 -> Option CQuick Check:
RMSE \(\approx\) 0.39 < 0.5 [OK]
- Forgetting to square errors
- Incorrect averaging of squared errors
- Mixing RMSE with MAE
Ready to prove it?
Pass the test and this pattern moves unknown
