-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
469 additions
and
464 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,112 +1,89 @@ | ||
import logging | ||
from abc import ABC, abstractmethod | ||
|
||
import numpy as np | ||
from sklearn.metrics import mean_squared_error, r2_score | ||
|
||
|
||
class Evaluation: | ||
class Evaluation(ABC): | ||
""" | ||
Evaluation class which evaluates the model performance using the sklearn metrics. | ||
Abstract Class defining the strategy for evaluating model performance | ||
""" | ||
|
||
def __init__(self) -> None: | ||
"""Initializes the Evaluation class.""" | ||
@abstractmethod | ||
def calculate_score(self, y_true: np.ndarray, y_pred: np.ndarray) -> float: | ||
pass | ||
|
||
def mean_squared_error( | ||
self, y_true: np.ndarray, y_pred: np.ndarray | ||
) -> float: | ||
|
||
class MSE(Evaluation): | ||
""" | ||
Evaluation strategy that uses Mean Squared Error (MSE) | ||
""" | ||
def calculate_score(self, y_true: np.ndarray, y_pred: np.ndarray) -> float: | ||
""" | ||
Mean Squared Error (MSE) is the mean of the squared errors. | ||
Args: | ||
y_true: np.ndarray | ||
y_pred: np.ndarray | ||
Returns: | ||
mse: float | ||
""" | ||
try: | ||
logging.info( | ||
"Entered the mean_squared_error method of the Evaluation class", | ||
) | ||
logging.info("Entered the calculate_score method of the MSE class") | ||
mse = mean_squared_error(y_true, y_pred) | ||
logging.info( | ||
"The mean squared error value is: " + str(mse), | ||
) | ||
|
||
logging.info("The mean squared error value is: " + str(mse)) | ||
return mse | ||
except Exception as e: | ||
logging.info( | ||
"Exception occurred in mean_squared_error method of the Evaluation class. Exception message: " | ||
+ str(e), | ||
) | ||
logging.info( | ||
"Exited the mean_squared_error method of the Evaluation class", | ||
logging.error( | ||
"Exception occurred in calculate_score method of the MSE class. Exception message: " | ||
+ str(e) | ||
) | ||
raise Exception() | ||
raise e | ||
|
||
def r2_score(self, y_true: np.ndarray, y_pred: np.ndarray) -> float: | ||
""" | ||
R2 Score (R2) is a statistical measure of how close the observed values | ||
are to the predicted values. It is also known as the coefficient of | ||
determination. | ||
|
||
class R2Score(Evaluation): | ||
""" | ||
Evaluation strategy that uses R2 Score | ||
""" | ||
def calculate_score(self, y_true: np.ndarray, y_pred: np.ndarray) -> float: | ||
""" | ||
Args: | ||
y_true: np.ndarray | ||
y_pred: np.ndarray | ||
Returns: | ||
r2_score: float | ||
""" | ||
try: | ||
logging.info( | ||
"Entered the r2_score method of the Evaluation class", | ||
) | ||
logging.info("Entered the calculate_score method of the R2Score class") | ||
r2 = r2_score(y_true, y_pred) | ||
logging.info( | ||
"The r2 score value is: " + str(r2), | ||
) | ||
logging.info( | ||
"Exited the r2_score method of the Evaluation class", | ||
) | ||
logging.info("The r2 score value is: " + str(r2)) | ||
return r2 | ||
except Exception as e: | ||
logging.info( | ||
"Exception occurred in r2_score method of the Evaluation class. Exception message: " | ||
+ str(e), | ||
logging.error( | ||
"Exception occurred in calculate_score method of the R2Score class. Exception message: " | ||
+ str(e) | ||
) | ||
logging.info( | ||
"Exited the r2_score method of the Evaluation class", | ||
) | ||
raise Exception() | ||
raise e | ||
|
||
def root_mean_squared_error( | ||
self, y_true: np.ndarray, y_pred: np.ndarray | ||
) -> float: | ||
|
||
class RMSE(Evaluation): | ||
""" | ||
Evaluation strategy that uses Root Mean Squared Error (RMSE) | ||
""" | ||
def calculate_score(self, y_true: np.ndarray, y_pred: np.ndarray) -> float: | ||
""" | ||
Root Mean Squared Error (RMSE) is the square root of the mean of the | ||
squared errors. | ||
Args: | ||
y_true: np.ndarray | ||
y_pred: np.ndarray | ||
Return: | ||
Returns: | ||
rmse: float | ||
""" | ||
try: | ||
logging.info( | ||
"Entered the root_mean_squared_error method of the Evaluation class", | ||
) | ||
logging.info("Entered the calculate_score method of the RMSE class") | ||
rmse = np.sqrt(mean_squared_error(y_true, y_pred)) | ||
logging.info( | ||
"The root mean squared error value is: " + str(rmse), | ||
) | ||
logging.info("The root mean squared error value is: " + str(rmse)) | ||
return rmse | ||
except Exception as e: | ||
logging.info( | ||
"Exception occurred in root_mean_squared_error method of the Evaluation class. Exception message: " | ||
+ str(e), | ||
) | ||
logging.info( | ||
"Exited the root_mean_squared_error method of the Evaluation class", | ||
logging.error( | ||
"Exception occurred in calculate_score method of the RMSE class. Exception message: " | ||
+ str(e) | ||
) | ||
raise Exception() | ||
raise e |
Oops, something went wrong.