Used Car Price Prediction Web deployment and Key learnings

Building a Web App for Used Car Price Prediction Using Streamlit

In the previous blog, we built a machine learning model to predict used car prices based on various features such as company, car model, year, fuel type, and kilometers driven. In this post, we will take the next step and build an interactive web application using Streamlit that will allow users to input car details and get an estimated price prediction.

Streamlit Setup:

Streamlit is an open-source app framework in Python that enables you to create beautiful, interactive web applications with minimal effort. By using Streamlit, we can easily deploy our trained machine learning model and build a user-friendly interface for making predictions.

Let's walk through the process of creating the web application.

1. Importing Required Libraries

We start by importing the necessary libraries—Streamlit for the web app, Pandas for data manipulation, NumPy for numerical operations, and scikit-learn for machine learning functions.

import streamlit as st
import pandas as pd
import pickle
import numpy as np
import sklearn 

2. Loading the Model

Next, we load the trained model that we saved earlier using the pickle module.

The model is stored in a file named Model.pkl.

model=pickle.load(open('Model.pkl','rb'))

3. Reading the Car Dataset

We read the cleaned car dataset, which contains information such as car companies,

models, year, fuel type, and the kilometers driven. This data will be used to

generate the select box options in the app.

car=pd.read_csv('Cleaned_cars.csv')
companies=sorted(car['company'].unique())
car_models=sorted(car['name'].unique())
year=sorted(car['year'].unique(),reverse=True)
fuel_type=car['fuel_type'].unique()

4. Creating the Streamlit Interface

Now, we set up the user interface using Streamlit's widgets. The following steps

create the form where the user can input the car details for prediction:

  • Select the car's company from a dropdown list.
  • Select the car model based on the selected company.
  • Select the year the car was bought.
  • Choose the fuel type.
  • Enter the kilometers driven by the car.
st.title("Car Price Predictor")

car_company = st.selectbox("Which company does your car belong to?", companies)
car_model = st.selectbox("Which car model is it?", car[car['company']==car_company]
['name'].unique())
car_year = st.selectbox("Which year was the car bought?", year)
car_fuel_type = st.selectbox("Fuel type?", fuel_type)
kms_driven = st.number_input('kms_driven')

5. Making Predictions

Once the user inputs the car details, they can click the Predict button to

generate the car's estimated price. The inputs are passed to the trained model,

and the model predicts the price based on the features.

if st.button('Predict'):
    prediction=model.predict(pd.DataFrame(columns=['name', 'company', 'year',
'kms_driven', 'fuel_type'],
                                          data=np.array([car_model,car_company,
car_year,kms_driven,
car_fuel_type]).reshape(1, 5)))
    st.text("The car may be sold for approximately Rs. "+str(int(prediction[0])))

6. Running the App

With the model loaded, the user interface set up, and the prediction functionality

added, it's time to run the application. To start the Streamlit app, save the

script and run the following command in your terminal:

streamlit run Website.py

This will launch a local server, and you can interact with the web app through

your browser.


Example Output

Here’s how the app might look when deployed

1. User Input Section

Select the car's company, model, fuel type, and year of purchase, then enter details such as kilometers

driven and fuel type to predict the car's price.

2. Prediction Result

  • On clicking "Predict," the app will display the estimated house price

Key Learnings

Data Cleaning and Preprocessing is Essential

Handling Missing Values: Ensuring that missing data is handled
appropriately, such as filling in missing values for essential attributes,
helps ensure a robust model.

Outlier Detection: Removing outliers in features like 'kms_driven' or 'year'
is essential for improving the accuracy and robustness of predictions.
Feature Engineering: Creating new features or transforming existing ones,
like encoding categorical variables using OneHotEncoder, enhances the model’s
predictive power.

Exploratory Data Analysis (EDA) Reveals Insights

Visualizations like box plots,
strip plots, and scatter plots help uncover relationships between car features
(e.g., fuel type, mileage, year) and their prices, guiding feature selection.
Correlations and patterns identified during EDA, such as the relationship between
'kms_driven' and price, can influence the way features are processed and selected
for model building.

Choosing the Right Model is Key

Model Comparison: Testing multiple algorithms like
Linear Regression, Random Forest, and XGBoost helped in identifying the most
accurate model for car price prediction.
Random Forest: Among the various models, Random Forest performed the best,
providing the highest R² score and making it the ideal model for the final
application.

Model Deployment with Streamlit Simplifies User Interaction

Integration: The trained
model was saved using pickle and integrated into a Streamlit app for easy real-time
predictions.

User-Friendly Interface: Streamlit’s interactive elements like dropdown menus,
input fields, and buttons make the application intuitive and accessible for users.

Reproducibility Enhances Workflow:

By saving the model pipeline, preprocessing steps,
and other critical configurations, the project can be easily reproduced or adapted
for new datasets or features in the future.

Web Application Deployment Makes the Model Accessible:

Deploying the app on
Streamlit Cloud or similar platforms ensures that users can easily access and
utilize the model, allowing for real-world application of the car price prediction
tool.

Conclusion

This project helped to reinforce concepts in machine learning model development,

data preprocessing, and the practical application of AI techniques to real-world

problems. Additionally, it showcased how machine learning models can be integrated

into accessible web applications, making predictions available to users in an

interactive way.

Comments