my_project.evaluation
1import numpy as np 2import torch 3 4from my_project.dataset import AutomobileDataset 5from my_project.train import create_model 6from pathlib import Path 7import matplotlib.pyplot as plt 8 9PROJECT_ROOT = Path(__file__).resolve().parent.parent 10 11def evaluate_model() -> tuple[np.ndarray, np.ndarray, dict]: 12 """ 13 Executes the evaluation of the model. 14 15 Returns 16 ------- 17 targets : np.ndarray 18 True targets from the test dataset 19 predictions : np.ndarray 20 Predicted targets on the test dataset 21 metrics : dict 22 Various metrics (MAE, RMAE, R^2) obtained from the test dataset 23 """ 24 25 dataset = AutomobileDataset( 26 PROJECT_ROOT / "data" / "processed" / "automobile_test.parquet" 27 ) 28 model = create_model( 29 dataset.X.shape[1] 30 ) 31 32 model.load_state_dict( 33 torch.load( 34 PROJECT_ROOT / "models" / "automobile_model.pth", 35 map_location="cpu" 36 ) 37 ) 38 39 model.eval() 40 41 predictions = [] 42 targets = [] 43 44 with torch.no_grad(): 45 46 for X, y in dataset: 47 48 prediction = model( 49 X.unsqueeze(0) 50 ).item() 51 52 predictions.append(prediction) 53 targets.append(y.item()) 54 55 predictions = np.array(predictions) 56 targets = np.array(targets) 57 58 errors = predictions - targets 59 60 metrics = { 61 "mae": np.mean(np.abs(errors)), 62 "rmse": np.sqrt(np.mean(errors ** 2)), 63 "r2": 1 - ( 64 np.sum(errors ** 2) 65 / np.sum((targets - targets.mean()) ** 2) 66 ) 67 } 68 69 return targets, predictions, metrics
PROJECT_ROOT =
PosixPath('/home/alumno/Desktop/git_repositories/SDOML-project')
def
evaluate_model() -> tuple[numpy.ndarray, numpy.ndarray, dict]:
12def evaluate_model() -> tuple[np.ndarray, np.ndarray, dict]: 13 """ 14 Executes the evaluation of the model. 15 16 Returns 17 ------- 18 targets : np.ndarray 19 True targets from the test dataset 20 predictions : np.ndarray 21 Predicted targets on the test dataset 22 metrics : dict 23 Various metrics (MAE, RMAE, R^2) obtained from the test dataset 24 """ 25 26 dataset = AutomobileDataset( 27 PROJECT_ROOT / "data" / "processed" / "automobile_test.parquet" 28 ) 29 model = create_model( 30 dataset.X.shape[1] 31 ) 32 33 model.load_state_dict( 34 torch.load( 35 PROJECT_ROOT / "models" / "automobile_model.pth", 36 map_location="cpu" 37 ) 38 ) 39 40 model.eval() 41 42 predictions = [] 43 targets = [] 44 45 with torch.no_grad(): 46 47 for X, y in dataset: 48 49 prediction = model( 50 X.unsqueeze(0) 51 ).item() 52 53 predictions.append(prediction) 54 targets.append(y.item()) 55 56 predictions = np.array(predictions) 57 targets = np.array(targets) 58 59 errors = predictions - targets 60 61 metrics = { 62 "mae": np.mean(np.abs(errors)), 63 "rmse": np.sqrt(np.mean(errors ** 2)), 64 "r2": 1 - ( 65 np.sum(errors ** 2) 66 / np.sum((targets - targets.mean()) ** 2) 67 ) 68 } 69 70 return targets, predictions, metrics
Executes the evaluation of the model.
Returns
- targets (np.ndarray): True targets from the test dataset
- predictions (np.ndarray): Predicted targets on the test dataset
- metrics (dict): Various metrics (MAE, RMAE, R^2) obtained from the test dataset