Documentation for Metrics¶

Common metrics to assess performance on NARX models.

explained_variance_score(y, yhat)¶

Calculate the Explained Variance Score.

Parameters¶

y : array-like of shape = number_of_outputs Represent the target values. yhat : array-like of shape = number_of_outputs Target values predicted by the model.

Returns¶

loss : float EVS output is non-negative values. Becoming 1.0 means your model outputs are exactly matched by true target values. Lower values means worse results.

Examples¶

y = [3, -0.5, 2, 7] yhat = [2.5, 0.0, 2, 8] explained_variance_score(y, yhat) 0.957

Source code in sysidentpy/metrics/_regression.py
 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 def explained_variance_score(y: NDArray, yhat: NDArray) -> NDArray: """Calculate the Explained Variance Score. Parameters ---------- y : array-like of shape = number_of_outputs Represent the target values. yhat : array-like of shape = number_of_outputs Target values predicted by the model. Returns ------- loss : float EVS output is non-negative values. Becoming 1.0 means your model outputs are exactly matched by true target values. Lower values means worse results. References ---------- - Wikipedia entry on the Explained Variance https://en.wikipedia.org/wiki/Explained_variation Examples -------- >>> y = [3, -0.5, 2, 7] >>> yhat = [2.5, 0.0, 2, 8] >>> explained_variance_score(y, yhat) 0.957 """ y_diff_avg = np.average(y - yhat) numerator = np.average((y - yhat - y_diff_avg) ** 2) y_avg = np.average(y) denominator = np.average((y - y_avg) ** 2) nonzero_numerator = numerator != 0 nonzero_denominator = denominator != 0 valid_score = nonzero_numerator & nonzero_denominator output_scores = np.ones(y.shape[0]) output_scores[valid_score] = 1 - (numerator[valid_score] / denominator[valid_score]) output_scores[nonzero_numerator & ~nonzero_denominator] = 0.0 return np.average(output_scores) 

forecast_error(y, yhat)¶

Calculate the forecast error in a regression model.

Parameters¶

y : array-like of shape = number_of_outputs Represent the target values. yhat : array-like of shape = number_of_outputs Target values predicted by the model.

Returns¶

loss : ndarray of floats The difference between the true target values and the predicted or forecast value in regression or any other phenomenon.

Examples¶

y = [3, -0.5, 2, 7] yhat = [2.5, 0.0, 2, 8] forecast_error(y, yhat) [0.5, -0.5, 0, -1]

Source code in sysidentpy/metrics/_regression.py
 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 def forecast_error(y: NDArray, yhat: NDArray) -> NDArray: """Calculate the forecast error in a regression model. Parameters ---------- y : array-like of shape = number_of_outputs Represent the target values. yhat : array-like of shape = number_of_outputs Target values predicted by the model. Returns ------- loss : ndarray of floats The difference between the true target values and the predicted or forecast value in regression or any other phenomenon. References ---------- - Wikipedia entry on the Forecast error https://en.wikipedia.org/wiki/Forecast_error Examples -------- >>> y = [3, -0.5, 2, 7] >>> yhat = [2.5, 0.0, 2, 8] >>> forecast_error(y, yhat) [0.5, -0.5, 0, -1] """ return np.array(y - yhat) 

mean_absolute_error(y, yhat)¶

Calculate the Mean absolute error.

Parameters¶

y : array-like of shape = number_of_outputs Represent the target values. yhat : array-like of shape = number_of_outputs Target values predicted by the model.

Returns¶

loss : float or ndarray of floats MAE output is non-negative values. Becoming 0.0 means your model outputs are exactly matched by true target values.

Examples¶

y = [3, -0.5, 2, 7] yhat = [2.5, 0.0, 2, 8] mean_absolute_error(y, yhat) 0.5

Source code in sysidentpy/metrics/_regression.py
 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 def mean_absolute_error(y: NDArray, yhat: NDArray) -> NDArray: """Calculate the Mean absolute error. Parameters ---------- y : array-like of shape = number_of_outputs Represent the target values. yhat : array-like of shape = number_of_outputs Target values predicted by the model. Returns ------- loss : float or ndarray of floats MAE output is non-negative values. Becoming 0.0 means your model outputs are exactly matched by true target values. References ---------- - Wikipedia entry on the Mean absolute error https://en.wikipedia.org/wiki/Mean_absolute_error Examples -------- >>> y = [3, -0.5, 2, 7] >>> yhat = [2.5, 0.0, 2, 8] >>> mean_absolute_error(y, yhat) 0.5 """ output_errors = np.average(np.abs(y - yhat)) return np.average(output_errors) 

mean_forecast_error(y, yhat)¶

Calculate the mean of forecast error of a regression model.

Parameters¶

y : array-like of shape = number_of_outputs Represent the target values. yhat : array-like of shape = number_of_outputs Target values predicted by the model.

Returns¶

loss : float The mean value of the difference between the true target values and the predicted or forecast value in regression or any other phenomenon.

Examples¶

y = [3, -0.5, 2, 7] yhat = [2.5, 0.0, 2, 8] mean_forecast_error(y, yhat) -0.25

Source code in sysidentpy/metrics/_regression.py
 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 def mean_forecast_error(y: NDArray, yhat: NDArray) -> NDArray: """Calculate the mean of forecast error of a regression model. Parameters ---------- y : array-like of shape = number_of_outputs Represent the target values. yhat : array-like of shape = number_of_outputs Target values predicted by the model. Returns ------- loss : float The mean value of the difference between the true target values and the predicted or forecast value in regression or any other phenomenon. References ---------- - Wikipedia entry on the Forecast error https://en.wikipedia.org/wiki/Forecast_error Examples -------- >>> y = [3, -0.5, 2, 7] >>> yhat = [2.5, 0.0, 2, 8] >>> mean_forecast_error(y, yhat) -0.25 """ return np.average(y - yhat) 

mean_squared_error(y, yhat)¶

Calculate the Mean Squared Error.

Parameters¶

y : array-like of shape = number_of_outputs Represent the target values. yhat : array-like of shape = number_of_outputs Target values predicted by the model.

Returns¶

loss : float MSE output is non-negative values. Becoming 0.0 means your model outputs are exactly matched by true target values.

Examples¶

y = [3, -0.5, 2, 7] yhat = [2.5, 0.0, 2, 8] mean_squared_error(y, yhat) 0.375

Source code in sysidentpy/metrics/_regression.py
  95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 def mean_squared_error(y: NDArray, yhat: NDArray) -> NDArray: """Calculate the Mean Squared Error. Parameters ---------- y : array-like of shape = number_of_outputs Represent the target values. yhat : array-like of shape = number_of_outputs Target values predicted by the model. Returns ------- loss : float MSE output is non-negative values. Becoming 0.0 means your model outputs are exactly matched by true target values. References ---------- - Wikipedia entry on the Mean Squared Error https://en.wikipedia.org/wiki/Mean_squared_error Examples -------- >>> y = [3, -0.5, 2, 7] >>> yhat = [2.5, 0.0, 2, 8] >>> mean_squared_error(y, yhat) 0.375 """ output_error = np.average((y - yhat) ** 2) return np.average(output_error) 

mean_squared_log_error(y, yhat)¶

Calculate the Mean Squared Logarithmic Error.

Parameters¶

y : array-like of shape = number_of_outputs Represent the target values. yhat : array-like of shape = number_of_outputs Target values predicted by the model.

Returns¶

loss : float MSLE output is non-negative values. Becoming 0.0 means your model outputs are exactly matched by true target values.

Examples¶

y = [3, 5, 2.5, 7] yhat = [2.5, 5, 4, 8] mean_squared_log_error(y, yhat) 0.039

Source code in sysidentpy/metrics/_regression.py
 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 def mean_squared_log_error(y: NDArray, yhat: NDArray) -> NDArray: """Calculate the Mean Squared Logarithmic Error. Parameters ---------- y : array-like of shape = number_of_outputs Represent the target values. yhat : array-like of shape = number_of_outputs Target values predicted by the model. Returns ------- loss : float MSLE output is non-negative values. Becoming 0.0 means your model outputs are exactly matched by true target values. Examples -------- >>> y = [3, 5, 2.5, 7] >>> yhat = [2.5, 5, 4, 8] >>> mean_squared_log_error(y, yhat) 0.039 """ return mean_squared_error(np.log1p(y), np.log1p(yhat)) 

median_absolute_error(y, yhat)¶

Calculate the Median Absolute Error.

Parameters¶

y : array-like of shape = number_of_outputs Represent the target values. yhat : array-like of shape = number_of_outputs Target values predicted by the model.

Returns¶

loss : float MdAE output is non-negative values. Becoming 0.0 means your model outputs are exactly matched by true target values.

Examples¶

y = [3, -0.5, 2, 7] yhat = [2.5, 0.0, 2, 8] median_absolute_error(y, yhat) 0.5

Source code in sysidentpy/metrics/_regression.py
 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 def median_absolute_error(y: NDArray, yhat: NDArray) -> NDArray: """Calculate the Median Absolute Error. Parameters ---------- y : array-like of shape = number_of_outputs Represent the target values. yhat : array-like of shape = number_of_outputs Target values predicted by the model. Returns ------- loss : float MdAE output is non-negative values. Becoming 0.0 means your model outputs are exactly matched by true target values. References ---------- - Wikipedia entry on the Median absolute deviation https://en.wikipedia.org/wiki/Median_absolute_deviation Examples -------- >>> y = [3, -0.5, 2, 7] >>> yhat = [2.5, 0.0, 2, 8] >>> median_absolute_error(y, yhat) 0.5 """ return np.median(np.abs(y - yhat)) 

normalized_root_mean_squared_error(y, yhat)¶

Calculate the normalized Root Mean Squared Error.

Parameters¶

y : array-like of shape = number_of_outputs Represent the target values. yhat : array-like of shape = number_of_outputs Target values predicted by the model.

Returns¶

loss : float nRMSE output is non-negative values. Becoming 0.0 means your model outputs are exactly matched by true target values.

Examples¶

y = [3, -0.5, 2, 7] yhat = [2.5, 0.0, 2, 8] normalized_root_mean_squared_error(y, yhat) 0.081

Source code in sysidentpy/metrics/_regression.py
 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 def normalized_root_mean_squared_error(y: NDArray, yhat: NDArray) -> NDArray: """Calculate the normalized Root Mean Squared Error. Parameters ---------- y : array-like of shape = number_of_outputs Represent the target values. yhat : array-like of shape = number_of_outputs Target values predicted by the model. Returns ------- loss : float nRMSE output is non-negative values. Becoming 0.0 means your model outputs are exactly matched by true target values. References ---------- - Wikipedia entry on the normalized Root Mean Squared Error https://en.wikipedia.org/wiki/Root-mean-square_deviation Examples -------- >>> y = [3, -0.5, 2, 7] >>> yhat = [2.5, 0.0, 2, 8] >>> normalized_root_mean_squared_error(y, yhat) 0.081 """ return root_mean_squared_error(y, yhat) / (y.max() - y.min()) 

r2_score(y, yhat)¶

Calculate the R2 score. Based on sklearn solution.

Parameters¶

y : array-like of shape = number_of_outputs Represent the target values. yhat : array-like of shape = number_of_outputs Target values predicted by the model.

Returns¶

loss : float R2 output can be non-negative values or negative value. Becoming 1.0 means your model outputs are exactly matched by true target values. Lower values means worse results.

Notes¶

This is not a symmetric function.

Examples¶

y = [3, -0.5, 2, 7] yhat = [2.5, 0.0, 2, 8] explained_variance_score(y, yhat) 0.948

Source code in sysidentpy/metrics/_regression.py
 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 def r2_score(y: NDArray, yhat: NDArray) -> NDArray: """Calculate the R2 score. Based on sklearn solution. Parameters ---------- y : array-like of shape = number_of_outputs Represent the target values. yhat : array-like of shape = number_of_outputs Target values predicted by the model. Returns ------- loss : float R2 output can be non-negative values or negative value. Becoming 1.0 means your model outputs are exactly matched by true target values. Lower values means worse results. Notes ----- This is not a symmetric function. References ---------- - Wikipedia entry on the Coefficient of determination https://en.wikipedia.org/wiki/Coefficient_of_determination Examples -------- >>> y = [3, -0.5, 2, 7] >>> yhat = [2.5, 0.0, 2, 8] >>> explained_variance_score(y, yhat) 0.948 """ numerator = ((y - yhat) ** 2).sum(axis=0, dtype=np.float64) denominator = ((y - np.average(y, axis=0)) ** 2).sum(axis=0, dtype=np.float64) nonzero_denominator = denominator != 0 nonzero_numerator = numerator != 0 valid_score = nonzero_denominator & nonzero_numerator output_scores = np.ones([y.shape[1]]) output_scores[valid_score] = 1 - (numerator[valid_score] / denominator[valid_score]) # arbitrary set to zero to avoid -inf scores, having a constant # y_true is not interesting for scoring a regression anyway output_scores[nonzero_numerator & ~nonzero_denominator] = 0.0 return np.average(output_scores) 

root_mean_squared_error(y, yhat)¶

Calculate the Root Mean Squared Error.

Parameters¶

y : array-like of shape = number_of_outputs Represent the target values. yhat : array-like of shape = number_of_outputs Target values predicted by the model.

Returns¶

loss : float RMSE output is non-negative values. Becoming 0.0 means your model outputs are exactly matched by true target values.

Examples¶

y = [3, -0.5, 2, 7] yhat = [2.5, 0.0, 2, 8] root_mean_squared_error(y, yhat) 0.612

Source code in sysidentpy/metrics/_regression.py
 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 def root_mean_squared_error(y: NDArray, yhat: NDArray) -> NDArray: """Calculate the Root Mean Squared Error. Parameters ---------- y : array-like of shape = number_of_outputs Represent the target values. yhat : array-like of shape = number_of_outputs Target values predicted by the model. Returns ------- loss : float RMSE output is non-negative values. Becoming 0.0 means your model outputs are exactly matched by true target values. References ---------- - Wikipedia entry on the Root Mean Squared Error https://en.wikipedia.org/wiki/Root-mean-square_deviation Examples -------- >>> y = [3, -0.5, 2, 7] >>> yhat = [2.5, 0.0, 2, 8] >>> root_mean_squared_error(y, yhat) 0.612 """ return np.sqrt(mean_squared_error(y, yhat)) 

root_relative_squared_error(y, yhat)¶

Calculate the Root Relative Mean Squared Error.

Parameters¶

y : array-like of shape = number_of_outputs Represent the target values. yhat : array-like of shape = number_of_outputs Target values predicted by the model.

Returns¶

loss : float RRSE output is non-negative values. Becoming 0.0 means your model outputs are exactly matched by true target values.

Examples¶

y = [3, -0.5, 2, 7] yhat = [2.5, 0.0, 2, 8] root_relative_mean_squared_error(y, yhat) 0.206

Source code in sysidentpy/metrics/_regression.py
 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 def root_relative_squared_error(y: NDArray, yhat: NDArray) -> NDArray: """Calculate the Root Relative Mean Squared Error. Parameters ---------- y : array-like of shape = number_of_outputs Represent the target values. yhat : array-like of shape = number_of_outputs Target values predicted by the model. Returns ------- loss : float RRSE output is non-negative values. Becoming 0.0 means your model outputs are exactly matched by true target values. Examples -------- >>> y = [3, -0.5, 2, 7] >>> yhat = [2.5, 0.0, 2, 8] >>> root_relative_mean_squared_error(y, yhat) 0.206 """ numerator = np.sum(np.square((yhat - y))) denominator = np.sum(np.square((y - np.mean(y, axis=0)))) return np.sqrt(np.divide(numerator, denominator)) 

symmetric_mean_absolute_percentage_error(y, yhat)¶

Calculate the SMAPE score.

Parameters¶

y : array-like of shape = number_of_outputs Represent the target values. yhat : array-like of shape = number_of_outputs Target values predicted by the model.

Returns¶

loss : float SMAPE output is a non-negative value. The results are percentages values.

Notes¶

One supposed problem with SMAPE is that it is not symmetric since over-forecasts and under-forecasts are not treated equally.

Examples¶

y = [3, -0.5, 2, 7] yhat = [2.5, 0.0, 2, 8] symmetric_mean_absolute_percentage_error(y, yhat) 57.87

Source code in sysidentpy/metrics/_regression.py
 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 def symmetric_mean_absolute_percentage_error(y: NDArray, yhat: NDArray) -> NDArray: """Calculate the SMAPE score. Parameters ---------- y : array-like of shape = number_of_outputs Represent the target values. yhat : array-like of shape = number_of_outputs Target values predicted by the model. Returns ------- loss : float SMAPE output is a non-negative value. The results are percentages values. Notes ----- One supposed problem with SMAPE is that it is not symmetric since over-forecasts and under-forecasts are not treated equally. References ---------- - Wikipedia entry on the Symmetric mean absolute percentage error https://en.wikipedia.org/wiki/Symmetric_mean_absolute_percentage_error Examples -------- >>> y = [3, -0.5, 2, 7] >>> yhat = [2.5, 0.0, 2, 8] >>> symmetric_mean_absolute_percentage_error(y, yhat) 57.87 """ return 100 / len(y) * np.sum(2 * np.abs(yhat - y) / (np.abs(y) + np.abs(yhat)))