0
0
ML Pythonml~10 mins

Time series evaluation metrics 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 Mean Absolute Error (MAE) between true and predicted values.

ML Python
from sklearn.metrics import [1]
true = [3, -0.5, 2, 7]
pred = [2.5, 0.0, 2, 8]
mae = [1](true, pred)
print(mae)
Drag options to blanks, or click blank then click option'
Ar2_score
Bmean_absolute_error
Caccuracy_score
Dmean_squared_error
Attempts:
3 left
💡 Hint
Common Mistakes
Using mean_squared_error instead of mean_absolute_error
Using accuracy_score which is for classification
2fill in blank
medium

Complete the code to calculate the Root Mean Squared Error (RMSE) from the Mean Squared Error (MSE).

ML Python
from sklearn.metrics import mean_squared_error
import numpy as np
true = [3, -0.5, 2, 7]
pred = [2.5, 0.0, 2, 8]
mse = mean_squared_error(true, pred)
rmse = np.[1](mse)
print(rmse)
Drag options to blanks, or click blank then click option'
Asquare
Bexp
Clog
Dsqrt
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.square instead of np.sqrt
Using np.log or np.exp which are unrelated
3fill in blank
hard

Fix the error in the code to calculate Mean Absolute Percentage Error (MAPE) manually.

ML Python
true = [100, 200, 300, 400]
pred = [110, 190, 310, 420]
mape = 100 * sum(abs((true[i] - pred[i]) / [1]) for i in range(len(true))) / len(true)
print(mape)
Drag options to blanks, or click blank then click option'
Asum(true)
Bpred[i]
Ctrue[i]
Dlen(true)
Attempts:
3 left
💡 Hint
Common Mistakes
Dividing by predicted value instead of true value
Dividing by sum or length instead of element-wise true value
4fill in blank
hard

Fill both blanks to create a dictionary of errors where keys are error names and values are their computed scores.

ML Python
from sklearn.metrics import mean_absolute_error, mean_squared_error
true = [1, 2, 3, 4]
pred = [1.1, 1.9, 3.2, 3.8]
errors = {
    'MAE': [1](true, pred),
    'MSE': [2](true, pred)
}
print(errors)
Drag options to blanks, or click blank then click option'
Amean_absolute_error
Bmean_squared_error
Cr2_score
Daccuracy_score
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up MAE and MSE function names
Using classification metrics like accuracy_score
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps each error name to its score, filtering only errors with score less than 0.5.

ML Python
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
true = [2, 4, 6, 8]
pred = [2.1, 3.9, 6.2, 7.8]
errors = {
    [1]: [2](true, pred)
    for [3] in ['mean_absolute_error', 'mean_squared_error', 'r2_score']
    if [2](true, pred) < 0.5
}
print(errors)
Drag options to blanks, or click blank then click option'
Aerror_name
Bglobals()[error_name]
Derror
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently
Trying to call functions without using globals() or similar