Skip to content

Instantly share code, notes, and snippets.

@morkapronczay
Last active March 21, 2023 08:30
Show Gist options
  • Save morkapronczay/950400a93466805fceba65d098168e7c to your computer and use it in GitHub Desktop.
Save morkapronczay/950400a93466805fceba65d098168e7c to your computer and use it in GitHub Desktop.
A snippet on how to calculate mean squared error (MSE) from scratch.
import numpy as np
actual = np.array([1, 2, 3, 4, 5])
predicted = np.array([1.1, 1.9, 2.7, 4.5, 6])
def mse(actual: np.ndarray, predicted: np.ndarray) -> float:
differences = np.subtract(actual, predicted)
squared_differences = np.square(differences)
return np.mean(squared_differences)
mse(actual, predicted)
# 0.27199999999999996
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment