# Data Visualization with Data Refinery

Let's take a quick detour to the _Data Refinery_ tool. Data Refinery can quickly filter and mutate data, create quick visualizations, and do other data cleansing tasks from an easy to use user interface.

This section is broken up into the following steps:

1. [Load the data](#1-load-data)
2. [Refine the data](#2-refine-data)
3. [Profile the data](#3-profile-data)
4. [Visualize the data](#4-visualize-data)

> _Note: The lab instructions below assume you have a project already and have data you will refine. If not, follow the instructions in the pre-work and import data to project sections to create a project and assign data to your project._

## 1. Load Data

* Go the (☰) navigation menu and under the _Projects_ section click on _`All Projects`_.

![(☰) Menu -> Projects](https://4168210942-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0YPhXPJogwds2wYiEl%2Fsync%2F0936471463dc1e929c482c8fd1818bff9dbb8021.png?generation=1626366967908447\&alt=media)

* Click the project name you created in the pre-work section.
* From the `Project` home, under the `Assets` tab, ensure the `Data assets` section is expanded or click on the arrow to toggle it and open up the list of data assets.
* Click the merged data data asset _`XXXAPPLICANTFINANCIALPERSONALLOANDATA`_ (the name of the file may vary, `XXX` may be your initials or the initials of the person who granted you data access) to open it. If this is the first time you are opening the data asset, you will be asked to unlock the connection with your personal credentials. Click the `Use your Cloud Pak for Data credentials to authenticate to the data source` checkbox and then click the _`Connect`_ button.

![Authenticate connection](https://4168210942-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0YPhXPJogwds2wYiEl%2Fsync%2F1d9190cbbf4cc64cc3de66aa68ff05ae985b8025.png?generation=1626367038473708\&alt=media)

* Once the preview of the data asset opens, click on the _`Refine`_ button on the top right of the table.

![Launch the action button](https://4168210942-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0YPhXPJogwds2wYiEl%2Fsync%2F2335c2fb55ac6ee4e9b434ec6585c532981a5bfd.png?generation=1626366975785424\&alt=media)

* Data Refinery will launch and open to the `Data` tab. It will also display the information panel with details of the data refinery flow and where the output of the flow will be placed. Go ahead and click the `X` to the right of the `Information` panel to close it.

![Data Refinery view](https://4168210942-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0YPhXPJogwds2wYiEl%2Fsync%2F8aca945aff1b1df9587c996b7a493f0a98801989.png?generation=1626367023335357\&alt=media)

## 2. Refine Data

We'll start out in the `Data` tab where we wrangle, shape and refine our data. As you refine your data, IBM Data Refinery keeps track of the steps in your data flow. You can modify them and even select a step to return to a particular moment in your data’s transformation.

### Create Transformation Flow

* With Data Refinery, we can transform our data by directly entering operations in [R syntax](https://cran.r-project.org/manuals.html) or interactively by selecting operations from the menu. For example, start typing `filter` on the Command line and observe that the list of operations displayed will get updated. Click on the filter operation.

![Command line filter](https://4168210942-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0YPhXPJogwds2wYiEl%2Fsync%2F2fb9e7de94adbc5f4dc0f399dac81251f9015551.png?generation=1626367039905014\&alt=media)

* A `filter` operation syntax will be displayed in the Command line. Clicking on the operation name within the Command line will give hints on the syntax and how to use the command. For instance, to filter for customers who have paid credits up to date, build the expression shown below. To inact the filter, you would `Apply` the expression.

```r
filter(`CreditHistory` == 'credits_paid_to_date')
```

* We can remove this custom filter by clicking on the trash icon on the `Custom code` step of our data workflow.

![Clear custom filter](https://4168210942-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0YPhXPJogwds2wYiEl%2Fsync%2F71093fb62d0843a75ef0eba2fa258f55ab9a6c61.png?generation=1626367028905797\&alt=media)

* We will use the UI to explore and transform the data. Click the `+Operation` button.

![Choose Operation button](https://4168210942-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0YPhXPJogwds2wYiEl%2Fsync%2Ffdfa7d19b85746a1c5e3b08b7acdd94b5cc41aac.png?generation=1626367036733341\&alt=media)

* Let's use the `Filter` operation to check some values. Click on `Filter` in the left panel.

![Filter Operation](https://4168210942-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0YPhXPJogwds2wYiEl%2Fsync%2Fcf1f8fb59fca7c8d65f8e0cc730be2a524a68628.png?generation=1626367007076109\&alt=media)

* We want to make sure that there are no empty values in the `StreetAddress` column. Select the `StreetAddress` column from the `Column` drop down list, select _`Is empty`_ from the `Operator` drop down list, and then click the `Apply` button.

![Filter is empty](https://4168210942-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0YPhXPJogwds2wYiEl%2Fsync%2F6e34c33faef79bdc94843a1f0e172e326fec8983.png?generation=1626367012124289\&alt=media)

> _Note: If there are records where the selected column is empty, they will be displayed after clicking the apply button. If there are no records for this filter, it means that the rows being sampled do not have any empty values for the selected column._

* Now, click on the counter-clockwise "back" arrow to remove the filter. Alternately, we can also remove the filter by clicking the trash icon for the Filter step in the `Steps` panel on the right.

![Click back arrow](https://4168210942-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0YPhXPJogwds2wYiEl%2Fsync%2F8673d5b0247df87bb0f509842a770676f3516cf0.png?generation=1626367000710538\&alt=media)

* We can remove these records with empty values. Click the `+Operation` again and this time select the `Remove empty rows` operation. Select the `StreetAddress` column, then click the `Next` button and finally the `Apply` button.

![Remove empty rows](https://4168210942-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0YPhXPJogwds2wYiEl%2Fsync%2F365854eca5b7c3db55adcc2e408c97446ec9dab1.png?generation=1626367022907961\&alt=media)

* Let's say we've decide that there are columns that we don't want to leave in our dataset ( maybe because they might not be usefule features in our Machine Learning model, or because we don't want to make those data attributes accessible to others, or any other reason). We'll remove the `FirstName`, `LastName`, `Email`, `StreetAddress`, `City`, `State`, `PostalCode` columns.
* For each columnn to be removed: Click the `+Operation` button, then select the `Remove` operation. Click the `Change column selection` option.

![Remove Column](https://4168210942-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0YPhXPJogwds2wYiEl%2Fsync%2Fa724d86c3e893ab8001e560366acd0e8db5e85c9.png?generation=1626367014276651\&alt=media)

* In the `Select column` drop down, choose one of the columns to remove (i.e `FirstName`). Click the `Next` button and then the `Apply` button. The columns will be removed. Repeat for each of the above columns.
* At this point, you have a data transformation flow with 8 steps. As we saw in the last section, we keep track of each of the steps and we can even undo (or redo) an action using the circular arrows. To see the steps in the data flow that you have performed, click the `Steps` button. The operations that you have performed on the data will be shown.

![Flow](https://4168210942-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0YPhXPJogwds2wYiEl%2Fsync%2F9781525c90889598f382e50c786617e44775ce04.png?generation=1626367036574788\&alt=media)

* You can modify these steps in real time and save for future use.

### Schedule Jobs

Data Refinery allows you to run jobs at scheduled times, and save the output. In this way, you can regularly refine new data as it is updated.

* Click on the "jobs" icon and then `Save and create job` option from the menu.

![Click jobs icon](https://4168210942-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0YPhXPJogwds2wYiEl%2Fsync%2F84af473d7f2654ccdd53bf93417ea8c8689af7d4.png?generation=1626367006514882\&alt=media)

* Give the job a name and optional description, then click the _`Next`_ button.

![jobs name](https://4168210942-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0YPhXPJogwds2wYiEl%2Fsync%2F5ddaf88631e2286bbfe5cbbe5332c562a154bfa1.png?generation=1626367016784641\&alt=media)

* The job will configure a default input and output data asset, as well as the runtime environment. Click the _`Next`_ button.

![jobs name](https://4168210942-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0YPhXPJogwds2wYiEl%2Fsync%2Fa055de301cb79ee1e9647beba35cb0841b5f675f.png?generation=1626366988877105\&alt=media)

* We can set the job to run on a schedule. For now, leave the schedule off and click the _`Next`_ button.

![jobs name](https://4168210942-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0YPhXPJogwds2wYiEl%2Fsync%2F68afc7b2ea60f271bc9f4e95515965bf299b4c33.png?generation=1626367013388051\&alt=media)

* Click the _`Create and Run`_ button to save and run this job.

![Create and Run Refinery job](https://4168210942-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0YPhXPJogwds2wYiEl%2Fsync%2F87de32c78e56d86bdcfdd757f49600096f16ace0.png?generation=1626367015477825\&alt=media)

* This refinery flow will be saved to your project in the `Data Refinery flows` section of the project overview page. From that section you could revisit the flow to edit the steps or even see any execution jobs you have run. For now, we will move on to exploring our data.

## 3. Profile Data

* Back on the top level of the data refinery view, click on the `Profile` tab to bring up a view of several statistics and histograms for the attributes in your data.

![Data Refinery Profile tab](https://4168210942-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0YPhXPJogwds2wYiEl%2Fsync%2F7a5b4d55eea406a5b4dda4a9bf6e5415f3008e29.png?generation=1626367007890625\&alt=media)

* Once the data profile loads, you can get insight into the data from the views and statistics:
  * The median age of the applicants is 36, with the bulk under 49.
  * About as many people had credits\_paid\_to\_date as prior\_payments\_delayed. Few had no\_credits.
  * The median was 3 years for duration at current residence. Range was 1-6 years.

## 4. Visualize Data

Let's do some visual exploration of our data using charts and graphs. Note that this is an exploratory phase and we're looking for insights in out data. We can accomplish this in Data Refinery interactively without coding.

* Choose the `Visualizations` tab to bring up the page where you can select columns that you want to visualize. Select `LoanAmount` from the "Columns to visualize" drop down list as the first column and click `Add another column` to add another column. Next add `LoanDuration` and click the _`Visualize data`_ button. The system will pick a suggested plot for you based on your data and show more suggested plot types at the top.

![Select columns](https://4168210942-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0YPhXPJogwds2wYiEl%2Fsync%2Fc387c814da011519d422c28ee29902926af0ea51.png?generation=1626367005563566\&alt=media)

* Remember that we are most interested in knowing how these features impact a loan being at the risk. So, let's add the `Risk` as a color on top of our current scatter plot. That should help us visually see if there's something of interest here. From the left panel, click the `Color Map` drop down and select `Risk`. Also, to see the full data, drag the right side of the data selector at the bottom all the way to the right, in order to show all the data inside your plot.

![Amount v Duration Scatter](https://4168210942-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0YPhXPJogwds2wYiEl%2Fsync%2Ff0dde8bd571229364938c7d7862e4e9d91ac42cf.png?generation=1626367005479678\&alt=media)

* We notice that there are more blue (risk) on this plot towards the top right, than there is on the bottom left. This is a good start as it shows that there is probably a relationship between the riskiness of a loan and its duration and amount. It appears that the higher the amount and duration, the riskier the loan. Interesting, let's dig in further in how the loan duration could play into the riskiness of a loan.

> _Note: The colors used in your visualization may be different. Be sure to look at chart legend for clarification_

* Let's plot a histogram of the `LoanDuration` to see if we can notice anything. First, select `Histogram` from the `Chart Type`.
* On the left, select `LoanDuration` for the 'X-axis', select `Risk` in the 'Split By' section, check the `Stacked` option, uncheck the `Show kde curve` toggle, uncheck the `Show distribution curve` toggle. You should see a chart that looks like the following image.

![Visualize loan duration](https://4168210942-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0YPhXPJogwds2wYiEl%2Fsync%2Fa95a501b71fcfc503c95ca92e1919114056915da.png?generation=1626367011014371\&alt=media)

* It looks like the longer the duration the larger the blue bar (risky loan count) become and the smaller the dark blue bars (non risky loan count) become. That indicate loans with longer duration are in general more likely to be risky. However, we need more information.
* We next explore if there is some insight in terms of the riskiness of a loan based on its duration when broken down by the loan purpose. To do so, let's create a Heat Map plot.
* At the top of the page, in the `Chart Type` section, open the arrows on the right, select `Heat Map`.

![Switch to heat map](https://4168210942-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0YPhXPJogwds2wYiEl%2Fsync%2Fb9e290d14d6979003f8cf430851c7d71c6284780.png?generation=1626367027359829\&alt=media)

* Next, select `Risk` in the column section and `LoanPurpose` for the `Row` section. Additionally, to see the effects of the loan duration, select `Mean` in the summary section, and select `LoanDuration` in the `Value` section.

![Loan purpose heat map](https://4168210942-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0YPhXPJogwds2wYiEl%2Fsync%2Fa2b0ce051510e2464753c666d2a3346cd7431c60.png?generation=1626367023566175\&alt=media)

* You can now see that the least risky loans are those taken out for purchasing a new car and they are on average 10 years long. To the left of that cell we see that loans taken out for the same purpose that average around 15 years for term length seem to be more risky. So one could conclude the longer the loan term is, the more likely it will be risky. In contrast, we can see that both risky and non-risky loans for the other category seem to have the same average term length, so one could conclude that there's little, if any, relationship between loan length and its riskiness for the loans of type other.
* In general, for each row, the bigger the color difference between the right and left column, the more likely that loan duration plays a role for the riskiness of the loan category.
* Now let's look into customizing our plot. Under the Actions panel, notice that you can perform tasks such as `Start over`, `Download chart details`, `Download chart image`, or set `Global visualization preferences` _(Note: Hover over the icons to see the names)_. Click on the drop down arrow next to `Action`. Then click on the `Global visualization preferences` option from the menu.

![Visualize theme preferences](https://4168210942-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0YPhXPJogwds2wYiEl%2Fsync%2F26a99617e22b8862602f2f2951bdff26fe1b0459.png?generation=1626367038561648\&alt=media)

* We see that we can do things in the `Global visualization preferences` for `Titles`, `Tools`, `Theme`, and `Notifications`. Click on the `Theme` tab and update the color scheme to `Dark`. Then click the `Apply` button, now the colors for all of our charts will reflect this. Play around with various Themes and find one that you like.

![Visualize set theme and choose preferences](https://4168210942-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0YPhXPJogwds2wYiEl%2Fsync%2F46d093f97cd645cbf33d4692baa0f32061ae6667.png?generation=1626367036654681\&alt=media)

## Conclusion

We've seen a some of the capabilities of the Data Refinery. We saw how we can transform data using R code, as well as using various operations on the columns such as changing the data type, removing empty rows, or deleting the column altogether. We next saw that all the steps in our Data Flow are recorded, so we can remove steps, repeat them, or edit an individual step. We were able to quickly profile the data, to see histograms and statistics for each column. And finally we created more in-depth Visualizations, creating a scatter plot, histogram, and heatmap to explore the relationship between the riskiness of a loan and its duration, and purpose.
