Note
Go to the end to download the full example code
Metrics - errors
Example with the ‘errors’ or scores implemented.
Scores:
-------
Mean Absolute Error : 0.857
Mean Absolute Error (_) : 0.857
Mean Squared Error : 1.429
Mean Squared Log Error : 0.006
Mean Absolute Percentage (_) : 6.355
Mean Directional Accuracy (_) : 0.143
Mean Absolute Scaled error (_) : 1.350
Median Absolute Error : 1.000
8 # General libraries.
9 import numpy as np
10
11 # Sklearn
12 from sklearn.metrics import mean_absolute_error
13 from sklearn.metrics import mean_squared_error
14 from sklearn.metrics import mean_squared_log_error
15 from sklearn.metrics import median_absolute_error
16
17 # pyAMR
18 from pyamr.metrics.scores import _mean_absolute_error
19 from pyamr.metrics.scores import _mean_absolute_percentage_error
20 from pyamr.metrics.scores import _mean_directional_accuracy
21 from pyamr.metrics.scores import _mean_absolute_scaled_error
22
23 # Numpy configuration
24 np.set_printoptions(precision=2)
25
26 # ---------------------------
27 # create data
28 # ---------------------------
29 # Create time series
30 y_true = np.array([10, 11, 12, 11, 12, 11, 13, 12, 13, 14, 15, 16, 18, 17])
31 y_pred = np.array([11, 11, 11, 11, 12, 12, 12, 13, 13, 13, 18, 18, 18, 18])
32
33 # Scores
34 s1 = mean_absolute_error(y_true, y_pred)
35 s2 = _mean_absolute_error(y_true, y_pred)
36 s3 = mean_squared_error(y_true, y_pred)
37 s4 = mean_squared_log_error(y_true, y_pred)
38 s5 = _mean_absolute_percentage_error(y_true, y_pred)
39 s6 = _mean_directional_accuracy(y_true, y_pred)
40 s7 = _mean_absolute_scaled_error(y_true[:10], y_true[10:], y_pred[10:])
41 s10 = median_absolute_error(y_true, y_pred)
42
43 # Compute scores
44 print("\nScores:")
45 print("-------")
46 print('Mean Absolute Error : %.3f' % s1)
47 print('Mean Absolute Error (_) : %.3f' % s2)
48 print('Mean Squared Error : %.3f' % s3)
49 print('Mean Squared Log Error : %.3f' % s4)
50 print('Mean Absolute Percentage (_) : %.3f' % s5)
51 print('Mean Directional Accuracy (_) : %.3f' % s6)
52 print('Mean Absolute Scaled error (_) : %.3f' % s7)
53 print('Median Absolute Error : %.3f' % s10)
Total running time of the script: ( 0 minutes 0.003 seconds)