# Machine Learning with AutoAI

For this part of the workshop, we'll learn how to use [AutoAI](https://www.ibm.com/support/producthub/icpdata/docs/content/SSQNUZ_current/wsj/analyze-data/autoai-overview.html). AutoAI is a capability that automates various tasks to ease the workflow for data scientists that are creating machine learning models. It automates steps such as preparing your data for modeling, chooses the best algorithm/estimator for your problem, experiments with pipelines and parameters for the trained models.

This section is broken up into the following steps:

1. [Run AutoAI Experiment](#1-run-autoai-experiment)
2. [Save AutoAI Model](#2-save-autoai-model)
3. [Save AutoAI Notebook](#3-save-autoai-notebook)
4. [Promote the Model](#4-promote-the-model)

> _Note: The lab instructions below assume you have a project already with the assets necessary to build a model. If not, follow the instructions in the pre-work section to create a project._

## 1. Run AutoAI Experiment

* Go the (☰) navigation menu and click on the _Projects_ link and then click on your analytics project.

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

* To start the AutoAI experiment, click the _`Add to Project`_ button from the top of the page and select the `AutoAI experiment` option.

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

* Name your AutoAI experiment asset and leave the default compute configuration option listed in the drop-down menu. Then click the `Create` button.

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

* To configure the experiment, we must first give it the dataset that will be used to train the machine learning model. We will be using one of the CSV file datasets we have preloaded into the project. Click on the `Select from project` option.

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

* In the dialog, select the `german_credit_data.csv` file and click the `Select asset` button.

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

* Once the dataset is read in, we will need to indicate what we want the model to predict. Under _Select prediction column_ panel, find and click on the `Risk` row.
* AutoAI will set up defaults values for the experiment based on the dataset and the column selected for the prediction. This includes the type of model to build, the metrics to optimize against, the test/train split, etc. To view/change these values, click the _`Experiment settings`_ button.

![Choose Churn column and run](https://4168210942-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0YPhXPJogwds2wYiEl%2Fsync%2F0c1f56deeb3b6c9a5befa9788b60d6b7705af1ec.png?generation=1626367019323694\&alt=media)

* On the `Data source settings` panel, in the `Select columns to include` section, deselect the checkbox for the `CustomerID` column name. This will remove the customer ID column from being used as a feature for the model. Although we could change other aspects of the experiment, we will accept the remaining default values and click the `Save settings` button.

![Choose features and save](https://4168210942-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0YPhXPJogwds2wYiEl%2Fsync%2F4a4015b9b08fbb10b82dd4c5b26aa284f08e992e.png?generation=1626367008899550\&alt=media)

* To start the experiment, click on the `Run experiment` button.

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

* The AutoAI experiment will now run. AutoAI will run through steps to prepare the dataset, split the dataset into training / evaluation groups and then find the best performing algorithms / estimators for the type of model. It will then build the following series of candidate pipelines for each of the top N performing algorithms (where N is a number chosen in the configuration which defaults to 2):
  * Baseline model (Pipeline 1)
  * Hyperparameter optimization (Pipeline 2)
  * Automated feature engineering (Pipeline 3)
  * Hyperparameter optimization on top of engineered features(Pipeline 4)
* The UI will show progress as different algorithms/evaluators are selected and as different pipelines are created & evaluated. You can view the performance of the pipelines that have completed by expanding each pipeline section in the leaderboard.

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

* The experiment can take several minutes to run. Upon completion you will see a message that the pipelines have been created. Do not proceed to the next section until the experiment completes.

## 2. Save AutoAI Model

* Once the experiment completes, you can explore the various pipelines and options in the UI. Some of the options available are to see a comparison of the pipelines, to change the ranking based on a different performance metric, to see a log of the experiment, or to see the ranked listing of the pipelines (ranking based on the optimization metric in your experiment, in this case accuracy.)

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

* Scroll down to see the _Pipeline leaderboard_. The top performing pipeline is in the first rank.
* The next step is to select the model that gives the best result and view its performance. In this case, Pipeline 4 gave the best result for our experiment. You can view the detailed results by clicking the corresponding pipeline name from the leaderboard:

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

* The model evaluation page will show metrics for the experiment, confusion matrix, feature transformations that were performed (if any), which features contribute to the model, and more details of the pipeline. Optionally, feel free to click through these views of the pipeline details.

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

* In order to deploy this model, click on the _`Save as`_ button. On the next scren, select the `Model` option. Keep the default name or change it, add an optional description and tags, and click `Create` to save it.

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

* You receive a notification to indicate that your model is saved to your project. You can return to your project using the notification by clicking `View in project`, or go back to your project main page by clicking on the project name on the navigator on the top left.

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

* You will see the new model under _Models_ section of the _Assets_ page:

## 3. Save AutoAI notebook

* To save the AutoAI experiment as a notebook, go back to the window for the pipeline you have chosen, and click `Save as`.

> _Note: You can get back to the pipeline window by going to your project overview page, clicking on the AutoAI experiment and clicking the pipeline from the leaderboard_

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

* Choose the `Notebook` tile, accept the default name or change it if you like. Add optional description or tags, and click `Create`.

![Name and create notebook](https://4168210942-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0YPhXPJogwds2wYiEl%2Fsync%2F215b4a16632d985c2bd3114b9b3b75174476dd0b.png?generation=1626367040717217\&alt=media)

* You will receive a notification to indicate that your notebook is saved to your project. Close the pipeline details window to expose the path back to the project at the top of the screen. Click on your project name to navigate to the project overview page.

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

* The notebook will be saved to your project, and can be examined in detail, changed and modified, and used to create a new model. See the documentations for [Modifying and running an AutoAI generated notebook](https://github.com/IBM/credit-risk-workshop-cpd/tree/d5a54b6d53ac2071c9bc8349016ef2fb2aca3c4b/workshop/machine-learning-autoai/running-autoai-notebook.md) for details.

## 4. Promote the model

* Now that we have saved our model, we will next need to make the model available in our deployment space so it can be deployed. Under the _Models_ section of the _Assets_ page, click the name of your saved model.

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

* To make the model available to be deployed, we need to make it available in the deployment space we previously set up. Click on the `Promote to deployment space`:

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

> _\*\*Note: This is assuming you have already created a deployment space in the_ pre-work\* section of the workshop.

* Add an optional description or tags if you'd like. Click on the _`Promote`_ button.

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

* You will see a notification that the model was promoted to the deployment space succesfully.

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

## Conclusion

In this section we covered one approach to building machine learning models on Cloud Pak for Data. We have seen how AutoAI helps find an optimal model by automating tasks such as:

* Data Wrangling
* Model Algorithm Evaluation & Selection
* Feature Engineering
* Hyperparameter Optimization.
