From 491cb3ebd654a99624f28cfc01ff9dd72ead6915 Mon Sep 17 00:00:00 2001 From: omkarcharatkar <132135901+omkarcharatkar@users.noreply.github.com> Date: Sat, 6 May 2023 16:44:15 +0530 Subject: [PATCH] Update loss_func.py added Root Mean Squared error function. --- MLlib/loss_func.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/MLlib/loss_func.py b/MLlib/loss_func.py index f182f09..e89ed3c 100644 --- a/MLlib/loss_func.py +++ b/MLlib/loss_func.py @@ -425,3 +425,59 @@ def loss(X, Y, W): y_pred = np.dot(X, W).T L = np.sum(np.true_divide((np.abs(Y - y_pred) * 100), Y)) / X.shape[0] return L + + + + +class RootMeanSquaredError(): + """ + Calculate Root Mean Squared Error. + """ + + @staticmethod + def loss(X, Y, W): + """ + Calculate loss by Root Mean Squared Error method. + + PARAMETERS + ========== + + X:ndarray(dtype=float,ndim=1) + input vector + Y:ndarray(dtype=float) + output vector + W:ndarray(dtype=float) + Weights + + RETURNS + ======= + + array of Root Mean Squared losses + """ + M = X.shape[0] + return np.sqrt(np.sum(np.square(np.dot(X, W) - Y)) / M) + + @staticmethod + def derivative(X, Y, W): + """ + Calculate derivative for Root Mean Squared error method. + + PARAMETERS + ========== + + X:ndarray(dtype=float,ndim=1) + input vector + Y:ndarray(dtype=float) + output vector + W:ndarray(dtype=float) + Weights + + RETURNS + ======= + + array of derivates + """ + M = X.shape[0] + return np.dot(X.T, np.dot(X, W) - Y) / M + +