This tutorial will show you how to use MindsDB to create a time series machine-learning model via historical data from retail sales in Brazil
Introduction
Machine learning focuses on using data and algorithms to imitate the way people learn, gradually improving its own accuracy along the way. Machine learning provides businesses with the tools they need to make powerful insights about everything from targeting and marketing to inventory and sales management.
Using machine learning algorithms to forecast sales in the retail industry is a common use of this technology. Sales predictions can help retail businesses prepare for periods of higher demand by stocking up on inventory, adjusting prices, and increasing staff. Without these forecasts, retailers run the risk of being ill-prepared with low supply during periods of high demand, and vice versa.
Setup
This tutorial will show you how to use MindsDB to create a time series sales forecasting machine learning model using sales data from Brazil that was recorded on a day-to-day basis from 2014–2016.
If you’d like to follow this tutorial, you can use the MindsDB Cloud Editor (create your free MindsDB Cloud account here) to create and train your time series prediction model. You may also download the dataset we will be using, retail_sales_data.csv, from Kaggle.
STEP 1 — Import your data
Visit the MindsDB Cloud Editor in your browser and click, ‘Add Data’.
Next, select the “Files” tab.
Click on “Import File” to upload the .csv file from your device. (Note: Make sure to name the table using underscores instead of spaces.)
To double-check the .csv file import, you can run this command. (Note: When you upload data files to MindsDB, they are saved in the files directory by default. Be sure to add files.your_file_name
to your SQL commands)
SHOW TABLES FROM files;
Step 2: Preview The Dataset
Now that you’ve imported the dataset, let’s use SQL to preview its contents.
SELECT * FROM files.retail_sales_data
LIMIT 10;
STEP 3: Create Your Predictor Model
Now that we’ve imported our data, we can start using it to train our predictor. Let’s start by creating our predictor model.
CREATE PREDICTOR mindsdb.retail_sales_predictor
FROM files
(SELECT * FROM retail_sales_data)
PREDICT retail_sale;
Make sure you don’t get any errors in your output here, as this may mean your predictor was not created properly.
STEP 4: Check the Status of the Predictor Model
Before we go any further, let’s check the status of our predictor:
SELECT status
FROM mindsdb.predictors
WHERE name="retail_sales_predictor";
If you run this command and the output reads, “training”, it means your model is still training. Give it a few minutes and run the command again. Do not proceed until your output reads complete!
If you find the output continually reads, “generating” you may have an error in your CREATE PREDICTOR
syntax.
STEP 5: Start Making Predictions
Now that you’ve trained your model, we can start predicting future sales! The SQLSELECT
statement allows us to make predictions based on input variables & input columns.
Let’s first check to see what our prediction model has gathered about previous sales. Our imported historical dataset provides daily time-series insights into retail sales in Brazil from 2014–2016.
Let’s make sure our predictor is in the right ballpark when we input a date that’s included in our retail_sales_data table.
Looks good. The outputted sales figure is consistent with the number of sales that year.
Let’s add some more variables and see what the predictor outputs for the year 2016 if the retail price was $1.39:
That seems pretty accurate to me.
Now let’s fast-forward a few years and change a few factors. Let’s find out what retail sales will be in the year 2040 if inventory is very low:
Now, let’s bump up the inventory for that same year:
Wow! It looks like the predictor understands that higher stock will lead to higher sales!
Conclusion
As can be seen in this tutorial, MindsDB’s sophisticated software gives us the power to create and train a time-series machine-learning model that forecasts future figures with ease. By inputting different variables and factors, retail businesses in Brazil and all over the world can use this technology to boost revenue and sales.
Check it out and get started today!