Adventures in Machine Learning

Streamlining Machine Learning Deployment for House Price Prediction

Introduction to Deploying a Machine Learning Model

Machine learning has revolutionized the field of data science. It has allowed businesses to make more informed decisions by analyzing past data and creating models that can predict future outcomes.

However, building a model is only half of the journey. The real test lies in deploying the model so that it can be used in real-world scenarios.

This article will provide an overview of deploying a machine learning model and set up the project structure to make the process simpler.

Setting up the Project Structure

When it comes to deploying a machine learning model, the first step is to set up the project structure. This involves creating a folder with a specific structure that contains all the necessary files and folders for the project.

In this article, we will use three Python scripts named app.py, houseprice_model.py, and predict_cost.py, along with a folder named data that will store the dataset. App.py

The app.py script will contain the main code that will handle the requests and responses.

This script will interact with the model and send/receive data from the user or the database. This script needs to run continuously as it will keep the API running.

Houseprice_model.py

The houseprice_model.py script will contain the model that has been built to predict the house price. This script will load the model, preprocess the input, and return the result.

The data folder will have the dataset files that will be used to train the model. Predict_cost.py

The predict_cost.py script will be used to test the houseprice_model.py script.

This script will load the data, preprocess it, and pass it to the model. The output from the model will be printed on the screen.

Data folder

The data folder will contain all the necessary data files required for the model. This folder will keep all the training and validation sets, as well as the required files for preprocessing the data.

Conclusion

Deploying a machine learning model can be a challenging and time-consuming task, but by setting up the project structure correctly, we can streamline the entire process. Using the above-mentioned Python scripts and creating the data folder, we can easily deploy the machine learning model to our website, application, and other real-world scenarios without any hassle.

3) Developing the Machine Learning Model

When it comes to developing a machine learning model to predict the house prices, we have an enormous range of algorithms we can choose from. Linear Regression is one of the widely used algorithms in the field of machine learning, making it an ideal choice for building models to predict house prices.

In this section, we will develop a Linear Regression model using the House Price dataset, which has five columns: Area of the house, Number of bedrooms, Number of balconies, Age of the house, and Price of the house.

Target and Features Separation

Before we train a model, we need to separate the target variable (Price of the House) and the features (Area of the house, Number of bedrooms, Number of balconies, Age of the house). We can use the pandas library to do this.

“`python

import pandas as pd

# Load the house price dataset into a dataframe

df = pd.read_csv(‘data/house_price.csv’)

# Separate the target and features

y = df[‘Price of the House’]

X = df[[‘Area of the House’, ‘Number of Bedrooms’, ‘Number of Balconies’, ‘Age of the House’]]

“`

Training and Test Set Splitting

We will split the dataset into training and test sets using the train_test_split function from the scikit-learn library. The model will train on the training set, and we will use the test set to evaluate the performance of the model.

“`python

from sklearn.model_selection import train_test_split

# Split the data into training and test sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

“`

Model Building and Saving using joblib module

We can now build our Linear Regression model using the LinearRegression class from scikit-learn. We will fit the model on the training data, and once it’s trained, we will save the model using the joblib module.

“`python

from sklearn.linear_model import LinearRegression

from joblib import dump

# Train the model on the training set

model = LinearRegression()

model.fit(X_train, y_train)

# Save the trained model as a joblib file

dump(model, ‘houseprice_model.joblib’)

“`

Our machine learning model is now ready for deployment, which we will cover in the next section.

4) Designing the Frontend with Streamlit

When it comes to designing the frontend for our machine learning model, we will use the Streamlit library. Streamlit is a Python library for creating interactive web applications for machine learning and data science.

It provides an easy-to-use API that allows us to create stunning user interfaces with just a few lines of code.

Usage and Installation of Streamlit

Streamlit can be easily installed using pip.

“`bash

pip install streamlit

“`

Code for Frontend in app.py

Now, let’s create an app.py file where we can develop the user interface for our machine learning model. In the app.py file, we will define the user interface using the Streamlit API.

“`python

import streamlit as st

from predict_cost import predict

# Create the UI elements

st.write(“# House Price Prediction”)

area = st.slider(“Area of the House (in sq.ft)”, 100, 5000, step=100)

bedrooms = st.slider(“Number of Bedrooms”, 1, 15, step=1)

balconies = st.slider(“Number of Balconies”, 0, 5, step=1)

age = st.slider(“Age of the House (in years)”, 1, 100, step=1)

button = st.button(“Predict”)

# Define the predict function

def predict_price(area: int, bedrooms: int, balconies: int, age: int) -> float:

features = [[area, bedrooms, balconies, age]]

price = predict(features)

return price

# Call the predict_price function on button click and display the result

if button:

price = predict_price(area, bedrooms, balconies, age)

st.write(f”## Estimated House Price: ${price:,.2f}”)

“`

Four Independent Features Required: Area, Number of Bedrooms, Number of Balconies, and Age of the House

In the above code, we defined four independent features that are required for predicting the house price. These features are the area of the house, the number of bedrooms, the number of balconies, and the age of the house.

We used the Streamlit slider function to create a user interface that enables users to select these features quickly and easily.

Display of Predicted House Price on Button Click

Once the user has selected the values for the four features, they can click the “Predict” button, which will call the predict_price function. This function will calculate the predicted house price based on the input, and the estimated house price will be displayed on the screen.

Importing Predict Function from predict_cost.py

In the app.py file, we imported the predict function from the predict_cost.py file. This function will be used to calculate the predicted house price based on the input from the user.

“`python

from typing import List

from joblib import load

import numpy as np

def predict(features: List[List[int]]) -> float:

# Load the trained model

model = load(“houseprice_model.joblib”)

# Make predictions on the input data

X = np.array(features)

prediction = model.predict(X)

# Return the predictions

return prediction[0]

“`

Conclusion

Deploying a machine learning model is not just about writing code; it’s about developing a user-friendly and robust application that can provide valuable insights to users. In this article, we discussed how to set up a project structure, develop the Linear Regression model using the House Price dataset, design the frontend with Streamlit, and integrate it with the machine learning model.

With this knowledge, you can take the next step and build real-world applications that not only predict house prices but can also be used in various other fields. 5) Predict function in predict_cost.py

After developing a machine learning model for predicting house prices, the next step is to implement a function that can take the user inputs and return the predicted house price.

In this section, we will discuss the predict function in predict_cost.py, which is used to make predictions based on the Linear Regression model we built in the previous section.

Loading Saved Model Using joblib Module

The first step in creating the predict function is to load the saved machine learning model using the joblib module. The following line of code loads the saved model.

“`python

from joblib import load

model = load(“houseprice_model.joblib”)

“`

Conversion of Input Data to a NumPy Array

Next, we need to convert the user input data into a NumPy array. The following code is used to convert the input data into a NumPy array.

“`python

import numpy as np

def predict(features):

# Convert input data to a numpy array

X = np.array(features)

# Make the prediction using the loaded model

prediction = model.predict(X)

return prediction[0]

“`

Return of Predicted House Price as 1-d Array

The predict function takes the input features and returns the predicted house price as a 1-d array. The following code is used to get the predicted house price.

“`python

def predict(features):

# Convert input data to a numpy array

X = np.array(features)

# Make the prediction using the loaded model

prediction = model.predict(X)

return prediction[0]

“`

Explanation of the cost[0] Notation for Printing the Value

We use cost[0] notation when we need to print the predicted house price. The reason for this is that the prediction is returned as a 1-d array, and we need to access the first element of the array to get the predicted house price.

The following code is used to print the predicted house price. “`python

def predict(features):

# Convert input data to a numpy array

X = np.array(features)

# Make the prediction using the loaded model

prediction = model.predict(X)

# Return the predicted house price

return prediction[0]

“`

Conclusion

In this section, we discussed the predict function in predict_cost.py, which is used to predict the house price based on the user inputs. We loaded the saved machine learning model using the joblib module, converted the input data into a NumPy array, returned the predicted house price as a 1-d array, and used cost[0] notation for printing the predicted value.

With these techniques, we can easily deploy the machine learning model to our website, application, or any other scenario. This will enable users to predict the house price based on the input values provided, giving them valuable insights into the current housing market.

In conclusion, deploying a machine learning model is critical for businesses and individuals to make informed decisions based on current and future trends. In this article, we discussed setting up the project structure, building a machine learning model using the Linear Regression algorithm and the House Price dataset, designing the frontend using Streamlit, and implementing the predict function in predict_cost.py.

By following these steps, we can deploy the machine learning model to various real-world scenarios. The takeaways from this article are that by using the correct tools and techniques, we can build and deploy machine learning models that can provide valuable insights to users.

Understanding the process of deploying a machine learning model will also enable you to create more complex models suitable for other fields. Whether you’re predicting housing prices or making recommendations based on user data, machine learning can help you make better decisions.

Popular Posts