Spaces:
Sleeping
Sleeping
import os | |
import sys | |
from dataclasses import dataclass | |
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score | |
from sklearn.linear_model import LinearRegression,Ridge,Lasso | |
from sklearn.tree import DecisionTreeRegressor | |
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor,AdaBoostRegressor | |
from xgboost import XGBRegressor | |
from sklearn.neighbors import KNeighborsRegressor | |
from src.exception import CustomException | |
from src.logger import logging | |
from src.utils import save_object,evaluate_models | |
class ModelTrainerConfig: | |
trained_model_file_path = os.path.join('artifacts','model.pkl') | |
class ModelTrainer: | |
def __init__(self) -> None: | |
self.model_trainer_config = ModelTrainerConfig() | |
def initiate_model_trainer(self,train_array, test_array): | |
try: | |
logging.info('spliting training and test input data') | |
X_train, y_train, X_test, y_test= ( | |
train_array[:,:-1], | |
train_array[:,-1], | |
test_array[:,:-1], | |
test_array[:,-1] | |
) | |
models = { | |
"LinearRegression":LinearRegression(), | |
"Ridge":Ridge(), | |
"Lasso":Lasso(), | |
"KNeighborsRegressor":KNeighborsRegressor(), | |
"DecisionTreeRegressor":DecisionTreeRegressor(), | |
"AdaBoostRegressor":AdaBoostRegressor(), | |
"RandomForestRegressor":RandomForestRegressor() | |
# "CatBoostRegressor":CatBoostRegressor(verbose=False), | |
} | |
logging.info('training models') | |
model_report:dict=evaluate_models(X_train=X_train,y_train=y_train,X_test=X_test,y_test=y_test,models=models) | |
logging.info("model trained") | |
best_model_score = max(sorted(model_report.values())) | |
best_model_name = list(model_report.keys())[ | |
list(model_report.values()).index(best_model_score) | |
] | |
best_model = models[best_model_name] | |
if best_model_score < 0.6: | |
raise CustomException("NO best model found") | |
logging.info("best model found") | |
save_object( | |
file_path=self.model_trainer_config.trained_model_file_path, | |
obj=best_model | |
) | |
predicted = best_model.predict(X_test) | |
r2 = r2_score(y_test,predicted) | |
return r2 | |
except Exception as e: | |
raise CustomException(e,sys) |