Car price prediction — Decision Tree Regressor
Using Decision Tree Regressor model we would be making a car price prediction model which will predict the value of car by knowing its specs (with source code).
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 —
what is Decision tree ?- an introduction to machine learning algorithms
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 —
codes/carprice.csv at main · aviralb13/codes
You can't perform that action at this time. You signed in with another tab or window. You signed out in another tab or…
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.
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.
you can go check the link for full code
codes/carprice(DTR).ipynb at main · aviralb13/codes
Contribute to aviralb13/codes development by creating an account on GitHub.
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.