Car Price Prediction in Machine Learning

Car Price Prediction in Machine Learning

To understand the Car Price Prediction model concept. Just explore the blog and learn.

In this tutorial we are going to implement a car price prediction of old cars in machine learning. This model predicts the price of the old cars with the help of a few parameters like fuel type, present price, kilometer driven, owner (First owner, second owner). The model is train by following the few steps are as follows:

  1. Import all the required libraries
  2. Perform exploratory data analysis ( check shape of data,  check unique values of some features, check there is Null value or not etc.)
  3. Create new features from existing features
  4. Apply one hot encoding
  5. Data visualization
  6. See the correlation
  7. Data preparation
  8. Apply hyper parameter tuning on RandomForestClassifier
  9. Test the model
  10. Check the accuracy
  11. Save the model

Let’s start :-

First of all import all the required libraries like Pandas, numpy, seaborn, matplotlib, sklearn etc. Then load our old car's data which stores some features to predict the old car price like Fuel_Type, Kms_Driven, Year, Selling_Price etc and display the top 5 rows of old car's data.


then perform the exploratory data analysis. In exploratory data analysis, check the shape of the data which shows that how many rows and columns are present in our old car's data, then check the unique values of some features like Fuel_Type, Seller_Type, Owner etc. Now check that there are Null values present or not in our dataset.


Then describe the data which shows the minimum value, maximum value, standard deviation, count, mean value etc. And drop the Car_Name feature which is no use for build a model. Find the current year for calculating the year difference between the older car's year and the current year to get the how many years the car is old.

Now make a new feature No._of_Year from the existing features Year. This feature is a difference between the current year and the older car's year which returns the how many year car is old in integer numbers (4,5,7,8 etc. ).  Now drop the feature Year which is not longer useful now and display the top 5 rows of data with the help of the head function.


Now apply one hot encoding to convert the categorical features into the numeric features. Basically categorical features are like strings (names). Numeric features are integer numbers (3,6,7,33 etc. ). We apply a one hot encoding because the machine learning model understands only the integer number.

Now visualize the data with the help of the seaborn and seaborn function paireplot().

Now check the correlation with features of car's data.


Now it's time for prepare the data, Divide the data into the independent and dependent features, X  stores all the Independent features and Y stores the dependent feature. Then split the data into the training and testing data. Training data stores the 80% data and Testing data stores the 20% data. Then check the shape of the data and then shuffle it.

Now apply a hyper parameter tuning on a RandomForestRegressor() to tune the model. Define all the parameters of the RandomForestRegressor() and make a dictionary of all the parameters. This dictionary is used to tune the model. Now define my model RandomForestRegressor(). And I used here RandomizedSearchCV to tune the RandomForestRegressor() model.


Then train the model with X_train data and y_train data. Now test the model on the testing data X_test, and check the r2_score with the predicted data (pred) and the actual data (y_test).



Now visualize the actual data and predicted data with the help of the scatter function of matplotlib.

Now display the actual data and predicted data in the form of DataFrame.  And the last step to save my model (car_price_prediction_model.pkl) with the pickle library.


I deploy this model ( Car Price Prediction ) using the Python Framework flask. Flask is easy to use which provides some tools to implement the web application. I implement a simple webpage using HTML, CSS and Bootstrap and I used a flask and Python for the backend.

I used some fields here to predict the price of the old car. Example: if you want to predict the price of the old car so first we need to give the purchasing year, showroom price, kilometer driven, fuel type etc. and then simply click on the calculating the selling price button to see the price of your old car.


Source code and how to use :-

  1. Go to GitHub and Download the project :  Car Price Prediction
  2. Extract the folder and go into the folder.
  3. open a command prompt and go to your project folder with cd command.
  4. Write python app.py in command prompt
  5. You will get a link like ( http:// 127.0.0.1:5000/ )
  6. just copy and paste into any browser.
  7. Now you can use project

Tutorial of Car Price Prediction


Thank You!!!!!!!!




If you have any doubts, Please let me know

Post a Comment (0)
Previous Post Next Post