Sales Prediction Model in Power PI

Leveraging the Python Scripting option in Power BI is a powerful way to build complex machine learning models with the interactive of a dashboard.

For the Python model, the SciKit Learn library to create a Linear Regression model that will have a training and testing set for the model to learn on. Then we will run the model on the total dataset.

We can derive the coefficients and rebuild the linear regression equation using What-If parameters in Power BI.

TV

radio

newspaper

sales

23.01

37.8

69.2

27183

4.45

39.3

45.1

12792

This is a sample of the data set that is going to be used.

In the data above we will be using Sales as our predictor and the 3 channels will make up of our coefficients.

When building your code, its best to use an IDE which will give you the ability to decode the Python script. Spyder is a good lightweight IDE that come with the Anaconda

Get the dataset: Advertisment Dataset

This is the code:

#Load in our essentials
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split

#Load the data and create the data variables
dataset=pd.read_csv(‘advertising_sales.csv’)
X = dataset.iloc[:,1:4] y=dataset[‘Sales’] X_train,X_test,y_train,y_test =train_test_split(X,y)

# Create and fit the model for prediction
lin = LinearRegression()
lin.fit(X_train,y_train)
y_pred =lin.predict(X_test)
error =np.sum(y_test)-np.sum(y_pred)

#Create Coefficients
coef = lin.coef_
components =pd.DataFrame(zip(X.columns,coef),columns=[‘component’,’value’])
components =components.append({‘component’:’intercept’,’value’:lin.intercept_}, ignore_index=True)

Please review the video and the code below. Feel free to ask questions in the comment section below.

Gaelim Holland

Subscribe
Notify of
guest

5 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Saurabh Mishra
Saurabh Mishra
4 years ago

Can you please attach the sample dataset in CSV format.

Sandeep M
Sandeep M
3 years ago

Thanks Gaelim Holland. Just small findings, The code you mentioned in the blog is for HR Data set. Where Can I find/get that data set? Thanks.

Wally
Wally
3 years ago

Great work! Can you share the pbix file

Akhil Racha
Akhil Racha
8 months ago

Can you share the power bi file for the same?