![]() |
Docker is a set of platforms as a service (PaaS) products that use Operating system-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their software, libraries, and configuration files; they can communicate through well-defined channels. Linear regression is a supervised machine learning algorithm that computes the linear relationship between a dependent variable and one or more independent features. We can deploy an ML model as a Web Application on Docker. Deploying a Linear Regression ML model as a web application on Docker involves several steps.
Deploying Linear Regression ModelPrerequisites: Software and Tools
Python Libraries:Ensure you have the following Python libraries installed. These can be installed via pip using pip install library-name:
Structure of the projectYour project structure should look like this, The Dockerfile should be saved without any extensions and the HTML file should be saved under the templates folder. /your-flask-app Train the Linear Regression ModelThe first step in deploying a machine learning model as a web application is to train the model itself. For this project, we use the Boston housing dataset, focusing on three significant features: the average number of rooms per dwelling (RM), the percentage of the lower status of the population (LSTAT), and the per capita crime rate by town (CRIM). These features are chosen for their strong influence on housing prices and their ease of understanding by users. The two lines involving Using Python’s sklearn library, we split the dataset into training and testing sets to validate the model’s performance. The linear regression model from sklearn.linear_model is trained on the training set. After training, the model is saved to disk using Pickle, allowing us to load it into our Flask application later. Python3
Build the Flask Web ApplicationWith the model trained, the next step is to create a Flask web application that can use the model to make predictions. The Flask app includes a simple route that renders an HTML form where users can input values for RM, LSTAT, and CRIM. Another route accepts POST requests from this form, uses the model to predict the Boston housing price based on the input values, and displays the prediction. index.htmlHTML
Save the HTML file in the templates folder. app.pyHere, the Flask app deploys a trained linear regression model to predict house prices. The model, loaded from ‘model.pkl’, is applied when the user submits a form on the website. In this Flask web application, the predict route is defined to handle POST requests. When a user submits a form on the web page, it sends a POST request to the ‘/predict’ endpoint. The values from the form are extracted, converted to floats, and reshaped into a NumPy array to ensure that the input data has the correct format for making predictions using the trained model. The The trained linear regression model (model) then predicts the house price based on these features. Finally, the predicted price is rendered on the web page using the ‘index.html’ template. The result is then displayed on the webpage. The app runs on the local server when executed. Python3
The Flask app is straightforward, with app.py serving as the main file. It loads the pickled model, defines routes for the homepage (with the form) and the prediction endpoint, and uses templates for the HTML content. Dockerize the Flask AppTo ensure that our Flask application is easy to deploy and runs consistently across different environments, we dockerize it. Dockerizing involves creating a Dockerfile that specifies the environment, dependencies, and commands needed to run the app. Dockerfile
Requirements.txt
The Dockerfile for our Flask app is based on python:3.8-slim, installs dependencies from a requirements.txt file, copies the application files into the container, and sets the command to run the Flask app. This setup encapsulates the application and its environment, making deployment seamless. Note: Save the Dockerfile with no extension selected. Build and Run Docker ImageWith the Dockerfile in place, we build the Docker image using the command executed in the directory containing the Dockerfile and application code.
This command creates a Docker image named linear-regression-web-app. ![]() creating a Docker image After building the image, we run it with
This command starts a container from the image, mapping port 5000 inside the container to port 5000 on the host, allowing us to access the Flask app by visiting http://localhost:5000 in a web browser. ![]() starting container from the image Test the ApplicationThe final step is to test the web application to ensure it works as expected. This involves navigating to http://localhost:5000, entering values for RM, LSTAT, and CRIM into the form, and submitting it. The application should display the predicted housing price based on the input values. ![]() http://localhost:5000 ConclusionDeploying a Linear Regression model as a web application involves training the model, creating a Flask application to use the model, dockerizing the Flask app for easy deployment, and testing to ensure everything works as expected. This process demonstrates how machine learning models can be made accessible and useful in real-world applications, providing valuable insights or predictions based on user input. |
Reffered: https://www.geeksforgeeks.org
AI ML DS |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 15 |