Sales Forecasting using Walmart Dataset using Machine Learning in Python

Forecasting means to predict the future. Forecasting is used to predict future conditions and making plans accordingly. In our daily life, we are using a weather forecast and plan our day activity accordingly. Forecasting is used in many businesses. Here we will learn Sales Forecasting using Walmart Dataset using Machine Learning in Python.

Sales forecasting

Sales forecasting or predicting the future is very important for every business. It is used for companies to making plans for high revenue, keep costs lower and high efficiency.  Companies made short-term and long term future planning as per forecasting data. Based on past data with some assumption which predict future trends and draw their budget accordingly.

There are many factors like Market changes, Product changes, Economic conditions, season changes, etc; which impact to forecast of sales. Companies can make a plane to meet future demands and make improvements in their sales by keeping in mind these various factors.

Sales forecasting using Machine learning

Here, we use the dataset of Walmart sales to forecast future sales using machine learning in Python. Linear regression use to forecast sales. Numpy, Pandas, Sklearn, Scipy, Seaborn Python libraries used in this program. We implement in three steps first to import libraries second by using that libraries prepare data and third forecast.

Step-1 First, importing libraries of Python.

#importing libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from scipy.special import boxcox1p
import seaborn as sns

Step-2 Now, we preparing data. Here we perform four tasks:

  1. Get the data from .csv file.
  2. Merging the data.
  3. Analyze the data.
  4. Manipulating the data.

1. Getting data from .csv file

#Getting data

2. Take important information and merging the data

From train.csv, taking groupby Store, Date and sum Weekly_Sales.

#reseting train

Output: Sales Forecasting using Walmart Dataset using Machine Learning Python

Merging train and features data by inner join.

#merging train and feature

Output:- Sales Forecasting using Walmart Dataset using Machine Learning in Python

Merging Data and Store on Store by inner join.

#marging store with data

Output:- Merging Data and Store on Store by inner join

sorting the data by date.

#sorting values of Data

Output:- data=data.sort_values(by='Date')

3. Analyzing the data

Here, we see different methods to analyze data.

Count plot of Type.

sns.countplot(x="Type", data=data)

Output:- sns.countplot(x="Type", data=data)

Box plot of Type and Weekly_Sales


Output:- sns.countplot(x="Type", data=data)

Histogram of Weekly_sales


Output:- data["Weekly_Sales"].plot.hist()

Count plot of IsHoliday

sns.countplot(x="IsHoliday", data=data)

Output:- sns.countplot(x="IsHoliday", data=data)

Now, we check the null values of data


Output:- data.isnull().sum()

Heatmap is another representation of null values.

sns.heatmap(data.isnull(),yticklabels=False, cmap="viridis")

Output:- Heatmap is another representation of null values

4. Manipulating data. Transform data into useful information and deleting unnecessary items. Getting the final data.

Removing the unnecessary columns.


Output:- Heatmap is another representation of null values

Again checking for null values and verify that data is clean.


Output:- data.isnull().sum()

Heatmap for verifying there are no null values in data.

sns.heatmap(data.isnull(),yticklabels=False, cmap="viridis")

Output:- sns.heatmap(data.isnull(),yticklabels=False, cmap="viridis")

Converting IsHoliday in Holiday which is integer and 1 for holiday and 0 otherwise.

data['Holiday']=[int(i) for i in list(data.IsHoliday)]

Output:- data['Holiday']=[int(i) for i in list(data.IsHoliday)]

Converting type to an integer by one-hot encoding. The first column is also removed because we know of both columns B and C are 0 then it is A-type. So B=1 and C=0 for B.B=0 and C=1 for C.B=0 and C=0 for A.


Output:- Type_dummy=pd.get_dummies(data['Type'],drop_first=True)

Concating type_dummy with data.


Output:- data=pd.concat([data,Type_dummy],axis=1)

We only use integer so we removing non-integer columns after transforming it into integer form.


Output: data=data.drop(['Type','IsHoliday'],axis=1)

Step-3 Now, we perform learning tasks on this data in four steps.

  1. Splitting the train and test data.
  2. Applying linear regression.
  3. Predicting the value
  4. Evaluate the model

1. Splitting data into train and test data. The size of the test data is 30%.

#splitting data in input and output

2. Applying linear regression and fit the training data into it.


3. Predicting the data for test value as per linear regression.


Output:- Predicting the data for test value as per linear regression

4. Evaluating the model by calculating errors by the root mean square error and R -squared.


Output:-Evaluating the model by calculating errors by the root mean square error and R -squared

from sklearn.metrics import r2_score


Now, if we want to predict the weekly sales. Then we give particular tuple to input in the model and predict the weekly sales as output. Here I’m showing an example of that.


Output:- prediction=LR.predict(pd.DataFrame([(40.37,2.876,173.325456,7.934,103464,0,0,0)]))


The dataset we are using is Walmart dataset for sales forecasting. A dataset is available at Kaggle. You can download it from here: Walmart-dataset

In this dataset, there are four following .csv files.

  • features.csv
  • stores.csv
  • train.csv
  • test.csv


In conclusion, the forecast should use for better plans and get more benefit from it. Here we get the following topics.

  • Sale Forecasting
  • Sale forecasting using machine learning in Python.

Leave a Reply

Your email address will not be published. Required fields are marked *