Spaces:
Runtime error
Runtime error
File size: 1,866 Bytes
6defa3d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
import logging
from abc import ABC, abstractmethod
import numpy as np
from sklearn.metrics import mean_squared_error, r2_score
class Evaluation(ABC):
"""
Abstract class for all evaluations strategy
"""
@abstractmethod
def calculate_scores(self, y_true: np.ndarray, y_pred: np.ndarray):
"""
Calculates the scores for the model
Args:
y_true: True labels
y_pred: Predicted labels
Returns:
None
"""
pass
class MSE(Evaluation):
"""
Mean Squared Error evaluation
"""
def calculate_scores(self, y_true: np.ndarray, y_pred: np.ndarray):
try:
logging.info("Calculating MSE")
mse = mean_squared_error(y_true, y_pred)
logging.info(f"MSE: {mse}")
return mse
except Exception as e:
logging.error(f"Error in training model: {e}")
raise e
class R2(Evaluation):
"""
R2 score evaluation
"""
def calculate_scores(self, y_true: np.ndarray, y_pred: np.ndarray):
try:
logging.info("Calculating R2 score")
r2 = r2_score(y_true, y_pred)
logging.info(f"R2 score: {r2}")
return r2
except Exception as e:
logging.error(f"Error in training model: {e}")
raise e
class RMSE(Evaluation):
"""
Root Mean Squared Error evaluation
"""
def calculate_scores(self, y_true: np.ndarray, y_pred: np.ndarray):
try:
logging.info("Calculating RMSE")
rmse = mean_squared_error(y_true, y_pred, squared=False)
logging.info(f"RMSE: {rmse}")
return rmse
except Exception as e:
logging.error(f"Error in training model: {e}")
raise e |