Calculate the mean squared error (MSE) between ground truth and predicted values. MSE measures the squared difference between true values and predicted values, and it serves as a measure of accuracy for regression models. MSE = (1/n) * Σ(y_true - y_pred)^2 Reference: https://
(y_true: np.ndarray, y_pred: np.ndarray)
| 331 | |
| 332 | |
| 333 | def mean_squared_error(y_true: np.ndarray, y_pred: np.ndarray) -> float: |
| 334 | """ |
| 335 | Calculate the mean squared error (MSE) between ground truth and predicted values. |
| 336 | |
| 337 | MSE measures the squared difference between true values and predicted values, and it |
| 338 | serves as a measure of accuracy for regression models. |
| 339 | |
| 340 | MSE = (1/n) * Σ(y_true - y_pred)^2 |
| 341 | |
| 342 | Reference: https://en.wikipedia.org/wiki/Mean_squared_error |
| 343 | |
| 344 | Parameters: |
| 345 | - y_true: The true values (ground truth) |
| 346 | - y_pred: The predicted values |
| 347 | |
| 348 | >>> true_values = np.array([1.0, 2.0, 3.0, 4.0, 5.0]) |
| 349 | >>> predicted_values = np.array([0.8, 2.1, 2.9, 4.2, 5.2]) |
| 350 | >>> bool(np.isclose(mean_squared_error(true_values, predicted_values), 0.028)) |
| 351 | True |
| 352 | >>> true_labels = np.array([1.0, 2.0, 3.0, 4.0, 5.0]) |
| 353 | >>> predicted_probs = np.array([0.3, 0.8, 0.9, 0.2]) |
| 354 | >>> mean_squared_error(true_labels, predicted_probs) |
| 355 | Traceback (most recent call last): |
| 356 | ... |
| 357 | ValueError: Input arrays must have the same length. |
| 358 | """ |
| 359 | if len(y_true) != len(y_pred): |
| 360 | raise ValueError("Input arrays must have the same length.") |
| 361 | |
| 362 | squared_errors = (y_true - y_pred) ** 2 |
| 363 | return np.mean(squared_errors) |
| 364 | |
| 365 | |
| 366 | def mean_absolute_error(y_true: np.ndarray, y_pred: np.ndarray) -> float: |