Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions pydeepflow/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,25 @@ def r2_score(y_true, y_pred):
ss_tot = np.sum((y_true - np.mean(y_true)) ** 2)
return 1 - (ss_res / ss_tot)

def rmse(y_true, y_pred):
"""
Compute Root Mean Squared Error between true and predicted values.

RMSE = sqrt((1/n) * Σ(y_true - y_pred)^2)

Parameters
----------
y_true : array-like
Ground truth (correct) target values.
y_pred : array-like
Estimated target values.

Returns
-------
float
The RMSE score.
"""
y_true = np.array(y_true)
y_pred = np.array(y_pred)
return np.sqrt(np.mean((y_true - y_pred) ** 2))

2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
numpy>=1.23.5
pandas>=1.5.3
scikit-learn>=1.2.0
scikit-learn>=1.3.0
jupyter>=1.0.0
tqdm>=4.64.1
colorama>=0.4.6
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
install_requires=[
"numpy>=1.23.5",
"pandas>=1.5.3",
"scikit-learn>=1.2.0",
"scikit-learn>=1.3.0",
"jupyter>=1.0.0",
"tqdm>=4.64.1",
"colorama>=0.4.6",
Expand Down
8 changes: 7 additions & 1 deletion tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import numpy as np
from pydeepflow.metrics import (
precision_score, recall_score, f1_score, confusion_matrix,
mean_absolute_error, mean_squared_error, r2_score
mean_absolute_error, mean_squared_error, r2_score, rmse
)

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

def test_rmse(self):
y_true = np.array([1, 2, 3])
y_pred = np.array([2, 2, 4])
expected = np.sqrt(((1-2)**2 + (2-2)**2 + (3-4)**2) / 3)
self.assertAlmostEqual(rmse(y_true, y_pred), expected)

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