How to Create Machine Learning Models In Power BI Using Python

Prabhat Pathak
Analytics Vidhya
Published in
3 min readOct 1, 2020

Where Power BI Meets with Purpose

Photo by Luke Chesser on Unsplash

Introduction

Power BI Desktop helps to Solve complex Problem statement with the help of inbuilt and custom visualizations.

Background

The first question is why would you use Python with Power BI? Well, Power BI provides access to a large number of data visualization and data blending capabilities. Python’s data engineering features and frameworks extend Power BI capabilities and also provide capabilities that are not available in Power BI.The challenges are many, but so are the opportunities.Python is a great addition to the Power BI family . We are going to see how to create Machine Learning models in Power BI using Python.

Configuration

To get started you need a anaconda distribution installed and install packages you wish to use. Turn on the Python Support under Power BI preview features (File -> Options and Settings -> Options -> Preview features -> Python Support).

For configuration Please follow this blog, I have mentioned step by step how to configure Anaconda distribution with Power BI for Python scripting.

Loading Data

Importing data into Power BI is fairly simple. In Power BI select Home -> Get Data -> Other -> Python script. The Python scripting window opens and you can enter your code. I will import the ever popular iris datasets formatted as a CSV file with headers.

After running above script, dateset will be loaded . If you want to get familiar with datasets , you can refer this repository .

Final Code that we will run :

import warnings
warnings.filterwarnings('ignore')
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
df=pd.read_csv('C:\\Users\\xxxxxxxxx\\Desktop\\xxxxxx\\New folder\\dataset.csv')
df
X = df[['Age', 'YearsExperience', 'Gender', 'Classification', 'Job']]
X = pd.get_dummies(data=X, drop_first=True)
Y = df[['Salary']]from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.4, random_state=101)
from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(X_train,y_train)
predictions = model.predict(X_test)
predictions
pred=pd.DataFrame(predictions)

After running above script you will get below output :

If you want to get better understanding of Linear regression please refer my previous blogs.

  1. https://medium.com/analytics-vidhya/implementing-linear-regression-using-sklearn-76264a3c073c
  2. https://medium.com/analytics-vidhya/a-beginners-guide-to-linear-regression-in-python-with-scikit-learn-6b0fe70b32d7

Conclusion

Now that you know how to use your Python integration in Power BI, The possibilities to do things endless. Starting from ETL to creating Machine learning modules. If you are thinking why we should run ML code in Power BI, The answer is you can create lots of amazing visualizations easily for better data exploration techniques and with that you can improve the accuracy of the model.

I hope this article will help you and save a good amount of time. Let me know if you have any suggestions.

HAPPY CODING.

Prabhat Pathak (Linkedin profile) is an Senior Analyst.

Photo by Aaron Burden on Unsplash

--

--