Skip to content

Commit 62bdef7

Browse files
authored
Merge pull request #116 from darshbaxi/darsh/rmse
Add Root Mean Squared Error (RMSE) metric to pydeepflow.metrics (#111)
2 parents b57f015 + 0683838 commit 62bdef7

3 files changed

Lines changed: 33 additions & 16 deletions

File tree

pydeepflow/metrics.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,12 @@ def r2_score(y_true, y_pred):
157157
ss_tot = np.sum((y_true - np.mean(y_true)) ** 2)
158158
return 1 - (ss_res / ss_tot)
159159

160-
def rmse(y_true, y_pred):
160+
def root_mean_squared_error(y_true, y_pred):
161161
"""
162-
Compute Root Mean Squared Error between true and predicted values.
163-
162+
Calculates the Root Mean Squared Error (RMSE).
163+
164164
RMSE = sqrt((1/n) * Σ(y_true - y_pred)^2)
165-
165+
166166
Parameters
167167
----------
168168
y_true : array-like
@@ -175,7 +175,5 @@ def rmse(y_true, y_pred):
175175
float
176176
The RMSE score.
177177
"""
178-
y_true = np.array(y_true)
179-
y_pred = np.array(y_pred)
180-
return np.sqrt(np.mean((y_true - y_pred) ** 2))
181-
178+
return ((np.array(y_true) - np.array(y_pred)) ** 2).mean() ** 0.5
179+

pydeepflow/model.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import matplotlib.pyplot as plt
55
from pydeepflow.activations import activation, activation_derivative
66
from pydeepflow.losses import get_loss_function, get_loss_derivative
7-
from pydeepflow.metrics import precision_score, recall_score, f1_score, confusion_matrix,mean_absolute_error, mean_squared_error, r2_score
7+
from pydeepflow.metrics import precision_score, recall_score, f1_score, confusion_matrix,mean_absolute_error, mean_squared_error, r2_score, root_mean_squared_error
88
from pydeepflow.device import Device
99
from pydeepflow.regularization import Regularization
1010
from pydeepflow.checkpoints import ModelCheckpoint
@@ -817,7 +817,7 @@ def evaluate(self, X, y, metrics=['loss', 'accuracy']):
817817
y (np.ndarray): The true labels for evaluation.
818818
metrics (list, optional): A list of metrics to calculate.
819819
Defaults to ['loss', 'accuracy'].
820-
Available metrics: 'loss', 'accuracy', 'precision', 'recall', 'f1_score', 'confusion_matrix'.
820+
Available metrics: 'loss', 'accuracy', 'precision', 'recall', 'f1_score', 'confusion_matrix', 'root_mean_squared_error'.
821821
822822
Returns:
823823
dict: A dictionary where keys are the metric names and values are the computed scores.
@@ -856,6 +856,9 @@ def evaluate(self, X, y, metrics=['loss', 'accuracy']):
856856
if 'r2_score' in metrics:
857857
results['r2_score'] = r2_score(y, predictions)
858858

859+
if 'root_mean_squared_error' in metrics:
860+
results['root_mean_squared_error'] = root_mean_squared_error(y, predictions)
861+
859862

860863

861864
return results
@@ -1740,6 +1743,9 @@ def evaluate(self, X, y, metrics=['loss', 'accuracy']):
17401743

17411744
if 'r2_score' in metrics:
17421745
results['r2_score'] = r2_score(y, predictions)
1746+
1747+
if 'root_mean_squared_error' in metrics:
1748+
results['root_mean_squared_error'] = root_mean_squared_error(y, predictions)
17431749

17441750
return results # Removed confusion_matrix for simplification/dependency reasons
17451751

tests/test_metrics.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
import numpy as np
33
from pydeepflow.metrics import (
44
precision_score, recall_score, f1_score, confusion_matrix,
5-
mean_absolute_error, mean_squared_error, r2_score, rmse
5+
mean_absolute_error, mean_squared_error, r2_score,root_mean_squared_error
6+
67
)
78

89
class TestMetrics(unittest.TestCase):
@@ -54,11 +55,23 @@ def test_r2_score(self):
5455
# R^2 = 1 - (1.5 / 29.1875) = 1 - 0.051389... approx 0.9486
5556
self.assertAlmostEqual(r2_score(self.y_true_reg, self.y_pred_reg), 0.94861051, places=5)
5657

57-
def test_rmse(self):
58-
y_true = np.array([1, 2, 3])
59-
y_pred = np.array([2, 2, 4])
60-
expected = np.sqrt(((1-2)**2 + (2-2)**2 + (3-4)**2) / 3)
61-
self.assertAlmostEqual(rmse(y_true, y_pred), expected)
58+
59+
def test_root_mean_squared_error(self):
60+
# Step 1: Differences
61+
# (3 - 2.5) = 0.5
62+
# (-0.5 - 0.0) = -0.5
63+
# (2 - 2) = 0
64+
# (7 - 8) = -1
65+
#
66+
# Step 2: Squared differences
67+
# [0.5², (-0.5)², 0², (-1)²] = [0.25, 0.25, 0, 1]
68+
#
69+
# Step 3: Mean Squared Error (MSE)
70+
# (0.25 + 0.25 + 0 + 1) / 4 = 0.375
71+
#
72+
# Step 4: Root Mean Squared Error (RMSE)
73+
# sqrt(0.375) = 0.6123724356957945
74+
self.assertAlmostEqual(root_mean_squared_error(self.y_true_reg, self.y_pred_reg), 0.6123724356957945, places=6)
6275

6376
if __name__ == '__main__':
6477
unittest.main()

0 commit comments

Comments
 (0)