0
0
ML Pythonml~10 mins

Evaluation metrics (RMSE, precision@k) in ML Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to calculate the RMSE between true and predicted values.

ML Python
import numpy as np

def rmse(y_true, y_pred):
    return np.sqrt(np.mean((y_true - y_pred)[1]2))
Drag options to blanks, or click blank then click option'
A**
B*
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication (*) instead of power (**) to square the difference.
Adding (+) or subtracting (-) instead of squaring.
2fill in blank
medium

Complete the code to calculate precision@k for a list of predicted scores and true labels.

ML Python
def precision_at_k(y_true, y_scores, k):
    top_k_indices = sorted(range(len(y_scores)), key=lambda i: y_scores[i], reverse=True)[:[1]]
    relevant = sum(y_true[i] for i in top_k_indices)
    return relevant / k
Drag options to blanks, or click blank then click option'
Alen(y_scores)
Bsum(y_true)
Ck
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using the length of all scores instead of k.
Using zero or sum of true labels incorrectly.
3fill in blank
hard

Fix the error in the RMSE calculation by completing the code.

ML Python
def rmse(y_true, y_pred):
    differences = y_true - y_pred
    squared_diff = differences[1]2
    mean_squared_diff = squared_diff.mean()
    return mean_squared_diff ** 0.5
Drag options to blanks, or click blank then click option'
A-
B*
C+
D**
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication (*) instead of power (**).
Using addition or subtraction operators.
4fill in blank
hard

Fill both blanks to complete the precision@k calculation correctly.

ML Python
def precision_at_k(y_true, y_scores, k):
    top_k_indices = sorted(range(len(y_scores)), key=lambda i: y_scores[i], reverse=True)[:[1]]
    relevant = sum(y_true[i] for i in top_k_indices)
    return relevant [2] k
Drag options to blanks, or click blank then click option'
Ak
Blen(y_scores)
C/
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong number for slicing top items.
Multiplying instead of dividing for precision.
5fill in blank
hard

Fill all three blanks to complete the RMSE function with numpy correctly.

ML Python
import numpy as np

def rmse(y_true, y_pred):
    error = y_true [1] y_pred
    squared_error = error [2] 2
    mean_squared_error = np.[3](squared_error)
    return np.sqrt(mean_squared_error)
Drag options to blanks, or click blank then click option'
A-
B**
Cmean
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Adding instead of subtracting errors.
Using multiplication instead of power for squaring.
Using sum instead of mean for averaging.