Introduction to Machine Learning in Power BI

## Introduction to Machine Learning

Machine learning is a field devoted to understanding and building algorithms that leverage data to improve performance on some set of tasks.  The process by which the model leverages the data to improve its performance is called learning.  During the learning process, a set of parameters are trained using data inputs in order to better predict some outcome (classification) or relationship between two variables (regression).  Machine learning algorithms can be used to learn about a data generating event but are most notably used for their ability to predict the outcome of an event given some data about predictor variables for that event.  In other words, we use some information (x data) to train a model to predict some relationship or outcome (y data).  The three main types of machine learning are Supervised Learning, Unsupervised Learning, and Reinforcement Learning.

## Types of Machine Learning

### Supervised Learning

Supervised machine learning is defined as using labeled datasets to train algorithms or models that classify data or predict outcomes accurately.  Supervised learning algorithms adjust their weights (i.e. train their parameters) based on the training data it is fed during the training process. The training set the algorithm is trained on must contain input data accompanied by the correct outputs (labels). Supervised learning methods can be used to solve classification or regression problems.  Examples of supervised learning algorithms include Neural Networks, Naïve Bayes, Linear Regression, Logistic Regression, Support Vector Machines (SVM), K-Nearest Neighbors, and Random Forests.

### Unsupervised Learning

Unsupervised machine learning is defined as using machine learning algorithms to analyze and cluster unlabeled datasets.  In contrast with supervised learning algorithms, unsupervised algorithms do not require labeled data.  These algorithms are used to discover hidden patterns or data groupings without the need for human intuition and can be used to discover similarities and differences in the information presented in the data.  They can be used for exploratory data analysis (EDA), customer segmentation, image/audio pattern recognition, and much more.  Examples of unsupervised learning algorithms include Clustering, Association Rules, Dimensionality Reduction, Principal Component Analysis (PCA), Singular Value Decomposition, and Autoencoders.

### Reinforcement Learning

Reinforcement machine learning is defined as training a model to map a series of inputs to a series of outputs with dependencies.  During the learning process, the model explores possible actions given a current state of the environment.  Then the algorithm randomly explores the space of state-action pairs and uses a cost or reward function to evaluate its decision and update its parameter weights.  Because reinforcement machine learning solves complex problems and requires a large amount of pre-requisite computer science experience, this algorithm type is outside the scope of PowerBI.

## Machine Learning Tasks

### Binary Prediction

Binary prediction is a task performed by supervised machine learning algorithms.  It is a task where the user is attempting to predict a binary outcome, where some outcome either occurs (1) or does not occur (0).  Given a labeled training dataset with some number of predictor variables (x columns) and a labeled output variable, machine learning is employed to fit to the dataset and provide predictions for the output variable, given data about the predictor variables.  Predictor data can be either categorical or numerical, but categorical data typically must be encoded to a numeric value before training. PowerBI does not require this, because it encodes all categorical values automatically for the user.  Binary prediction models attempt to predict the probability of the output variable being either a 0 or 1 given the data about its predictors and uses a decision rule based on that probability to decide whether the predicted output will be a 0 or 1.  While the output will be numeric (0 or 1), PowerBI decodes this binary event back to text for the user to provide easier interpretability.  The most popular model for performing binary classification is a logit model.

### Multiple Classification

Multiple classification is a task performed by supervised machine learning algorithms.  It is a task similar to binary prediction, but the user is now attempting to predict an output variable with more than two possible outcomes.  For example, given some data about a flower, we may want to predict what type of flower it is.  Because it could be a sunflower, rose, tulip, or some other type of flower, we have more than two classes we are attempting to sort our data in.  One important thing to note is that in multiple classification, each sample in your data can only have one class (a flower can’t be both a sunflower AND a tulip.)  Predictor data can be either categorical or numerical, but categorical data typically must be encoded to a numeric value before training.  PowerBI does not require this, because it encodes all categorical values automatically for the user.  There are many different models that can be used to perform multiple classification, but most algorithms provide the user with output probabilities.  These output probabilities tell the user the probability a data sample is a member of a certain class (in our example, we would be provided with the probability that a sample flower is a tulip, rose, sunflower, and so on.  A simple decision rule can then be employed on the resulting probability to ultimately predict the class (flower).

### Regression

Regression is a method of analysis that allows a user to make conclusions about the relationship between two or more numerical variables.  For example, we may want to see how well human height explains the weight of that same human.  Regression is often considered to be a statistical computation method, but it is also technically a machine learning algorithm because it trains parameters to achieve a line of best fit for the data using a method called optimization.  The two most popular forms of optimization are ordinary least squares (where we find the line of best fit by minimizing the distance between the data and the line), and maximum likelihood estimation (estimating the parameters of our best fit line such that they maximize the likelihood that the data generating event described by the model produced the data in our dataset). The parameters that are trained are the intercept and slope of the best fit line.  Regression can be performed on any number of predictors but can predict only one outcome for each sample in our data.

## About the Data

This wiki page uses credit related information obtained from Kaggle. The dataset contains basic bank details of people with a goal to segregate people into credit score brackets.

Data Source: https://www.kaggle.com/datasets/parisrohan/credit-score-classification

There are two datasets available in the above link - train and test data. While the train data has the credit score (Credit\_Score column) available for each person (classified as Good, Standard or Poor), the test data does not have this column. Our aim was to build a machine learning model in Power BI that could classify the credit score based on a person’s credit-related information and also to use AI visualizations. As part of the feature selection process, we removed the below columns, most of which were removed either because they had a very large number of categories or they required deeper text analysis:

* Name
* SSN
* Type of Loan
* Credit History Age
* Amount Invested Monthly

## Data Cleaning

### Technology

Data cleaning was performed using R for both the train and test data.

### Numeric Columns

Some of the numeric columns had numbers appended with underscore at the end. We replaced the underscore with blank (e.g.: 24\_ to 24). Additionally, some columns had unusual negative values (e.g.: Some people had number of bank accounts or number of loans mentioned as less than zero). We replaced these values with an appropriate default value of either 0 or 1.

Blank values and positive outliers were replaced with the median value. For example, the age of people whose age was mentioned as below 0 or above 120 years was replaced with the median age value. Furthermore, it seemed quite unusual for a person to have more than 15 credit cards. Hence, a value above 15 was replaced with the median value.

Also, columns like Credit Utilization Ratio and Monthly Balance had values with more than 2 decimal places. To make it easier for our calculations and for better readability, we truncated these numbers to 2 decimal places.

### Text Columns

We replaced text columns containing special characters (e.g.: \_, #) with 'Unknown'. For example, we replaced '\_\_\_\_\_\_\_' in Occupation with 'Unknown'. Also, text containing '#' in Payment Behavior was replaced with 'Unknown'.

## AI Visuals

### Q&A

Q&A visual in Power BI enables users to explore data using natural language and is driven by AI. Along with providing a couple of suggestions, this feature enables users to ask their own custom query and choose the best chart/graph. Q&A visual also has auto-correct tool feature.

### Key Influencers

Key Influencers visual in Power BI uses ML.net behind the scenes. It is a machine learning solution that assists users in making key business decisions faster. Key Influencers rank factors that drive a metric being analyzed.

### Decomposition Tree

Decomposition Tree is an excellent tool for conducting root cause analysis. It is valuable for ad-hoc exploration and enables visualization of data across multiple dimensions. It is not only smart, intelligent and AI driven, but also very intuitive and user-friendly.

## Q&A Demonstration Steps

Many times the quickest way to the answer we are seeking is by simply asking Power BI using natural language with the Q&A feature, such as "What is the average interest rate by credit score". There are certainly other ways to obtain this information, but the Q&A feature can produce quick results.

Upon opening, the Q&A will provide some suggested questions based on the data that has been imported. If none of the suggested questions are helpful, type the question you want answered into the question bar above the suggestions.

Power BI will automatically select what it believes to be the best visual based on the type of data it is displaying. In the question you can have Power BI grab different types of data. You can indicate that you would like the average, or min/max, etc. Additionally, if there is a particular type of visual you want, you can indicate that in the question by putting "as a \_\_\_\_\_\_\_" at the end of your question.

If it is desired to keep the visual created as a regular visual, select the icon to the right of the question bar. This will turn the Q&A visual into a normal visual and you can then change the visual as you see fit.

Lastly, the Q&A tooling feature, which is only available on the desktop app allows users to make the Q&A more user friendly. It allows to input synonyms so that commonly spoken jargon will match the correct column. Similar to synonyms, you can teach the Q&A so that when a user inputs a word in the question bar, there is a definition behind the word. Finally, the tooling feature offers the ability for the creator to create their own suggested questions, replacing the Power BI generated ones.

The customizability, along with the natural language capability of the Q&A feature makes this tool one of the most impactful within the Power BI toolkit.

## Key Influencers Demonstration Steps

Key Influencers visual helps us to understand and expose the individual factors (independent variables) that drive some sort of outcome (dependent variable). The outcome can be categorical or numerical. Key Influencers in AI works with regression models with a purpose to understand the dependence and correlation between the variables.

There are two key fields in the Visualizations tab: "Analyze" and "Explain by".

* "Analyze": This is the outcome that we care about. For our example, we used the Credit Score field from the train dataset which is categorical ('Good', 'Standard', 'Poor'). For the purpose of our demonstration, we chose to analyze the question "What influences Credit Score to be Good?".
* "Explain by": Drag any data field here that you think may influence the outcome. For our example. we chose Occupation, Age and Interest Rate,

**Key Influencers Tab:**

In the Key Influencers visual, there are two main parts.

* The left side has key influencers or factors that Power BI has deemed to be statistically significant and influential in impacting the outcome. By default, these key influencers are sorted by impact. The user can hover over the bubbles to find how much portion of the data the key influencer actually represents. In the below example, there is a key influencer where when the sum of interest rate goes down 8.73, the likelihood of the credit score being good increases by 4.33 times, all else equal.
* The right side has a visual representation of the entire dataset for a given field.

If the factor (key influencer) is based on a continuous field, Power BI may bin those values, just like you would with a histogram into different chunks. Or if there is a clear linear relationship between the key influencer and outcome, Power BI may plot it as a scatter plot with the line of best fit (as shown above).

**Top Segments Tab:**

Top Segments tab runs a cluster analysis behind the scenes. Power BI combines factors into segments or populations that seem to have a very high level of influence. In this example, Segment 1 is defined as a segment where the sum of interest rate is less than or equal to 4. Within this segment, more than 48% of the population has a good credit score as compared to the average of only 17.8%, indicating that this is a very influential segment.

## Decomposition Tree Demonstration Steps

Since the Decomposition Tree is used for ad-hoc analysis, it uses two different inputs; “Analyze” and “Explain by”. “Analyze” is the variable that needs to be analyzed, while “Explain by” is a variable(s) that can explain it.

Once the dimensions are chosen, you can select which dimensions to drill down to first. Once the aggregated measure is displayed on the Decomposition tree pressing the “+” will allow you to expand the tree by adding another dimension. The “High and Low Value” options allow you to drill into the dimension that has the highest value possible and lowest value possible, respectively.

By clicking on the format page, a setting that can be very important depending on what data is being presented is under the “Format Page” tab on the Decomposition tree. By clicking on “Analysis” there is a drop-down box that allows for the AI splits to be “Absolute” or “Relative”. Meaning that “Relative” looks for high values compared to the rest of the dimensions.

## Creating AutoML Models

Log into https://app.powerbi.com/ with an account that has premium license level in Power BI.

### Create a New Workspace

In order to create an AutoML dataflow, we must first create a workspace with premium per user license access. First select Workspaces on the left pane, and click the yellow button to create a workspace.

Enter in the name for the workspace, click "Advanced", and make sure to change the license mode to "Premium per user".

### Create a New Dataflow

On the Workspace homepage, click the "+New" button and select "Dataflow" from the drop down.

Next, click "Add new tables" to start creating your dataflow.

Select your dataset and connect to the data source using the on screen prompts. Be sure to make any necessary transformations to your data using the Power Query editor.

When you are finished, save and close your dataflow. Refresh the data and navigate back to your workspace dataflow.

### Create a New Model

On your dataflow's homepage, select the top tab "Machine learning models".

Click "Get started".

On the next page, choose the data table. Next, choose the column of data you want to train the model to predict. This is called the **outcome column**.

Power BI will suggest the model type based on your outcome column. If your outcome column only has two possible values, it will suggest a Binary Prediction model. If it has more than two possible values, it will suggest a Multiple Prediction model. If both columns are numerical, it may suggest a Regression model.

Next, simply select which columns you'd like to have your model learn on, in an attempt to predict your previously chosen outcome column. Power BI will present to a user if any of the possible predictor columns have low or suspiciously high correlation with your outcome column, to signify columns in your data that may not be suitable for the model.

Finally select how long you would like your model to train for. The longer the training time, the more time the model has to iterate and train potential candidates for your final model.

In practice, the model will train according to the set duration. However, Power BI will use an additional 10-15 minutes to generate an AutoML training report. When the model is done training and the dataflow has completely refreshed, it is time to view the training report to see how our model performs!

### Edit and Fine Tune the Model

Once the training report is viewed, we may wish to change which columns we want our model to use to train itself to predict our outcome column. In this case, we can edit the columns the model will be trained on and retrain the model. This will overwrite the previously trained state of the dataflow, and you will have to wait the additional training time to see the new results and model report.

In order to change and retrain your model, when viewing the training report page, select "Edit Model".

## AutoML Reports

AutoML creates a report to display performance, statistical details about the model, and a training report. These various reports can change depending on the type of machine learning performed.

### Navigating to Your Model Report

In order to navigate to your model report, you must have first trained your model and waited the necessary additional 10-15 minutes for the model report to generate. In order to navigate to your model report:

Navigate to your dataflow, in your created workspace.

Once in the dataflow, you should see a box corresponding to your model. Once the status is "Trained", and the icon of a clipboard is active (no longer greyed out), select it to view your model training report.

### Types of Model Reports

#### Binary Predication Model Report

Performance report shows the performance of True Positives and Negatives, as well as False Positives and Negatives. True Positives and Negatives show the amount of correctly predicted outcomes. The False Positives and Negatives show the incorrectly predicted outcomes.

A key part of the Binary Prediction Performance Report is the Precision and Recall performance which includes a slicer. Depending on the user's need they can adjust the threshold of the model to increase recall or precision.

The Accuracy Report for a Binary Predication Model includes a Cumulative Gains chart and the Received Operating Characteristics (ROC) curve. The Cumulative Gains chart compares the percentage of rows with the target outcome with the percentage of total rows. It provides three lines; Random Guess, Model, and Ideal. The better the model the closer the Model line will be to the Ideal line. The ROC curve shows how well the model predicts positive outcomes as positive and negative as negative. Ideally, a model would predict all true positives with no false positives, however, in reality this is impossible.

#### Classification Model Report

Very similar to the Binary Model report, but does not include a slicer. It will show the precision and recall characteristics. This report will also include top predictors for each class. It also provides the ability to drill-down each class to analyze how the predictions are distributed.

#### Regression Model Reports

The Regression Model report includes a residual error chart that compares the predicted values to the actual values. It also contains a Training Details page as all other reports.

## Applying AutoML Models

Once the model has been run and optimized to ones liking, there is the option to apply the model to a data set that is similar to the model. This can be used to gain further insight into the data. To apply the model, one must click on the training report and at the top right corner there should be a yellow box that says, “Apply model”. Clicking that opens a box that can be used to choose the data set to apply it to, and what to name the model.

Depending on the type of model was run, there may be different options. For example, if it is a binary model, there will be an option to change what value to accept the new values. Once the settings are correct, you can apply this model. It may take some time to finish but after it is finished, there will be new entities called *enriched (model\_name)* and *enriched (model\_name) explanations*.

The difference between the two is that the enriched entity shows the predicted outputs for the model while the enriched explanation entity shows the top influencers for the model; meaning that it shows which variables had an affect on the output. The enriched entity is used for data visualizations and is able to put it in Power BI desktop to build a dashboard. In order to get the workflow into Power BI desktop, when on the *Home* tab under the data section click on *Get Data* which will bring a drop-down menu. Find the *Dataflows* option and click it, then click the arrow next to *Workspaces* and find the enriched (model\_name) that the dashboard will be created with. Once it is loaded, visualizations can be used.

## References

https://dotnet.microsoft.com/en-us/platform/customers/power-bi

https://learn.microsoft.com/en-us/power-bi/transform-model/dataflows/dataflows-machine-learning-integration#automated-machine-learning-in-power-bi

https://learn.microsoft.com/en-us/power-bi/consumer/end-user-q-and-a

https://learn.microsoft.com/en-us/power-bi/visuals/power-bi-visualization-influencers?tabs=powerbi-desktop

https://learn.microsoft.com/en-us/power-bi/visuals/power-bi-visualization-decomposition-tree

https://www.udemy.com/ - Microsoft Power BI Desktop for Business Intelligence

## Further Useful Wiki Pages on Power BI

Introduction to Power BI: What is Power BI?

Making Visuals Interact

Combining Data Sources

Introduction to Maps

Using DAX

Automated Reports in Power BI

Theming in Power BI