Car price prediction — Decision Tree Regressor

as in the previous article, I have given you an introduction to Decision Tree now I will tell you how to make a Decision Tree Regressor model in this article with some lines of codes.

if you want to know about the decision tree model click on this link —

so let’s start

the first step is we need to download the dataset and then apply the dataset to the model. you can download or copy data from the URL —

Importing the libraries

Now we will import pandas and NumPyas shown below. If your system does not have these libraries installed, you may get them using the pip command.

Data preparation

Now we’ll read the data using Pandas and save it in a variable called dataso we don’t have to call it again and again. Using the head command, we can view the first 5 components of the data; if you want to see more, enter the number inside the bracket.

data.columns will give a brief about all the columns in dataset.

now as some data columns are strings(words) so first we have to convert them into integers and we will do it by encoding techniques by using a label encoder

so we have to encode 3 columns () — drivewheel, carbody, and cylinder number

Defining X and Y

And now I have created a list in which I think it will be the decidingfactor for a car price i.e. carbody, drivewheel, cylindernumber, curbweight, horsepower, peakrpm, citympg, and highwaympg and assign it to variable features now I will pass features in the dataset and store it as x and the price of the car as y.

I believe that these x parameters are more appropriate, and if you want to modify the parameters because you believe they are relevant, you can do so.

Making the model

Splitting the Dataset

We must first import the test train split from sklearn model selection before splitting our model dataset into a train and test dataset.


here we will be making a decision tree regressor model because we want our value to be an integer value and naming our model tree.


nowhere I have made a variable named prediction which will predict the values of our test data.

we also can predict entered values simply by adding the values to the list manually.

here I have entered values









which is the first value of our dataset and the real value is 13495.0 and predicted value is 13495 which is 100% accurate

and also, we can predict values from our dataset i.e. here I am predicting first 5 values from dataset.


Now we’ll look at our model’s accuracy. Our model is 100 percent accurate, which means it correctly predicted 100 values out of a possible 100. Which is excellent.

and our Mean Absolute Error (MAE) is 2608.6153846153848.

source code

you can go check the link for full code


in the article I have given you information and codes on how to make a Car price prediction with source code I would be making more exciting models for you so stay connected.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aviral Bhardwaj

Aviral Bhardwaj


One of the youngest writer and mentor on AI-ML & Technology.