-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
55 lines (41 loc) · 1.52 KB
/
utils.py
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
import os
import sys
import pickle
import numpy as np
import pandas as pd
from exception import CustomException
from logger import logging
from sklearn.metrics import r2_score, mean_squared_error, mean_absolute_error
def save_object(file_path, obj):
try:
dir_path = os.path.dirname(file_path)
os.makedirs(dir_path, exist_ok=True)
with open(file_path, 'wb') as file_obj:
pickle.dump(obj, file_obj)
except Exception as e:
logging.info('Exception occured while saving pickle file')
raise CustomException(e, sys)
def evaluate_model(X_train, y_train, X_test, y_test, models):
try:
report = {}
for i in range(len(models)):
model=list(models.values())[i]
# Train Model
model.fit(X_train, y_train)
# Predict on Test Data
y_test_pred = model.predict(X_test)
# Get R2 scores for train & test data
# train_model_score = r2_score(y_train, y_train_pred)
test_model_score = r2_score(y_test, y_test_pred)
report[list(models.keys())[i]] = test_model_score
return report
except Exception as e:
logging.info('Exception occured while evaluating model')
raise CustomException(e, sys)
def load_object(file_path):
try:
with open(file_path, 'rb') as file_obj:
return pickle.load(file_obj)
except Exception as e:
logging.info('Exception occured while loading pickle file')
raise CustomException(e, sys)