Model Trainer Issue on End-to-End ML Project - TypeError: __init__() got an unexpected keyword argument 'config'

79 views Asked by At

I am following the process shown on Wine Quality Prediction End-to-End ML Project on Krish Naik's YouTube channel to do a Flight Fare Prediction Project.

I run this cell of model trainer pipeline on 04_model_trainer.ipynb:

try:
    config = ConfigurationManager()
    model_trainer_config = config.get_model_trainer_config()
    model_trainer_config = ModelTrainer(config=model_trainer_config)
    model_trainer_config.train()
except Exception as e:
    raise e

I get this error:

TypeError: __init__() got an unexpected keyword argument 'config'

Here is the full traceback:

[2023-12-06 09:32:22,134: INFO: common: yaml file: config\config.yaml loaded successfully]
[2023-12-06 09:32:22,141: INFO: common: yaml file: params.yaml loaded successfully]
[2023-12-06 09:32:22,150: INFO: common: yaml file: schema.yaml loaded successfully]
[2023-12-06 09:32:22,153: INFO: common: created directory at: artifacts]
[2023-12-06 09:32:22,157: INFO: common: created directory at: artifacts/model_trainer]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
g:\Machine_Learning_Projects\iNeuron internship\Flight-Fare-Prediction-End-to-End-ML-Project\research\04_model_trainer.ipynb Cell 11 line 7
      <a href='vscode-notebook-cell:/g%3A/Machine_Learning_Projects/iNeuron%20internship/Flight-Fare-Prediction-End-to-End-ML-Project/research/04_model_trainer.ipynb#X12sZmlsZQ%3D%3D?line=4'>5</a>     model_trainer_config.train()
      <a href='vscode-notebook-cell:/g%3A/Machine_Learning_Projects/iNeuron%20internship/Flight-Fare-Prediction-End-to-End-ML-Project/research/04_model_trainer.ipynb#X12sZmlsZQ%3D%3D?line=5'>6</a> except Exception as e:
----> <a href='vscode-notebook-cell:/g%3A/Machine_Learning_Projects/iNeuron%20internship/Flight-Fare-Prediction-End-to-End-ML-Project/research/04_model_trainer.ipynb#X12sZmlsZQ%3D%3D?line=6'>7</a>     raise e

g:\Machine_Learning_Projects\iNeuron internship\Flight-Fare-Prediction-End-to-End-ML-Project\research\04_model_trainer.ipynb Cell 11 line 4
      <a href='vscode-notebook-cell:/g%3A/Machine_Learning_Projects/iNeuron%20internship/Flight-Fare-Prediction-End-to-End-ML-Project/research/04_model_trainer.ipynb#X12sZmlsZQ%3D%3D?line=1'>2</a>     config = ConfigurationManager()
      <a href='vscode-notebook-cell:/g%3A/Machine_Learning_Projects/iNeuron%20internship/Flight-Fare-Prediction-End-to-End-ML-Project/research/04_model_trainer.ipynb#X12sZmlsZQ%3D%3D?line=2'>3</a>     model_trainer_config = config.get_model_trainer_config()
----> <a href='vscode-notebook-cell:/g%3A/Machine_Learning_Projects/iNeuron%20internship/Flight-Fare-Prediction-End-to-End-ML-Project/research/04_model_trainer.ipynb#X12sZmlsZQ%3D%3D?line=3'>4</a>     model_trainer_config = ModelTrainer(config=model_trainer_config)
      <a href='vscode-notebook-cell:/g%3A/Machine_Learning_Projects/iNeuron%20internship/Flight-Fare-Prediction-End-to-End-ML-Project/research/04_model_trainer.ipynb#X12sZmlsZQ%3D%3D?line=4'>5</a>     model_trainer_config.train()
      <a href='vscode-notebook-cell:/g%3A/Machine_Learning_Projects/iNeuron%20internship/Flight-Fare-Prediction-End-to-End-ML-Project/research/04_model_trainer.ipynb#X12sZmlsZQ%3D%3D?line=5'>6</a> except Exception as e:

TypeError: __init__() got an unexpected keyword argument 'config'

Here is the code of ModelTrainer class:

class ModelTrainer:

    def __init__(self, model_trainer_config):
    # def __init__(self):
        self.model_trainer_config = ModelTrainerConfig()    


    def save_obj(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:
                joblib.dump(obj, file_obj, compress= ('gzip'))

        except Exception as e:
            logger.info('Error occured in utils save_obj')
            raise e
        

    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 Testing data
                y_test_pred = model.predict(X_test)

                # Get R2 scores for train and test data
                test_model_score = r2_score(y_test,y_test_pred)

                report[list(models.keys())[i]] =  test_model_score

            return report

        except Exception as e:
            logger.info('Exception occured during model training')
            raise e    



    def initiate_model_training(self, X_train, X_test, y_train, y_test):
        try:
            logger.info('Splitting ')

            models={
            'LinearRegression':LinearRegression(),
            'Lasso':Lasso(),
            'Ridge':Ridge(),
            'Elasticnet':ElasticNet(),
            'RandomForestRegressor': RandomForestRegressor(),
            'GradientBoostRegressor()' : GradientBoostingRegressor(),
            "AdaBoost" : AdaBoostRegressor(),
            'DecisionTreeRegressor' : DecisionTreeRegressor(),
            "SupportVectorRegressor" : SVR(),
            "KNN" : KNeighborsRegressor()
            }

            model_report:dict = ModelTrainer.evaluate_model(X_train,y_train, X_test, y_test, models)
            print(model_report)
            print("\n====================================================================================")
            logger.info(f'Model Report : {model_report}')

            # to get best model score from dictionary
            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]

            print(f"Best Model Found, Model Name :{best_model_name}, R2-score: {best_model_score}")
            print("\n====================================================================================")
            logger.info(f"Best Model Found, Model name: {best_model_name}, R2-score: {best_model_score}")
            logger.info(f"{best_model.feature_names_in_}")
            
            ModelTrainer.save_obj(
            file_path = self.model_trainer_config.trained_model_file_path,
            obj = best_model
            )

        except Exception as e:
            logger.info('Exception occured at model trianing')
            raise e
        

Here is my file in GitHub.

My file encoding is UTF-8

Would you please help me to fix this issue?

0

There are 0 answers