{"id":2296,"date":"2025-05-10T10:51:57","date_gmt":"2025-05-10T10:51:57","guid":{"rendered":"https:\/\/techtrendfeed.com\/?p=2296"},"modified":"2025-05-10T10:51:57","modified_gmt":"2025-05-10T10:51:57","slug":"machine-predictive-upkeep-with-mlops","status":"publish","type":"post","link":"https:\/\/techtrendfeed.com\/?p=2296","title":{"rendered":"Machine Predictive Upkeep with MLOps"},"content":{"rendered":"<p> <br \/>\n<\/p>\n<div id=\"article-start\">\n<p>Machines don\u2019t break down out of nowhere\u2014there are at all times indicators. The issue? People aren\u2019t at all times nice at noticing them. That\u2019s the place Machine Predictive Upkeep is available in! This information will take you thru the thrilling world of Machine Predictive Upkeep, utilizing AWS and <a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/www.analyticsvidhya.com\/blog\/2022\/03\/a-comprehensive-guide-on-mlops-for-machine-learning-engineering\/\" target=\"_blank\" rel=\"noreferrer noopener\">MLOps <\/a>to make sure your gear stays predictably dependable.<\/p>\n<h3 class=\"wp-block-heading\" id=\"h-learning-objectives\">Studying Aims<\/h3>\n<ul class=\"wp-block-list\">\n<li>Perceive find out how to design and implement an end-to-end MLOps pipeline for predictive upkeep, overlaying information ingestion, mannequin coaching, and deployment.<\/li>\n<li>Study to combine important instruments like Docker, FastAPI, and AWS companies to construct a sturdy, production-ready machine studying utility.<\/li>\n<li>Discover using GitHub Actions for automating<a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/www.analyticsvidhya.com\/blog\/2022\/08\/setting-up-ci-cd-using-github-actions\/#:~:text=Various%20Stages%20of%20the%20CI,application%20in%20our%20GitHub%20repository.\" target=\"_blank\" rel=\"noreferrer noopener\"> CI\/CD workflows<\/a>, guaranteeing clean and dependable code integration and deployment.<\/li>\n<li>Arrange greatest practices for monitoring, efficiency monitoring, and steady enchancment to maintain your machine studying fashions environment friendly and maintainable.<\/li>\n<\/ul>\n<p><em><strong>This text was printed as part of the\u00a0<\/strong><\/em><a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/www.analyticsvidhya.com\/datahack\/blogathon\" target=\"_blank\" rel=\"noreferrer noopener\"><em><strong>Information Science Blogathon.<\/strong><\/em><\/a><\/p>\n<h2 class=\"wp-block-heading\" id=\"h-problem-unplanned-downtime-amp-maintenance-costs\">Downside: Unplanned Downtime &amp; Upkeep Prices<\/h2>\n<p>Sudden gear failures in industrial settings trigger downtime and monetary losses. In our mission, we&#8217;re utilizing MLOps greatest practices and machine studying to detect points early, enabling well timed repairs and decreasing disruptions.<\/p>\n<p>Earlier than diving into implementation, let\u2019s take a better take a look at the mission structure.<\/p>\n<figure class=\"wp-block-image size-full is-resized figure  mt-2 mb-2 d-table mx-auto\"><img fetchpriority=\"high\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/machine_Predictive_Maintenance.webp\" alt=\"machine_Predictive_Maintenance\" class=\"wp-image-221099\" style=\"width:585px;height:auto\" srcset=\"https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/machine_Predictive_Maintenance.webp 960w, https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/machine_Predictive_Maintenance-300x169.webp 300w, https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/machine_Predictive_Maintenance-768x432.webp 768w, https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/machine_Predictive_Maintenance-150x84.webp 150w\" sizes=\"(max-width: 960px) 100vw, 960px\"\/><\/figure>\n<h2 class=\"wp-block-heading\" id=\"h-necessary-prerequisites\">Essential Conditions<\/h2>\n<p>Beneath we&#8217;ll first look within the conditions required:<\/p>\n<p><strong>Clone the repository<\/strong>:<\/p>\n<pre class=\"wp-block-code\"><code>git clone \"https:\/\/github.com\/karthikponna\/Predictive_Maintenance_MLOps.git\"\ncd Predictive_Maintenance_MLOps<\/code><\/pre>\n<p><strong>Create and activate the digital setting<\/strong>:<\/p>\n<pre class=\"wp-block-code\"><code># For macOS and Linux:\npython3 -m venv venv\nsupply venv\/bin\/activate\n\n# For Home windows:\npython -m venv venv\n.venvScriptsactivate<\/code><\/pre>\n<p><strong>Set up Required Dependencies<\/strong>:<\/p>\n<pre class=\"wp-block-code\"><code>pip set up -r necessities.txt<\/code><\/pre>\n<p><strong>Set Up Surroundings Variables:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code># Create a `.env` file and add your MongoDB connection string:\nMONGO_URI=your_mongodb_connection_string<\/code><\/pre>\n<h2 class=\"wp-block-heading\" id=\"h-project-structure\">Undertaking Construction<\/h2>\n<p>The Undertaking Construction outlines the important thing parts and group of the mission, guaranteeing readability and maintainability. It helps in understanding how completely different modules work together and the way the general system is designed. A well-defined construction simplifies growth, debugging, and scalability.<\/p>\n<pre class=\"wp-block-code\"><code>project_root\/\n\u2502\n\u251c\u2500\u2500 .github\/\n\u2502   \u2514\u2500\u2500 workflows\/\n\u2502       \u2514\u2500\u2500 predominant.yml\n\u2502\n\u251c\u2500\u2500 data_schema\/\n\u2502   \u2514\u2500\u2500 schema.yaml\n\u2502\n\u251c\u2500\u2500 final_model\/\n\u2502   \u251c\u2500\u2500 mannequin.pkl\n\u2502   \u2514\u2500\u2500 preprocessor.pkl\n\u2502\n\u251c\u2500\u2500 Machine_Predictive_Data\/\n\u2502   \u2514\u2500\u2500 predictive_maintenance.csv\n\u2502\n\u251c\u2500\u2500 machine_predictive_maintenance\/\n\u2502   \u251c\u2500\u2500 cloud\/\n\u2502   \u251c\u2500\u2500 parts\/\n\u2502   \u251c\u2500\u2500 fixed\/\n\u2502   \u251c\u2500\u2500 entity\/\n\u2502   \u251c\u2500\u2500 exception\/\n\u2502   \u251c\u2500\u2500 logging\/\n\u2502   \u251c\u2500\u2500 pipeline\/\n\u2502   \u251c\u2500\u2500 utils\/\n\u2502   \u2514\u2500\u2500 __init__.py\n\u2502\n\u251c\u2500\u2500 my_venv\/\n\u2502\n\u251c\u2500\u2500 notebooks\/\n\u2502   \u251c\u2500\u2500 EDA.ipynb\n\u2502   \u251c\u2500\u2500 prediction_output\/\n\u2502\n\u251c\u2500\u2500 templates\/\n\u2502   \u2514\u2500\u2500 desk.html\n\u2502\n\u251c\u2500\u2500 valid_data\/\n|    \u2514\u2500\u2500check.csv\n\u2502\n\u251c\u2500\u2500 .env\n\u251c\u2500\u2500 .gitignore\n\u251c\u2500\u2500 app.py\n\u251c\u2500\u2500 Dockerfile\n\u251c\u2500\u2500 predominant.py\n\u251c\u2500\u2500 push_data.py\n\u251c\u2500\u2500 README.md\n\u251c\u2500\u2500 necessities.txt\n\u251c\u2500\u2500 setup.py\n\u251c\u2500\u2500 test_mongodb.py<\/code><\/pre>\n<h2 class=\"wp-block-heading\" id=\"h-data-ingestion\">Information Ingestion<\/h2>\n<p>On this mission, we use a machine predictive upkeep CSV file, changing it into JSON information, and inserting it right into a MongoDB assortment.\u00a0<\/p>\n<p>Dataset Hyperlink: <a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/www.kaggle.com\/datasets\/shivamb\/machine-predictive-maintenance-classification\" target=\"_blank\" rel=\"nofollow noopener\">https:\/\/www.kaggle.com\/datasets\/shivamb\/machine-predictive-maintenance-classification<\/a><\/p>\n<p>Right here is the code snippet to transform CSV -&gt; JSON information -&gt; MongoDB<\/p>\n<pre class=\"wp-block-code\"><code>class PredictiveDataExtract():\n\n    def __init__(self):\n        \"\"\"\n        Initializes the PredictiveDataExtract class.\n        \"\"\"\n        attempt:\n            cross\n        besides Exception as e:\n            increase MachinePredictiveMaintenanceException(e, sys)\n        \n    def csv_to_json_convertor(self, file_path):\n        attempt:\n            information = pd.read_csv(file_path)\n            information.reset_index(drop=True, inplace=True)\n            information = listing(json.hundreds(information.T.to_json()).values())\n            return information\n        besides Exception as e:\n            increase MachinePredictiveMaintenanceException(e, sys)\n        \n    def insert_data_mongodb(self, information, database, assortment):\n        attempt:\n            self.information = information\n            self.database = database\n            self.assortment = assortment\n\n            self.mongo_client = pymongo.MongoClient(MONGO_DB_URL)\n            self.database = self.mongo_client[self.database]\n            \n            self.assortment = self.database[self.collection]\n            self.assortment.insert_many(self.information)\n            return(len(self.information))\n        besides Exception as e:\n            increase MachinePredictiveMaintenanceException(e, sys)\n        \nif __name__==\"__main__\":\n    FILE_PATH=\"Machine_Predictive_Data\/predictive_maintenance.csv\"\n    DATABASE=\"Predictive_Maintenance_MLOps\"\n    assortment = \"Machine_Predictive_Data\"\n\n    predictive_data_obj=PredictiveDataExtract()\n    information = predictive_data_obj.csv_to_json_convertor(FILE_PATH)\n    no_of_records = predictive_data_obj.insert_data_mongodb(information, DATABASE, assortment)\n    print(no_of_records)<\/code><\/pre>\n<p>Right here is the code snippet to fetch information from MongoDB, cut up the info into practice and check CSV information, and retailer them as a Information Ingestion artifact.<\/p>\n<pre class=\"wp-block-code\"><code>class DataIngestion:\n    def __init__(self, data_ingestion_config:DataIngestionConfig):\n\n        \"\"\"\n        Initializes the DataIngestion class with the supplied configuration.\n\n        Parameters:\n            data_ingestion_config: DataIngestionConfig\n                Configuration object containing particulars for information ingestion.\n        \"\"\"\n        attempt:\n            self.data_ingestion_config=data_ingestion_config\n        besides Exception as e:\n            increase MachinePredictiveMaintenanceException(e, sys)\n        \n    def export_collection_as_dataframe(self):\n        attempt:\n            database_name= self.data_ingestion_config.database_name\n            collection_name= self.data_ingestion_config.collection_name\n            self.mongo_client = pymongo.MongoClient(MONGO_DB_URL)\n            assortment = self.mongo_client[database_name][collection_name]\n            df = pd.DataFrame(listing(assortment.discover()))\n            if \"_id\" in df.columns.to_list():\n                df = df.drop(columns=[\"_id\"], axis=1)\n            df.substitute({\"na\":np.nan},inplace=True)\n            return df\n        besides Exception as e:\n            increase MachinePredictiveMaintenanceException(e, sys)\n\n    def export_data_into_feature_store(self,dataframe: pd.DataFrame):\n        attempt:\n            feature_store_file_path=self.data_ingestion_config.feature_store_file_path\n            #creating folder\n            dir_path = os.path.dirname(feature_store_file_path)\n            os.makedirs(dir_path,exist_ok=True)\n            dataframe.to_csv(feature_store_file_path,index=False,header=True)\n            return dataframe\n        besides Exception as e:\n            increase MachinePredictiveMaintenanceException(e,sys)\n\n    def split_data_as_train_test(self, dataframe:pd.DataFrame):        \n        attempt:\n            train_set, test_set = train_test_split(\n                dataframe, test_size=self.data_ingestion_config.train_test_split_ratio\n            )\n            logging.data(\"Carried out practice check cut up on the dataframe\")\n            logging.data(\n                \"Exited split_data_as_train_test methodology of Data_Ingestion class\"\n            )\n            dir_path = os.path.dirname(self.data_ingestion_config.training_file_path)\n            os.makedirs(dir_path, exist_ok=True)\n            logging.data(f\"Exporting practice and check file path.\")\n            train_set.to_csv(\n                self.data_ingestion_config.training_file_path, index=False, header=True\n            )\n            test_set.to_csv(\n                self.data_ingestion_config.testing_file_path, index=False, header=True\n                            )\n            logging.data(f\"Exported practice and check file path.\"  )\n        besides Exception as e:\n            increase MachinePredictiveMaintenanceException(e, sys)\n\n    def initiate_data_ingestion(self):\n        attempt:\n            dataframe = self.export_collection_as_dataframe()\n            dataframe = self.export_data_into_feature_store(dataframe)\n            self.split_data_as_train_test(dataframe)\n            dataingestionartifact= DataIngestionArtifact(trained_file_path=self.data_ingestion_config.training_file_path,\n                                                         test_file_path=self.data_ingestion_config.testing_file_path)\n            return dataingestionartifact\n        besides Exception as e:\n            increase MachinePredictiveMaintenanceException(e,sys)<\/code><\/pre>\n<h2 class=\"wp-block-heading\" id=\"h-data-validation\">Information Validation<\/h2>\n<p>On this step, we\u2019ll verify if the ingested information meets the anticipated format by guaranteeing all required columns are current utilizing a predefined schema and evaluating the coaching and testing information for any variations. Then we save the clear information and create a drift report, so solely high quality information is used for the subsequent step, which is remodeling the info.\u00a0<\/p>\n<p>Information Validation code snippet:<\/p>\n<pre class=\"wp-block-code\"><code>class DataValidation:\n\n    def __init__(self, data_ingestion_artifact:DataIngestionArtifact,\n                 data_validation_config: DataValidationConfig):\n        attempt:\n            self.data_ingestion_artifact= data_ingestion_artifact\n            self.data_validation_config= data_validation_config\n            self._schema_config = read_yaml_file(SCHEMA_FILE_PATH)\n        besides Exception as e:\n            increase MachinePredictiveMaintenanceException(e, sys)\n        \n    @staticmethod\n    def read_data(file_path) -&gt; pd.DataFrame:\n        attempt:\n            return pd.read_csv(file_path)\n        besides Exception as e:\n            increase MachinePredictiveMaintenanceException(e, sys) \n\n    \n    def validate_number_of_columns(self, dataframe:pd.DataFrame)-&gt; bool:\n        attempt:\n            number_of_columns = len(self._schema_config)\n            logging.data(f\"Required variety of columns:{number_of_columns}\")\n            logging.data(f\"Information body has columns:{len(dataframe.columns)}\")\n\n            if len(dataframe.columns) == number_of_columns: \n                return True\n            return False\n        besides Exception as e:\n            increase MachinePredictiveMaintenanceException (e, sys)\n        \n\n    def is_columns_exist(self, df:pd.DataFrame) -&gt; bool:\n        attempt:\n            dataframe_columns = df.columns\n            missing_numerical_columns = []\n            missing_categorical_columns = []\n\n            for column in self._schema_config[\"numerical_columns\"]:\n                if column not in dataframe_columns:\n                    missing_numerical_columns.append(column)\n\n            if len(missing_numerical_columns) &gt; 0:\n                logging.data(f\"Lacking numerical column: {missing_numerical_columns}\")\n\n            for column in self._schema_config[\"categorical_columns\"]:\n                if column not in dataframe_columns:\n                    missing_categorical_columns.append(column)\n\n            if len(missing_categorical_columns) &gt; 0:\n                logging.data(f\"Lacking categorical column: {missing_categorical_columns}\")\n            return False if len(missing_categorical_columns)&gt;0 or len(missing_numerical_columns)&gt;0 else True\n            \n        besides Exception as e:\n            increase MachinePredictiveMaintenanceException(e, sys)\n\n    \n    def detect_dataset_drift(self, base_df, current_df, threshold = 0.05) -&gt; bool :\n        attempt:\n            standing = True\n            report = {}\n            for column in base_df.columns:\n                d1 = base_df[column]\n                d2 = current_df[column]\n\n                is_same_dist = ks_2samp(d1, d2)\n                if threshold &lt;= is_same_dist.pvalue:\n                    is_found = False\n\n                else: \n                    is_found = True\n                    standing = False\n\n                report.replace({column:{\n                    \"p_value\": float(is_same_dist.pvalue),\n                    \"drift_status\": is_found\n                }})\n            drift_report_file_path = self.data_validation_config.drift_report_file_path\n            dir_path = os.path.dirname(drift_report_file_path)# create listing\n            os.makedirs(dir_path, exist_ok=True)\n\n            write_yaml_file(file_path=drift_report_file_path, content material=report)\n        besides Exception as e:\n            increase MachinePredictiveMaintenanceException(e, sys)\n        \n    def initiate_data_validation(self)-&gt; DataValidationArtifact:\n        attempt:\n            validation_error_msg  = \"\"\n            logging.data(\"Beginning information validation\")\n\n            train_file_path = self.data_ingestion_artifact.trained_file_path\n            test_file_path = self.data_ingestion_artifact.test_file_path\n\n            # learn the info from the practice and check \n            train_dataframe = DataValidation.read_data(train_file_path)\n            test_dataframe = DataValidation.read_data(test_file_path)\n\n            # validate variety of columns\n            standing = self.validate_number_of_columns(dataframe=train_dataframe)\n            logging.data(f\"All required columns current in coaching dataframe: {standing}\")\n\n            if not standing:\n                validation_error_msg += f\"Prepare dataframe doesn't include all columns.n\"\n\n            standing = self.validate_number_of_columns(dataframe=test_dataframe)\n\n            if not standing:\n                validation_error_msg += f\"Take a look at dataframe doesn't include all columns.n\"\n\n            standing = self.is_columns_exist(df=train_dataframe)\n\n            if not standing:\n                validation_error_msg += f\"Columns are lacking in coaching dataframe.\"\n\n            standing = self.is_columns_exist(df=test_dataframe)\n\n            if not standing:\n                validation_error_msg += f\"columns are lacking in check dataframe.\"\n            ## lets verify datadrift\n            standing=self.detect_dataset_drift(base_df=train_dataframe,current_df=test_dataframe)\n            dir_path=os.path.dirname(self.data_validation_config.valid_train_file_path)\n            os.makedirs(dir_path,exist_ok=True)\n\n            train_dataframe.to_csv(\n                self.data_validation_config.valid_train_file_path, index=False, header=True\n\n            )\n            test_dataframe.to_csv(\n                self.data_validation_config.valid_test_file_path, index=False, header=True\n            )\n\n            data_validation_artifact = DataValidationArtifact(\n                validation_status=standing,\n                valid_train_file_path=self.data_validation_config.valid_train_file_path,\n                valid_test_file_path=self.data_validation_config.valid_test_file_path,\n                invalid_train_file_path=None,\n                invalid_test_file_path=None,\n                drift_report_file_path=self.data_validation_config.drift_report_file_path,\n            )\n            return data_validation_artifact\n        besides Exception as e:\n            increase MachinePredictiveMaintenanceException(e,sys)<\/code><\/pre>\n<p>Beneath is the drift report generated by the Information Validation.<\/p>\n<pre class=\"wp-block-code\"><code>Air temperature [K]:\n  drift_status: true\n  p_value: 0.016622943467175914\nFailure Sort:\n  drift_status: false\n  p_value: 1.0\nCourse of temperature [K]:\n  drift_status: false\n  p_value: 0.052940072765804994\nProduct ID:\n  drift_status: false\n  p_value: 0.09120557172716418\nRotational pace [rpm]:\n  drift_status: false\n  p_value: 0.2673520066245566\nGoal:\n  drift_status: false\n  p_value: 0.999999998717466\nInstrument put on [min]:\n  drift_status: false\n  p_value: 0.13090856779628832\nTorque [Nm]:\n  drift_status: false\n  p_value: 0.5001773464540389\nSort:\n  drift_status: false\n  p_value: 1.0\nUDI:\n  drift_status: true\n  p_value: 0.022542489133976953<\/code><\/pre>\n<h2 class=\"wp-block-heading\" id=\"h-data-transformation\">Information Transformation<\/h2>\n<p>Right here, we&#8217;ll clear and rework the validated information by changing temperature options from Kelvin to Celsius, dropping pointless columns, and making use of a change pipeline that makes use of ordinal encoding and Min-Max scaling, and dealing with information imbalance utilizing SMOTEENN.<\/p>\n<p>The reworked coaching and check datasets are saved as <b>.npy information<\/b> together with a serialized preprocessing object i.e. Minmax scaler(<b>preprocessing.pkl<\/b>) all encapsulated as an artifact for additional mannequin coaching.<\/p>\n<pre class=\"wp-block-code\"><code>class DataTransformation:\n\n    def __init__(self,data_validation_artifact: DataValidationArtifact,\n                 data_transformation_config: DataTransformationConfig):\n        attempt:\n            self.data_validation_artifact: DataValidationArtifact = data_validation_artifact\n            self.data_transformation_config: DataTransformationConfig = data_transformation_config\n            self._schema_config = read_yaml_file(file_path=SCHEMA_FILE_PATH)\n        besides Exception as e:\n            increase MachinePredictiveMaintenanceException(e,sys)\n        \n    @staticmethod\n    def read_data(file_path) -&gt; pd.DataFrame:\n        attempt:\n            return pd.read_csv(file_path)\n        besides Exception as e:\n            increase MachinePredictiveMaintenanceException(e, sys)\n        \n    def get_data_transformer_object(self):\n        attempt:\n            logging.data(\"Bought numerical cols from schema config\")\n            \n            scaler = MinMaxScaler()\n            # Fetching classes for OrdinalEncoder from schema config\n            ordinal_categories = self._schema_config.get('ordinal_categories', [])\n            ordinal_encoder = OrdinalEncoder(classes=ordinal_categories)\n\n            logging.data(\"Initialized MinMaxScaler, OrdinalEncoder with classes\")\n\n            ordinal_columns = self._schema_config['ordinal_columns']\n            scaling_features = self._schema_config['scaling_features']\n            preprocessor = ColumnTransformer(\n                [\n                    (\"Ordinal_Encoder\", ordinal_encoder, ordinal_columns),\n                    (\"MinMaxScaling\", scaler, scaling_features)\n                ]\n            )\n            logging.data(\"Created preprocessor object from ColumnTransformer\")\n            return preprocessor\n        besides Exception as e:\n            increase MachinePredictiveMaintenanceException(e, sys)\n        \n    def initiate_data_transformation(self) -&gt; DataTransformationArtifact:\n        attempt:\n            \n            logging.data(\"Beginning information transformation\")\n            preprocessor = self.get_data_transformer_object()\n            train_df = DataTransformation.read_data(self.data_validation_artifact.valid_train_file_path)\n            test_df = DataTransformation.read_data(self.data_validation_artifact.valid_test_file_path)\n            input_feature_train_df = train_df.drop(columns=[TARGET_COLUMN], axis=1)\n            target_feature_train_df = train_df[TARGET_COLUMN]\n\n            input_feature_train_df['Air temperature [c]'] = input_feature_train_df['Air temperature [K]'] - 273.15\n            input_feature_train_df['Process temperature [c]'] = input_feature_train_df['Process temperature [K]'] - 273.15\n            drop_cols = self._schema_config['drop_columns']\n\n            input_feature_train_df = drop_columns(df=input_feature_train_df, cols = drop_cols)\n            logging.data(\"Accomplished dropping the columns for Coaching dataset\")\n            input_feature_test_df = test_df.drop(columns=[TARGET_COLUMN], axis=1)\n            target_feature_test_df = test_df[TARGET_COLUMN]\n\n            input_feature_test_df['Air temperature [c]'] = input_feature_test_df['Air temperature [K]'] - 273.15\n            input_feature_test_df['Process temperature [c]'] = input_feature_test_df['Process temperature [K]'] - 273.15\n            drop_cols = self._schema_config['drop_columns']\n            input_feature_test_df = drop_columns(df=input_feature_test_df, cols = drop_cols)\n\n            logging.data(\"Accomplished dropping the columns for Testing dataset\")\n            input_feature_train_arr = preprocessor.fit_transform(input_feature_train_df)\n            input_feature_test_arr = preprocessor.rework(input_feature_test_df)\n            \n            smt =  SMOTEENN(sampling_strategy=\"minority\")\n            input_feature_train_final, target_feature_train_final = smt.fit_resample(\n                input_feature_train_arr, target_feature_train_df\n            )\n            logging.data(\"Utilized SMOTEENN on coaching dataset\")\n\n            input_feature_test_final, target_feature_test_final = smt.fit_resample(\n                input_feature_test_arr, target_feature_test_df\n            )\n            train_arr = np.c_[\n                input_feature_train_final, np.array(target_feature_train_final)\n            ]\n            test_arr = np.c_[\n                input_feature_test_final, np.array(target_feature_test_final)\n            ]\n            save_numpy_array_data(self.data_transformation_config.transformed_train_file_path, array=train_arr, )\n            save_numpy_array_data(self.data_transformation_config.transformed_test_file_path,array=test_arr,)\n            save_object( self.data_transformation_config.transformed_object_file_path, preprocessor,)\n\n            save_object( \"final_model\/preprocessor.pkl\", preprocessor,)\n\n            data_transformation_artifact=DataTransformationArtifact(\n                transformed_object_file_path=self.data_transformation_config.transformed_object_file_path,\n                transformed_train_file_path=self.data_transformation_config.transformed_train_file_path,\n                transformed_test_file_path=self.data_transformation_config.transformed_test_file_path\n            )\n            return data_transformation_artifact\n        besides Exception as e:\n            increase MachinePredictiveMaintenanceException(e, sys)<\/code><\/pre>\n<h2 class=\"wp-block-heading\" id=\"h-training-and-evaluation\">Coaching and Analysis<\/h2>\n<p>Now we&#8217;ll practice completely different classification fashions on the reworked coaching information like Resolution Tree,\u00a0Random Forest,\u00a0Gradient Boosting,\u00a0Logistic Regression, and AdaBoost fashions and consider their efficiency utilizing analysis metrics like f1-score, precision, and recall, after which log these particulars with <a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/www.analyticsvidhya.com\/blog\/2021\/07\/machine-learning-workflow-using-mlflow-a-beginners-guide\/\" target=\"_blank\" rel=\"noopener\">MLflow<\/a>.<\/p>\n<p>After selecting the right mannequin, it saves each the mannequin and transformation object domestically to artifact and analysis metrics to make use of it for deployment.<\/p>\n<pre class=\"wp-block-code\"><code>class ModelTrainer:\n    def __init__(self, data_transformation_artifact: DataTransformationArtifact, model_trainer_config: ModelTrainerConfig):\n        attempt:\n            self.data_transformation_artifact = data_transformation_artifact\n            self.model_trainer_config = model_trainer_config\n\n        besides Exception as e:\n            increase MachinePredictiveMaintenanceException(e, sys)\n        \n    def track_mlflow(self, best_model, classification_metric, input_example):\n        \"\"\"\n        Log mannequin and metrics to MLflow.\n\n        Args:\n            best_model: The educated mannequin object.\n            classification_metric: The classification metrics (f1, precision, recall).\n            input_example: An instance enter information pattern for the mannequin.\n        \"\"\"\n        attempt:\n            with mlflow.start_run():\n                f1_score=classification_metric.f1_score\n                precision_score=classification_metric.precision_score\n                recall_score=classification_metric.recall_score\n\n                mlflow.log_metric(\"f1_score\",f1_score)\n                mlflow.log_metric(\"precision\",precision_score)\n                mlflow.log_metric(\"recall_score\",recall_score)\n                mlflow.sklearn.log_model(best_model,\"mannequin\", input_example=input_example)\n        besides Exception as e:\n            increase MachinePredictiveMaintenanceException(e, sys)\n    \n    def train_model(self, X_train, y_train, X_test, y_test ):\n        \"\"\"\n        Prepare a number of fashions and choose the best-performing one primarily based on analysis metrics.\n\n        Args:\n            X_train: Coaching options.\n            y_train: Coaching labels.\n            X_test: Testing options.\n            y_test: Testing labels.\n\n        Returns:\n            ModelTrainerArtifact: An artifact containing particulars in regards to the educated mannequin and its metrics.\n        \"\"\"\n        fashions = {\n                \"Random Forest\": RandomForestClassifier(verbose=1),\n                \"Resolution Tree\": DecisionTreeClassifier(),\n                \"Gradient Boosting\": GradientBoostingClassifier(verbose=1),\n                \"Logistic Regression\": LogisticRegression(verbose=1),\n                \"AdaBoost\": AdaBoostClassifier(),\n            }\n        params={\n            \"Resolution Tree\": {\n                'criterion':['gini', 'entropy', 'log_loss'],\n                # 'splitter':['best','random'],\n                # 'max_features':['sqrt','log2'],\n            },\n            \"Random Forest\":{\n                # 'criterion':['gini', 'entropy', 'log_loss'],\n                \n                # 'max_features':['sqrt','log2',None],\n                'n_estimators': [8,16,32,128,256]\n            },\n            \"Gradient Boosting\":{\n                # 'loss':['log_loss', 'exponential'],\n                'learning_rate':[.1,.01,.05,.001],\n                'subsample':[0.6,0.7,0.75,0.85,0.9],\n                # 'criterion':['squared_error', 'friedman_mse'],\n                # 'max_features':['auto','sqrt','log2'],\n                'n_estimators': [8,16,32,64,128,256]\n            },\n            \"Logistic Regression\":{},\n            \"AdaBoost\":{\n                'learning_rate':[.1,.01,.001],\n                'n_estimators': [8,16,32,64,128,256]\n            }\n            \n        }\n        model_report: dict = evaluate_models(X_train=X_train, y_train=y_train, X_test=X_test, y_test=y_test,\n                                             fashions=fashions, param=params)\n        best_model_score = max(sorted(model_report.values()))\n        logging.data(f\"Greatest Mannequin Rating: {best_model_score}\")\n        best_model_name = listing(model_report.keys())[\n            list(model_report.values()).index(best_model_score)\n        ]\n        logging.data(f\"Greatest Mannequin Identify: {best_model_name}\")\n        best_model = fashions[best_model_name]\n\n        y_train_pred = best_model.predict(X_train)\n        classification_train_metric = get_classification_score(y_true=y_train, y_pred=y_train_pred)\n\n        input_example = X_train[:1]\n        print(input_example)\n        # Observe the experiments with mlflow\n        self.track_mlflow(best_model, classification_train_metric, input_example)\n\n\n        y_test_pred=best_model.predict(X_test)\n        classification_test_metric = get_classification_score(y_true=y_test, y_pred=y_test_pred)\n\n        # Observe the experiments with mlflow\n        self.track_mlflow(best_model, classification_test_metric, input_example)\n\n\n        preprocessor = load_object(file_path=self.data_transformation_artifact.transformed_object_file_path)\n\n\n        model_dir_path = os.path.dirname(self.model_trainer_config.trained_model_file_path)\n        os.makedirs(model_dir_path,exist_ok=True)\n\n        Machine_Predictive_Model = MachinePredictiveModel(mannequin=best_model)\n\n        save_object(self.model_trainer_config.trained_model_file_path,obj=MachinePredictiveModel)\n\n        save_object(\"final_model\/mannequin.pkl\",best_model)\n\n        ## Mannequin Coach Artifact\n        model_trainer_artifact=ModelTrainerArtifact(trained_model_file_path=self.model_trainer_config.trained_model_file_path,\n                             train_metric_artifact=classification_train_metric,\n                             test_metric_artifact=classification_test_metric\n                             )\n        logging.data(f\"Mannequin coach artifact: {model_trainer_artifact}\")\n        return model_trainer_artifact\n\n    def initiate_model_trainer(self) -&gt; ModelTrainerArtifact:\n\n        attempt:\n            train_file_path = self.data_transformation_artifact.transformed_train_file_path\n            test_file_path = self.data_transformation_artifact.transformed_test_file_path\n\n            #loading coaching array and testing array\n            train_arr = load_numpy_array_data(train_file_path)\n            test_arr = load_numpy_array_data(test_file_path)\n\n            X_train, y_train, X_test, y_test = (\n                train_arr[:, :-1],\n                train_arr[:, -1],\n                test_arr[:, :-1],\n                test_arr[:, -1],\n            )\n\n            model_trainer_artifact=self.train_model(X_train,y_train,X_test,y_test)\n            return model_trainer_artifact\n            \n        besides Exception as e:\n            increase MachinePredictiveMaintenanceException(e, sys)<\/code><\/pre>\n<p>Now, let\u2019s create a <b>training_pipeline.py<\/b> file the place we sequentially combine all of the steps of information ingestion, validation, transformation, and mannequin coaching into a whole pipeline.<\/p>\n<pre class=\"wp-block-code\"><code>def run_pipeline(self):\n\n        \"\"\"\n        Executes your complete coaching pipeline.\n\n        Returns:\n            ModelTrainerArtifact: Incorporates metadata in regards to the educated mannequin.\n        \"\"\"\n        attempt:\n            data_ingestion_artifact= self.data_ingestion()\n            data_validation_artifact= self.data_validation(data_ingestion_artifact=data_ingestion_artifact)\n            data_transformation_artifact= self.data_transformation(data_validation_artifact=data_validation_artifact)\n            model_trainer_artifact= self.model_trainer(data_transformation_artifact=data_transformation_artifact)\n            self.sync_artifact_dir_to_s3()\n            self.sync_saved_model_dir_to_s3()\n            \n            return model_trainer_artifact\n        besides Exception as e:\n            increase MachinePredictiveMaintenanceException(e,sys)<\/code><\/pre>\n<p>You possibly can visually see how we have now constructed the <a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/github.com\/karthikponna\/Predictive_Maintenance_MLOps\/blob\/main\/assets\/training_pipeline.gif\" target=\"_blank\" rel=\"nofollow noopener\">training_pipeline<\/a>.<\/p>\n<figure class=\"wp-block-image size-full is-resized figure  mt-2 mb-2 d-table mx-auto\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/training_pipeline.webp\" alt=\"training_pipeline\" class=\"wp-image-221101\" style=\"width:700px;height:auto\" srcset=\"https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/training_pipeline.webp 960w, https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/training_pipeline-300x169.webp 300w, https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/training_pipeline-768x432.webp 768w, https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/training_pipeline-150x84.webp 150w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\"\/><\/figure>\n<p>Now that we\u2019ve accomplished creating the pipeline, run the training_pipeline.py file to view the artifacts generated within the earlier steps.<\/p>\n<pre class=\"wp-block-code\"><code>python training_pipeline.py<\/code><\/pre>\n<p>Run the next command to view the MLflow dashboard.<\/p>\n<pre class=\"wp-block-code\"><code>mlflow ui  # Launch the MLflow dashboard to watch experiments.<\/code><\/pre>\n<p>As you may see, we have now efficiently logged mannequin metrics like recall, precision, and F1-score in MLflow.<\/p>\n<figure class=\"wp-block-image size-full is-resized figure  mt-2 mb-2 d-table mx-auto\"><img loading=\"lazy\" decoding=\"async\" width=\"1895\" height=\"869\" src=\"https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/mlflow_pwrlSun.webp\" alt=\"ML log in\" class=\"wp-image-221103\" style=\"width:770px;height:auto\" srcset=\"https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/mlflow_pwrlSun.webp 1895w, https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/mlflow_pwrlSun-300x138.webp 300w, https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/mlflow_pwrlSun-768x352.webp 768w, https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/mlflow_pwrlSun-1536x704.webp 1536w, https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/mlflow_pwrlSun-150x69.webp 150w\" sizes=\"auto, (max-width: 1895px) 100vw, 1895px\"\/><\/figure>\n<h2 class=\"wp-block-heading\" id=\"h-aws-integration\">AWS Integration<\/h2>\n<p>After operating the training_pipeline.py file, we generated artifacts and saved them domestically. Now, we&#8217;ll retailer these artifacts in an AWS S3 bucket to allow cloud-based storage and accessibility.<\/p>\n<p>Utilizing a Docker picture, we&#8217;ll push it to AWS ECR by way of GitHub Actions after which deploy it to manufacturing utilizing AWS EC2. We are going to focus on this course of in additional element within the upcoming sections.<\/p>\n<figure class=\"wp-block-image size-full figure  mt-2 mb-2 d-table mx-auto\"><img loading=\"lazy\" decoding=\"async\" width=\"1585\" height=\"410\" src=\"https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/AWS-Integration.webp\" alt=\"AWS Integration\" class=\"wp-image-221104\" srcset=\"https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/AWS-Integration.webp 1585w, https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/AWS-Integration-300x78.webp 300w, https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/AWS-Integration-768x199.webp 768w, https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/AWS-Integration-1536x397.webp 1536w, https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/AWS-Integration-150x39.webp 150w\" sizes=\"auto, (max-width: 1585px) 100vw, 1585px\"\/><\/figure>\n<h2 class=\"wp-block-heading\" id=\"h-aws-s3\">AWS S3<\/h2>\n<p>Observe these steps to create an AWS S3 bucket:<\/p>\n<h3 class=\"wp-block-heading\" id=\"h-step1-download-aws-cli\">Step1: Obtain AWS CLI<\/h3>\n<ul class=\"wp-block-list\">\n<li>You possibly can click on <a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/docs.aws.amazon.com\/cli\/latest\/userguide\/getting-started-install.html\" rel=\"nofollow\">right here<\/a> to obtain AWS CLI for Home windows, Linux, and macOS<\/li>\n<\/ul>\n<h3 class=\"wp-block-heading\" id=\"h-step2-log-in-to-aws-iam-user\">Step2:\u00a0Log in to AWS IAM Person<\/h3>\n<ul class=\"wp-block-list\">\n<li>You possibly can sign up to the <a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/aws.amazon.com\/console\/\" rel=\"nofollow\">Console<\/a> together with your IAM person credentials and choose the suitable AWS area (eg., us-east-1).<\/li>\n<li>As soon as logged in, seek for \u2018IAM\u2019 within the AWS search bar. Navigate to \u2018Customers,\u2019 choose your username, and go to the \u2018Safety Credentials\u2019 tab. Underneath \u2018Entry Keys,\u2019 click on \u2018Create entry key,\u2019 select \u2018CLI,\u2019 after which affirm by clicking \u2018Create entry key\u2019.<\/li>\n<li>Now, open your terminal and kind aws configure. Enter your AWS Entry Key and Secret Entry Key when prompted, then press Enter.<\/li>\n<\/ul>\n<p>Your IAM person has been efficiently linked to your mission.<\/p>\n<h3 class=\"wp-block-heading\" id=\"h-step3-navigate-to-s3-service\">Step3: Navigate to S3 Service<\/h3>\n<ul class=\"wp-block-list\">\n<li>As soon as logged in, seek for \u201cS3\u201d within the AWS search bar.<\/li>\n<li>Click on on Amazon S3 service and choose the \u201cCreate bucket\u201d button. Subsequent, kind in a singular bucket title (eg., machinepredictive)<\/li>\n<li>Assessment the configuration settings, after which click on on \u201cCreate bucket.\u201d<\/li>\n<\/ul>\n<p>Your bucket is now created, and you can begin importing artifacts. Now, add the next code to your training_pipeline.py file and run it once more to see the artifacts in your AWS S3 bucket.<\/p>\n<pre class=\"wp-block-code\"><code>    def sync_artifact_dir_to_s3(self):\n\n        \"\"\"\n        Syncs the artifact listing to S3.\n        \"\"\"\n        attempt:\n\n            aws_bucket_url = f\"s3:\/\/{TRAINING_BUCKET_NAME}\/artifact\/{self.training_pipeline_config.timestamp}\"\n            self.s3_sync.sync_folder_to_s3(folder = self.training_pipeline_config.artifact_dir,aws_bucket_url=aws_bucket_url)\n\n        besides Exception as e:\n            increase MachinePredictiveMaintenanceException(e,sys)\n        \n    def sync_saved_model_dir_to_s3(self):\n\n        \"\"\"\n        Syncs the saved mannequin listing to S3.\n        \"\"\"\n\n        attempt:\n            aws_bucket_url = f\"s3:\/\/{TRAINING_BUCKET_NAME}\/final_model\/{self.training_pipeline_config.timestamp}\"\n            self.s3_sync.sync_folder_to_s3(folder = self.training_pipeline_config.model_dir,aws_bucket_url=aws_bucket_url)\n        besides Exception as e:\n            increase MachinePredictiveMaintenanceException(e,sys)<\/code><\/pre>\n<figure class=\"wp-block-image size-full figure  mt-2 mb-2 d-table mx-auto\"><img loading=\"lazy\" decoding=\"async\" width=\"1894\" height=\"828\" src=\"https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/aws_s3_bucket.webp\" alt=\"aws_s3_bucket\" class=\"wp-image-221107\" srcset=\"https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/aws_s3_bucket.webp 1894w, https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/aws_s3_bucket-300x131.webp 300w, https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/aws_s3_bucket-768x336.webp 768w, https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/aws_s3_bucket-1536x671.webp 1536w, https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/aws_s3_bucket-150x66.webp 150w\" sizes=\"auto, (max-width: 1894px) 100vw, 1894px\"\/><\/figure>\n<h2 class=\"wp-block-heading\" id=\"h-amazon-elastic-container-registry-ecr\">Amazon Elastic Container Registry (ECR)<\/h2>\n<p>Observe these steps to create an ECR repository.<\/p>\n<ul class=\"wp-block-list\">\n<li>Seek for \u2018ECR\u2019 within the AWS search bar.<\/li>\n<li>Click on Create Repository, and choose Personal Repository.<\/li>\n<li>Present a repository title (e.g., machinepredictive) and click on Create.<\/li>\n<\/ul>\n<figure class=\"wp-block-image size-full figure  mt-2 mb-2 d-table mx-auto\"><img loading=\"lazy\" decoding=\"async\" width=\"1897\" height=\"444\" src=\"https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/Amazon-Elastic-Container-Registry-ECR.webp\" alt=\"Amazon Elastic Container Registry (ECR)\" class=\"wp-image-221108\" srcset=\"https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/Amazon-Elastic-Container-Registry-ECR.webp 1897w, https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/Amazon-Elastic-Container-Registry-ECR-300x70.webp 300w, https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/Amazon-Elastic-Container-Registry-ECR-768x180.webp 768w, https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/Amazon-Elastic-Container-Registry-ECR-1536x360.webp 1536w, https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/Amazon-Elastic-Container-Registry-ECR-150x35.webp 150w\" sizes=\"auto, (max-width: 1897px) 100vw, 1897px\"\/><\/figure>\n<p>Copy the URI out of your ECR repository, which ought to look one thing like \u201c<b>788614365622.dkr.ecr.ap-southeast-2.amazonaws.com<\/b>\u201c, and reserve it someplace. We are going to want it later to stick into GitHub Secrets and techniques.<\/p>\n<h2 class=\"wp-block-heading\" id=\"h-docker-integration-for-deployment\">Docker Integration for Deployment<\/h2>\n<p>Dockerizing our mission ensures it runs easily in any setting with out dependency points. It\u2019s a must have instrument for packaging and sharing functions effortlessly.<\/p>\n<p>Holding the Docker picture as small as attainable is essential for effectivity. We will reduce its dimension through the use of strategies like multi-staging and selecting a light-weight Python base picture.<\/p>\n<pre class=\"wp-block-code\"><code>FROM python:3.10-slim-buster\nWORKDIR \/app\nCOPY . \/app\n\nRUN apt replace -y &amp;&amp; apt set up awscli -y\n\nRUN apt-get replace &amp;&amp; pip set up -r necessities.txt\nCMD [\"python3\", \"app.py\"]<\/code><\/pre>\n<h2 class=\"wp-block-heading\" id=\"h-setup-action-secrets-and-variables-nbsp\">Setup Motion Secrets and techniques and Variables\u00a0<\/h2>\n<p>To securely retailer delicate info like AWS credentials and repository URIs, we have to arrange GitHub Motion Secrets and techniques in our repository. Observe these steps:<\/p>\n<h4 class=\"wp-block-heading\" id=\"h-step1-open-your-github-repository\">Step1: Open Your GitHub Repository<\/h4>\n<p>Navigate to your repository on GitHub.<\/p>\n<h4 class=\"wp-block-heading\" id=\"h-step2-go-to-settings\">Step2:\u00a0Go to Settings<\/h4>\n<p>On the high of your repository web page, find and click on on the \u201cSettings\u201d tab.<\/p>\n<h4 class=\"wp-block-heading\" id=\"h-step3-access-secrets-and-variables\">Step3: Entry Secrets and techniques and Variables<\/h4>\n<p>Within the left sidebar, scroll all the way down to Secrets and techniques and Variables \u2192 Choose Actions.<\/p>\n<h4 class=\"wp-block-heading\" id=\"h-step4-create-a-new-secret\">Step4:\u00a0Create a New Secret<\/h4>\n<p>Click on the New repository secret button.<\/p>\n<h4 class=\"wp-block-heading\" id=\"h-step5-add-aws-credentials\">Step5:\u00a0Add AWS Credentials<\/h4>\n<ul class=\"wp-block-list\">\n<li>Create a secret with the title \u201cAWS_ACCESS_KEY_ID\u201d and paste your AWS Entry Key.<\/li>\n<li>Create one other secret named AWS_SECRET_ACCESS_KEY and paste your AWS Secret Entry Key.<\/li>\n<li>create a secret named \u201cAWS_REGION\u201d and paste your chosen area (eg., us-east-1).<\/li>\n<\/ul>\n<h4 class=\"wp-block-heading\" id=\"h-step6-add-aws-ecr-repository-uri\">Step6:\u00a0Add AWS ECR Repository URI<\/h4>\n<ul class=\"wp-block-list\">\n<li>Copy your ECR repository URI (e.g., 788614365622.dkr.ecr.ap-southeast-2.amazonaws.com).<\/li>\n<li>Create a brand new secret named \u201cAWS_ECR_LOGIN_URI\u201d and paste the copied URI.<\/li>\n<li>Create a brand new secret named \u201cECR_REPOSITORY_NAME\u201d and paste the title of the ECR repository e.g., machinepredictive).<\/li>\n<\/ul>\n<figure class=\"wp-block-image size-full is-resized figure  mt-2 mb-2 d-table mx-auto\"><img loading=\"lazy\" decoding=\"async\" width=\"1176\" height=\"832\" src=\"https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/Screenshot_2025-02-09_194621.webp\" alt=\"\" class=\"wp-image-221111\" style=\"width:725px;height:auto\" srcset=\"https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/Screenshot_2025-02-09_194621.webp 1176w, https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/Screenshot_2025-02-09_194621-300x212.webp 300w, https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/Screenshot_2025-02-09_194621-768x543.webp 768w, https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/Screenshot_2025-02-09_194621-150x106.webp 150w\" sizes=\"auto, (max-width: 1176px) 100vw, 1176px\"\/><\/figure>\n<h2 class=\"wp-block-heading\" id=\"h-aws-ec2-nbsp\">AWS EC2\u00a0<\/h2>\n<p>Now let\u2019s perceive find out how to create an occasion with AWS EC2. Observe these steps.<\/p>\n<ul class=\"wp-block-list\">\n<li>Navigate to the EC2 service within the AWS Administration Console and click on Launch an occasion.<\/li>\n<li>Identify your occasion (e.g., machinepredictive) and choose Ubuntu as your working system.<\/li>\n<li>Select the occasion kind as t2.massive.<\/li>\n<li>Choose your default key pair.<\/li>\n<li>Underneath community safety, select the default VPC and configure the safety teams as wanted.<\/li>\n<li>Lastly, click on Launch Occasion.<\/li>\n<\/ul>\n<h3 class=\"wp-block-heading\" id=\"h-aws-ec2-cli\">AWS EC2 CLI<\/h3>\n<p>After creating the occasion named machinepredictive, choose its Occasion ID and click on Join. Then, underneath EC2 Occasion Join, click on Join once more. This can open an AWS CLI interface the place you may run the required instructions.<\/p>\n<p>Now, enter these instructions within the CLI one after the other to arrange Docker in your EC2 occasion:<\/p>\n<pre class=\"wp-block-code\"><code>sudo apt-get replace -y\nsudo apt-get improve\ncurl -fsSL https:\/\/get.docker.com -o get-docker.sh\nsudo sh get-docker.sh\nsudo usermod -aG docker ubuntu\nnewgrp docker<\/code><\/pre>\n<p>Hold your CLI session open. Subsequent, navigate to your GitHub repository, click on on Settings, then go to the Actions part, and underneath Runners, click on New self-hosted runner. Select the Linux runner picture. Now, run the next instructions one after the other in your CLI to obtain and configure your self-hosted runner:<\/p>\n<pre class=\"wp-block-code\"><code># Create a folder and navigate into it\nmkdir actions-runner &amp;&amp; cd actions-runner\n\n# Obtain the most recent runner package deal\ncurl -o actions-runner-linux-x64-2.322.0.tar.gz -L https:\/\/github.com\/actions\/runner\/releases\/obtain\/v2.322.0\/actions-runner-linux-x64-2.322.0.tar.gz\n\n# Optionally available: Validate the hash\necho \"b13b784808359f31bc79b08a191f5f83757852957dd8fe3dbfcc38202ccf5768  actions-runner-linux-x64-2.322.0.tar.gz\" | shasum -a 256 -c\n\n# Extract the installer\ntar xzf .\/actions-runner-linux-x64-2.322.0.tar.gz\n\n# Configure the runner\n.\/config.sh --url https:\/\/github.com\/karthikponna\/Predictive_Maintenance_MLOps --token \"Paste your token right here\"\n\n# Run the runner\n.\/run.sh<\/code><\/pre>\n<p>This can obtain, configure, and begin your self-hosted GitHub Actions runner in your EC2 occasion.<\/p>\n<p>After establishing the self-hosted GitHub Actions runner, the CLI will immediate you to enter a reputation for the runner. Sort self-hosted and press Enter. <\/p>\n<h2 class=\"wp-block-heading\" id=\"h-ci-cd-with-github-actions\">CI\/CD with GitHub Actions<\/h2>\n<p>You possibly can try the <b>.github\/workflows\/predominant.yml<\/b> <a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/github.com\/karthikponna\/Predictive_Maintenance_MLOps\/blob\/main\/.github\/workflows\/main.yml\" target=\"_blank\" rel=\"nofollow noopener\">code<\/a>\u00a0file.<\/p>\n<p>Now, let\u2019s dive into what every part of this predominant.yml file does.<\/p>\n<ul class=\"wp-block-list\">\n<li><b>Steady Integration Job<\/b>: This job runs on an Ubuntu runner to take a look at the code, lint it, and execute unit exams.<\/li>\n<li><b>Steady Supply Job<\/b>: Triggered after CI, this job installs utilities, configures AWS credentials, logs into Amazon ECR, and builds, tags, and pushes your Docker picture to ECR.<\/li>\n<li><b>Steady Deployment Job<\/b>: Operating on a self-hosted runner, this job pulls the most recent Docker picture from ECR, runs it as a container to serve customers, and cleans up any earlier photographs or containers.<\/li>\n<\/ul>\n<figure class=\"wp-block-image size-full figure mt-2 mb-2 d-table mx-auto\"><img loading=\"lazy\" decoding=\"async\" width=\"1893\" height=\"687\" src=\"https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/GitHub-Actions.webp\" alt=\"GitHub Actions\" class=\"wp-image-221112\" srcset=\"https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/GitHub-Actions.webp 1893w, https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/GitHub-Actions-300x109.webp 300w, https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/GitHub-Actions-768x279.webp 768w, https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/GitHub-Actions-1536x557.webp 1536w, https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/GitHub-Actions-150x54.webp 150w\" sizes=\"auto, (max-width: 1893px) 100vw, 1893px\"\/><\/figure>\n<p>As soon as the Steady Deployment Job completes efficiently, you\u2019ll see an output like this within the AWS CLI.<\/p>\n<figure class=\"wp-block-image size-full figure  mt-2 mb-2 d-table mx-auto\"><img loading=\"lazy\" decoding=\"async\" width=\"1906\" height=\"785\" src=\"https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/aws_ec2_cli_1.webp\" alt=\"aws_ec2_cli_1\" class=\"wp-image-221113\" srcset=\"https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/aws_ec2_cli_1.webp 1906w, https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/aws_ec2_cli_1-300x124.webp 300w, https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/aws_ec2_cli_1-768x316.webp 768w, https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/aws_ec2_cli_1-1536x633.webp 1536w, https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/aws_ec2_cli_1-150x62.webp 150w\" sizes=\"auto, (max-width: 1906px) 100vw, 1906px\"\/><\/figure>\n<p>Open your AWS EC2 occasion by clicking its Occasion ID. Confirm that the occasion state is Operating and find the Public IPv4 DNS. Click on on the \u201cOpen handle\u201d choice to robotically launch your FastAPI utility in your browser. Now, let\u2019s dive into FastAPI.<\/p>\n<h2 class=\"wp-block-heading\" id=\"h-fastapi\">FastAPI<\/h2>\n<p>You possibly can try the <b>app.py<\/b> <a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/github.com\/karthikponna\/Predictive_Maintenance_MLOps\/blob\/main\/app.py\" target=\"_blank\" rel=\"nofollow noopener\">file<\/a>.\u00a0<\/p>\n<p>On this FastAPI utility, I\u2019ve created two predominant routes, one for coaching the mannequin and one other for producing predictions. Let\u2019s discover every route intimately.\u00a0\u00a0<\/p>\n<figure class=\"wp-block-image size-full figure mt-2 mb-2 d-table mx-auto\"><img loading=\"lazy\" decoding=\"async\" width=\"1898\" height=\"749\" src=\"https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/fast_api_interface.webp\" alt=\"FastAPI\" class=\"wp-image-221114\" srcset=\"https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/fast_api_interface.webp 1898w, https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/fast_api_interface-300x118.webp 300w, https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/fast_api_interface-768x303.webp 768w, https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/fast_api_interface-1536x606.webp 1536w, https:\/\/cdn.analyticsvidhya.com\/wp-content\/uploads\/2025\/02\/fast_api_interface-150x59.webp 150w\" sizes=\"auto, (max-width: 1898px) 100vw, 1898px\"\/><\/figure>\n<h3 class=\"wp-block-heading\" id=\"h-train-route\">Prepare route<\/h3>\n<p>The \/practice endpoint begins the mannequin coaching course of utilizing a predefined dataset, making it simple to replace and enhance the mannequin with new information.\u00a0It&#8217;s particularly helpful for retraining the mannequin to enhance accuracy or incorporate new information, guaranteeing the mannequin stays up-to-date and performs optimally.<\/p>\n<figure class=\"wp-block-image figure mt-2 mb-2 d-table mx-auto\"><img decoding=\"async\" src=\"https:\/\/av-eks-lekhak.s3.amazonaws.com\/media\/article_images\/train_route.png\" alt=\" \/train route\"\/><\/figure>\n<h4 class=\"wp-block-heading\" id=\"h-predict-route\">Predict route<\/h4>\n<p>The \/predict endpoint accepts a CSV file by way of POST. This endpoint handles incoming information, leverages the educated mannequin to generate predictions, after which returns the outcomes formatted as JSON.\u00a0This route is ideal for making use of the mannequin to new datasets, making it environment friendly for large-scale prediction duties.<\/p>\n<p>Within the \/predict route, we\u2019ve included a pattern check.csv file; you may obtain it <a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/github.com\/karthikponna\/Predictive_Maintenance_MLOps\/blob\/main\/valid_data\/test.csv\" target=\"_blank\" rel=\"nofollow noopener\">right here<\/a>.<\/p>\n<figure class=\"wp-block-image figure mt-2 mb-2 d-table mx-auto\"><img decoding=\"async\" src=\"https:\/\/av-eks-lekhak.s3.amazonaws.com\/media\/article_images\/predict_route.png\" alt=\" \/predict route\"\/><figcaption class=\"wp-element-caption\">\/predict route<\/figcaption><\/figure>\n<h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2>\n<p>Collectively, we constructed a full production-ready Predictive Upkeep MLOps mission\u2014from gathering and preprocessing information to coaching, evaluating, and deploying our mannequin utilizing Docker, AWS, and FastAPI. This mission reveals how MLOps can bridge the hole between growth and manufacturing, making it simpler to construct sturdy, scalable options.<\/p>\n<p>Bear in mind, this information is all about studying and making use of these strategies to your information science tasks. Don\u2019t hesitate to experiment, innovate, and discover new enhancements as you progress. Thanks for sticking with me till the top\u2014continue to learn, hold doing, and continue to grow!<\/p>\n<p><b>GitHub repo:\u00a0<\/b><a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/github.com\/karthikponna\/Predictive_Maintenance_MLOps\" target=\"_blank\" rel=\"nofollow noopener\">https:\/\/github.com\/karthikponna\/Predictive_Maintenance_MLOps<\/a><\/p>\n<h3 class=\"wp-block-heading\" id=\"h-key-takeaways\">Key Takeaways<\/h3>\n<ul class=\"wp-block-list\">\n<li>We constructed an end-to-end MLOps pipeline that covers information ingestion, mannequin coaching, and deployment.<\/li>\n<li>Docker, AWS, and FastAPI work collectively seamlessly to maneuver from growth to manufacturing.<\/li>\n<li>Dockerizing our ML mission is essential\u2014it ensures it runs easily in any setting with none dependency complications.<\/li>\n<li>Steady deployment ensures the mannequin stays environment friendly and up-to-date in real-world functions.<\/li>\n<\/ul>\n<h2 class=\"wp-block-heading\" id=\"h-frequently-asked-questions\">Continuously Requested Questions<\/h2>\n<div class=\"schema-faq wp-block-yoast-faq-block\">\n<div class=\"schema-faq-section\" id=\"faq-question-1739343060762\"><strong class=\"schema-faq-question\">Q1. Why will we use Docker on this mission?<\/strong> <\/p>\n<p class=\"schema-faq-answer\">A.\u00a0Docker ensures our ML mission runs easily in any setting by eliminating dependency points.<\/p>\n<\/p><\/div>\n<div class=\"schema-faq-section\" id=\"faq-question-1739343117868\"><strong class=\"schema-faq-question\">Q2. How does AWS assist with this mission?<\/strong> <\/p>\n<p class=\"schema-faq-answer\">A. AWS companies like EC2, S3, and ECR allow seamless deployment, storage, and scaling of our utility.<\/p>\n<\/p><\/div>\n<div class=\"schema-faq-section\" id=\"faq-question-1739343144774\"><strong class=\"schema-faq-question\">Q3. What&#8217;s MLflow used for?<\/strong> <\/p>\n<p class=\"schema-faq-answer\">A. MLflow makes machine studying growth simpler by providing instruments for monitoring experiments, versioning fashions, and deploying them.<\/p>\n<\/p><\/div>\n<div class=\"schema-faq-section\" id=\"faq-question-1739343158387\"><strong class=\"schema-faq-question\">This autumn.\u00a0What&#8217;s the position of GitHub Actions on this mission?<\/strong> <\/p>\n<p class=\"schema-faq-answer\">A.\u00a0GitHub Actions automates the CI\/CD course of\u2014operating exams, constructing Docker photographs, and deploying updates\u2014guaranteeing a clean transition from growth to manufacturing.<\/p>\n<\/p><\/div><\/div>\n<p><strong>The media proven on this article shouldn&#8217;t be owned by Analytics Vidhya and is used on the Creator\u2019s discretion.<\/strong><\/p>\n<div class=\"border-top py-3 author-info my-4\">\n<div class=\"author-card d-flex align-items-center\">\n<div class=\"flex-shrink-0 overflow-hidden\">\n                                    <a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/www.analyticsvidhya.com\/blog\/author\/karthik3852845\/\" class=\"text-decoration-none active-avatar\"><br \/>\n                                                                       <img decoding=\"async\" src=\"https:\/\/av-eks-lekhak.s3.amazonaws.com\/media\/lekhak-profile-images\/converted_image_3tjdORM.webp\" width=\"48\" height=\"48\" alt=\"Karthik Ponna\" loading=\"lazy\" class=\"rounded-circle\"\/><\/p>\n<p>                                <\/a>\n                                <\/div><\/div>\n<p>Hello! I am Karthik Ponna, a Machine Studying Engineer at Antern. I am deeply captivated with exploring the fields of AI and Information Science, as they consistently evolve and form the long run. I consider writing blogs is an effective way to not solely improve my abilities and solidify my understanding but additionally to share my data and insights with others in the neighborhood. This helps me join with like-minded people who share a curiosity for expertise and innovation.<\/p>\n<\/p><\/div><\/div>\n<p><h4 class=\"fs-24 text-dark\">Login to proceed studying and revel in expert-curated content material.<\/h4>\n<p>                        <button class=\"btn btn-primary mx-auto d-table\" data-bs-toggle=\"modal\" data-bs-target=\"#loginModal\" id=\"readMoreBtn\">Hold Studying for Free<\/button>\n                    <\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>Machines don\u2019t break down out of nowhere\u2014there are at all times indicators. The issue? People aren\u2019t at all times nice at noticing them. That\u2019s the place Machine Predictive Upkeep is available in! This information will take you thru the thrilling world of Machine Predictive Upkeep, utilizing AWS and MLOps to make sure your gear stays [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":2298,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[55],"tags":[113,2259,2260,2258],"class_list":["post-2296","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-machine-learning","tag-machine","tag-maintenance","tag-mlops","tag-predictive"],"_links":{"self":[{"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=\/wp\/v2\/posts\/2296","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2296"}],"version-history":[{"count":1,"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=\/wp\/v2\/posts\/2296\/revisions"}],"predecessor-version":[{"id":2297,"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=\/wp\/v2\/posts\/2296\/revisions\/2297"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=\/wp\/v2\/media\/2298"}],"wp:attachment":[{"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}<!-- This website is optimized by Airlift. Learn more: https://airlift.net. Template:. Learn more: https://airlift.net. Template: 69d9690a190636c2e0989534. Config Timestamp: 2026-04-10 21:18:02 UTC, Cached Timestamp: 2026-06-17 10:01:07 UTC -->