-
-
Notifications
You must be signed in to change notification settings - Fork 50.6k
Expand file tree
/
Copy pathmean_squared_error.py
More file actions
33 lines (23 loc) · 865 Bytes
/
mean_squared_error.py
File metadata and controls
33 lines (23 loc) · 865 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import numpy as np
def mean_squared_error(y_true: np.ndarray, y_pred: np.ndarray) -> float:
"""
Calculate the Mean Squared Error (MSE) between two arrays.
Parameters:
- y_true: The true values (ground truth).
- y_pred: The predicted values.
Returns:
- mse: The Mean Squared Error between y_true and y_pred.
Example usage:
true_values = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
predicted_values = np.array([0.8, 2.1, 2.9, 4.2, 5.2])
mse = mean_squared_error(true_values, predicted_values)
print(f"Mean Squared Error: {mse}")
"""
if len(y_true) != len(y_pred):
raise ValueError("Input arrays must have the same length.")
squared_errors = np.square(np.subtract(y_true, y_pred))
mse = np.mean(squared_errors)
return mse
if __name__ == "__main__":
import doctest
doctest.testmod()