File size: 2,817 Bytes
a244ac5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import os 
import sys
import pandas as pd
import  numpy as np
import dill

from dataclasses import dataclass
from src.exception import CustomException
from src.logger import logging

from sklearn.metrics import r2_score
from sklearn.model_selection import GridSearchCV


def save_object(file_path: str, obj: object):
    """
    Saves a Python object to a file using pickle.

    Parameters:
    file_path (str): The path where the object should be saved.
    obj (object): The Python object to be saved.
    """
    try:
        os.makedirs(os.path.dirname(file_path), exist_ok=True)
        with open(file_path, 'wb') as file_obj:
            dill.dump(obj, file_obj)
        logging.info(f"Object saved successfully at {file_path}")
    except Exception as e:
        logging.error("Error saving object: {0}".format(e))
        raise CustomException(e, sys)
    

def evaluate_models(X_train, y_train, X_test, y_test, models , param_grids) -> dict:
   
    """
    Evaluates multiple regression models and returns their R2 scores.

    Parameters:
    X_train (array-like): Training features.
    y_train (array-like): Training target.
    X_test (array-like): Testing features.
    y_test (array-like): Testing target.
    models (dict): A dictionary where keys are model names and values are model instances.

    Returns:
    dict: A dictionary with model names as keys and their R2 scores as values.
    """
    
    try:
        model_report = {}

        for model_name, model in models.items():
            param_grid = param_grids.get(model_name, {})
            gs = GridSearchCV(model, param_grid, cv=5, n_jobs=-1, verbose=0)
            gs.fit(X_train, y_train)
            model.set_params(**gs.best_params_)
            logging.info(f"Best parameters for {model_name}: {gs.best_params_}")

            model.fit(X_train, y_train)
        
            y_pred_test  = model.predict(X_test)

            test_r2_score = r2_score(y_test, y_pred_test)
            model_report[model_name] = test_r2_score
            logging.info(f"{model_name} R2 Score: {test_r2_score}")

        return model_report
    except Exception as e:
        logging.error("Error evaluating models: {0}".format(e))
        raise CustomException(e, sys)
    
def load_object(file_path: str) -> object:
    """
    Loads a Python object from a file using pickle.

    Parameters:
    file_path (str): The path to the file from which the object should be loaded.

    Returns:
    object: The loaded Python object.
    """
    try:
        with open(file_path, 'rb') as file_obj:
            obj = dill.load(file_obj)
        logging.info(f"Object loaded successfully from {file_path}")
        return obj
    except Exception as e:
        logging.error("Error loading object: {0}".format(e))
        raise CustomException(e, sys)