VOOZH about

URL: https://www.geeksforgeeks.org/machine-learning/deploy-machine-learning-model-using-flask/

⇱ Deploy Machine Learning Model using Flask - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Deploy Machine Learning Model using Flask

Last Updated : 7 Apr, 2026

Deploying a Machine Learning model using Flask enables integration of trained models into web applications for real-time predictions. It allows users to provide input through a simple interface and receive instant results powered by the model. This approach helps transform machine learning solutions into practical, user-accessible systems.

  • Connect backend ML models with frontend interfaces using Flask routes and APIs
  • Handle user input, data preprocessing, and prediction flow within the application
  • Structure and organize the project for smooth deployment and scalability
👁 deploy_machine_learning_model_using_flask
Deploy Machine Learning Model using Flask

1. Environment Setup and Project Structure

Before building and deploying the Flask application, it is important to set up the development environment and organize the project files properly. A clean setup ensures smooth development, easy debugging and better scalability of the application.

Installation and Setup

Start by creating and activating a virtual environment to manage dependencies efficiently. Then install all the required libraries for this project using the following commands:

Project Structure

After completing the project, your directory should be well-organized to separate the model, application logic, and static files. A typical structure looks like this:

👁 MK-flask-filestructure
File Structure
  • app.py: Main Flask application file
  • model.pkl: Saved trained Machine Learning model
  • templates/: HTML files for the user interface
  • static/: CSS, JavaScript, and other static assets
  • requirements.txt: List of project dependencies

A proper structure helps in maintaining the codebase, simplifies deployment, and makes the project easier to understand and extend.

2. Data Preparation and Model Training

To build an effective Machine Learning application, it is essential to properly prepare the dataset and train a reliable model. In this section, we will load the dataset, perform preprocessing and train a Decision Tree Classifier, followed by saving the trained model for deployment.

Step 1: Load and Explore the Dataset

Import the required libraries, load the dataset and preview its structure to understand the features.

Download dataset from here

Output:

👁 Screenshot-2026-03-17-152638
Dataset

Step 2: Handle Missing Values

Replace missing values with NaN and fill them using the most frequent value (mode) of each column.

Step 3: Simplify Categorical Data

Reduce complexity by grouping similar categories into fewer meaningful classes.

Step 4: Encode Categorical Variables

Here converts categorical features into numerical values using Label Encoding and stores the mapping for reference.

  • LabelEncoder() transforms each categorical column into numeric form, and mapping_dict stores the original-to-encoded value mapping
  • df.drop([...], axis=1, inplace=True) removes unnecessary columns that do not contribute to model training

Step 5: Split Features and Target

Separate the dataset into input features (X) and target variable (Y) for training.

Step 6: Train the Model

Split the dataset into training and testing sets, then train the Decision Tree Classifier.

Step 7: Evaluate Model Performance

Check how well the model performs using accuracy.

Output:

Accuracy: 0.8313031016480704

Step 8: Save the Trained Model

Save the trained model so it can be reused in the Flask application.

3. Building the Flask Application and User Interface

Here we create the main Flask application that connects the trained Machine Learning model with a user-friendly web interface. Users can enter their details and see predictions directly on the same page.

Step 1: Create Flask Application (app.py)

The app.py file handles loading the trained model, accepting user inputs, making predictions and rendering results.

  • Loads the saved model.pkl and maps predicted values to readable income categories.
  • Accepts user input via the HTML form and converts it into numeric features.
  • Returns the prediction.

Step 2: Create the Input Form (index.html)

The index.html file collects user data and displays the prediction dynamically.

Step 3: Add Styles (styles.css)

This file improves the UI design making the form visually appealing and readable.

Step 4: Running the Flask Application

Once your Flask app and required files are ready, you can start the web application locally and access it via a browser to make predictions.

  • Open a browser and go to: http://127.0.0.1:5000/
  • Fill in the Income Prediction Form and click Predict to see the results.

Output:

👁 Screenshot-2026-03-17-172851
Web App

Download full code from here

Comment