Predicting car prices using MindsDB

Marcel Coetzee
2 min readOct 6, 2022

--

Have you ever wandered how much you should charge for you car, or whether the price you paying for a used car is a fair one?

It’s time to use in-database machine learning to improve your estimates of the true worth of the vehicle in question:

The data

Machine learning needs past data to train one. We will use Car price prediction data from kaggle to train the algorithm on:

Download the data, and use following code to upload it to your Postgres server (I’m using a local instance for this article):

CREATE TABLE public.cars (
car_name varchar NULL,
“Year” integer NULL,
selling_price numeric NULL,
present_price numeric NULL,
driven_kms numeric NULL,
fuel_type varchar NULL,
selling_type varchar NULL,
“owner” integer NULL
);

Once you imported the data using your favorite database-IDE, your table should look like this:

On to machine learning!

Fitting the Model

We want to predict the selling price, given all the other columns, ordered by year and grouped by car name.

To start off, let’s connect our MindsDB instance to our data source. In the mindsDB SQL editor, create the data source:

CREATE DATABASE cars
WITH ENGINE = "postgres",
PARAMETERS = {
"user": "caruser",
"password": "**********",
"host": "localhost",
"port": "5432",
"database": "cars"
};

We train our predictor:

CREATE PREDICTOR mindsdb.pricing_model
FROM cars
(SELECT * FROM public.cars)
PREDICT selling_price;

The estimator will take a while to train. You can view the status with:

SELECT status
FROM mindsdb.predictors
WHERE name='pricing_model';

Finally, let’s predict the prices for each year and car name to see MindsDB’s AutoML in action:

SELECTcar_name,year,Avg(predicted_selling_price) AS avg_predicted_selling_priceFROM(SELECTc.car_name,c.year,m.selling_price AS predicted_selling_priceFROMcars.public.cars AS cJOIN mindsdb.pricing_model AS m) AS predsGROUP BYcar_name,yearORDER BYyear;
Predictions of car prices

Thank you for reading!

--

--