<commit_before>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "aklJxkHBD5aR"
   },
   "source": [
    "# LAB 2:  AutoML Tables Babyweight Training.\n",
    "\n",
    "**Learning Objectives**\n",
    "\n",
    "1. Setup AutoML Tables\n",
    "1. Create and import AutoML Tables dataset from BigQuery\n",
    "1. Analyze AutoML Tables dataset\n",
    "1. Train AutoML Tables model\n",
    "1. Check evaluation metrics\n",
    "1. Deploy model\n",
    "1. Make batch predictions\n",
    "1. Make online predictions\n",
    "\n",
    "\n",
    "## Introduction \n",
    "In this notebook, we will use AutoML Tables to train a model to predict the weight of a baby before it is born.  We will use the AutoML Tables UI to create a training dataset from BigQuery and will then train, evaluate, and predict with a Auto ML Tables model.\n",
    "\n",
    "In this lab, we will setup AutoML Tables, create and import an AutoML Tables dataset from BigQuery, analyze AutoML Tables dataset, train an AutoML Tables model, check evaluation metrics of trained model, deploy trained model, and then finally make both batch and online predictions using the trained model.\n",
    "\n",
    "Each learning objective will correspond to a series of steps to complete in this student lab notebook."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Verify tables exist\n",
    "\n",
    "Run the following cells to verify that we previously created the dataset and data tables. If not, go back to lab [1b_prepare_data_babyweight](../solutions/1b_prepare_data_babyweight.ipynb) to create them."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bigquery\n",
    "-- LIMIT 0 is a free query; this allows us to check that the table exists.\n",
    "SELECT * FROM babyweight.babyweight_augmented_data\n",
    "LIMIT 0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Lab Task #1: Setup AutoML Tables"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 1: Open AutoML Tables\n",
    "Go the GCP console and open the console menu in the upper left corner. Then scroll down to the bottom to get to the `Artificial Intelligence` section. Click on `Tables` to open AutoML Tables."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![AutoML](./assets/1_automl_tables_hamburger_dropdown.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 2: Enable API\n",
    "If you haven't already enabled the AutoML Tables API, then you'll see the screen below. Make sure to click the `ENABLE API` button."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![AutoML](./assets/2_automl_tables_enable_api.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 3: Get started\n",
    "If this is your first time using AutoML Tables, then you'll see the screen below. Make sure to click the `GET STARTED` button."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![AutoML](./assets/3_automl_tables_get_started.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Lab Task #2: Create and import AutoML Tables dataset from BigQuery"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 4: Datasets\n",
    "You should now be on AutoML Table's Datasets page. This is where all imported datasets are shown. We'll want to add our babyweight dataset, so click the `+ NEW DATASET` button."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![AutoML](./assets/4_automl_tables_click_create_dataset.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 5: Create new dataset\n",
    "We need to give our new dataset a unique name. I named mine `babyweight_automl` but feel free to name yours whatever you want. When you are done choosing a unique name, click the `CREATE DATASET` button."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![AutoML](./assets/5_automl_tables_create_new_dataset.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 6: Import your data\n",
    "Now that we've created a dataset, let's import our data so that AutoML Tables can use it for training. Our data is currently already in BigQuery, so we will select the radio button `Import data from BigQuery`. This will give us some text boxes to fill in with our data's `BigQuery Project ID`, `BigQuery Dataset ID`, and `BigQuery Table or View ID`. Once you are done entering those in, click the `IMPORT` button."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![AutoML](./assets/6_automl_tables_import_data.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 7: Wait for your data to be imported\n",
    "AutoML Tables should now be importing your data from BigQuery. Depending on the size of your dataset, this could take a while, so this step is about just waiting and being patient."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![AutoML](./assets/7_automl_tables_importing_data.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 8: Select target column\n",
    "Awesome! Our dataset has been successfully imported! You can now look at the dataset's schema which will show for each column the column name, the data type, and its nullability. Out of these columns we need to select which column is we want to be our target or label column. Clikc the drop down for `Target column` and choose `weight_pounds`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![AutoML](./assets/8_automl_tables_schema_target_column.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 9: Approve target and schema\n",
    "When you successfully choose your target column you will see a green checkmark and the tag target added to the column row on the right. It will also disable its nullability since machine learning doesn't do too well with null labels. Once you've verified everything is correct with your target column and the schema, then click the `CONTINUE` button."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![AutoML](./assets/9_automl_tables_schema_continue.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Lab Task #3: Analyze AutoML Tables dataset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 10: Analyze\n",
    "The next tab we're brought to is `ANALYZE`. This is where some basic statistics are shown. We can see that we have 6 features, 4 of which are numeric and 2 of which are categorical. We can also see that there are 0% missing and 0 invalid values across all of our columns, which is great! We can also see the number of distinct values which we can compare with our expectations. Additionally, the linear correlation with the target column, `weight_pounds` in this instance, is shown as well as the mean and standard deviation for each column. Once you are satisfied with the analysis, then click the `TRAIN` tab."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![AutoML](./assets/10_automl_tables_analyze.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Lab Task #4: Train AutoML Tables model"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 11: Setup training\n",
    "We are almost ready to train our model. It took a lot of steps to get here but those were mainly to import the data and make sure the data is alright. As we all know, data is extremely important for ML and if it is not what we expect then our model will also not perfom as we expect. Garbage in, garbage out. We need to set the `Training budget` which is the maxmimum number of node hours to spend training our model. Thankfully, if improvement stops before that, then the training will stop and you'll only be charged for the actual node hours you used. For this dataset, I got decent results with a budget of just 1 to 3 node hours. We also need to select which features we want to use in our model out of the superset of features by selecting the `Input feature selection` dropdown where we will see details in the next step below. Once all of that is set the click the `TRAIN MODEL` button."
   ]<commit_msg>- Updated the instructions according to AutoML UI.<commit_after>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "aklJxkHBD5aR"
   },
   "source": [
    "# LAB 2:  AutoML Tables Babyweight Training.\n",
    "\n",
    "**Learning Objectives**\n",
    "\n",
    "1. Setup AutoML Tables\n",
    "1. Create and import AutoML Tables dataset from BigQuery\n",
    "1. Analyze AutoML Tables dataset\n",
    "1. Train AutoML Tables model\n",
    "1. Check evaluation metrics\n",
    "1. Deploy model\n",
    "1. Make batch predictions\n",
    "1. Make online predictions\n",
    "\n",
    "\n",
    "## Introduction \n",
    "In this notebook, we will use AutoML Tables to train a model to predict the weight of a baby before it is born.  We will use the AutoML Tables UI to create a training dataset from BigQuery and will then train, evaluate, and predict with a Auto ML Tables model.\n",
    "\n",
    "In this lab, we will setup AutoML Tables, create and import an AutoML Tables dataset from BigQuery, analyze AutoML Tables dataset, train an AutoML Tables model, check evaluation metrics of trained model, deploy trained model, and then finally make both batch and online predictions using the trained model.\n",
    "\n",
    "Each learning objective will correspond to a series of steps to complete in this student lab notebook."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Verify tables exist\n",
    "\n",
    "Run the following cells to verify that we previously created the dataset and data tables. If not, go back to lab [1b_prepare_data_babyweight](../solutions/1b_prepare_data_babyweight.ipynb) to create them."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bigquery\n",
    "-- LIMIT 0 is a free query; this allows us to check that the table exists.\n",
    "SELECT * FROM babyweight.babyweight_augmented_data\n",
    "LIMIT 0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Lab Task #1: Setup AutoML Tables"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 1: Open AutoML Tables\n",
    "Go the GCP console and open the console menu in the upper left corner. Then scroll down to the bottom to get to the `Artificial Intelligence` section. Click on `Tables` to open AutoML Tables."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![AutoML](./assets/1_automl_tables_hamburger_dropdown.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 2: Enable API\n",
    "If you haven't already enabled the AutoML Tables API, then you'll see the screen below. Make sure to click the `ENABLE API` button."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![AutoML](./assets/2_automl_tables_enable_api.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 3: Get started\n",
    "If this is your first time using AutoML Tables, then you'll see the screen below. Make sure to click the `GET STARTED` button."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![AutoML](./assets/3_automl_tables_get_started.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Lab Task #2: Create and import AutoML Tables dataset from BigQuery"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 4: Datasets\n",
    "You should now be on AutoML Table's Datasets page. This is where all imported datasets are shown. We'll want to add our babyweight dataset, so click the `+ NEW DATASET` button."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![AutoML](./assets/4_automl_tables_click_create_dataset.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 5: Create new dataset\n",
    "We need to give our new dataset a unique name. I named mine `babyweight_automl` but feel free to name yours whatever you want. When you are done choosing a unique name, click the `CREATE DATASET` button."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![AutoML](./assets/5_automl_tables_create_new_dataset.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 6: Import your data\n",
    "Now that we've created a dataset, let's import our data so that AutoML Tables can use it for training. Our data is currently already in BigQuery, so we will select the radio button `Import data from BigQuery`. This will give us some text boxes to fill in with our data's `BigQuery Project ID`, `BigQuery Dataset ID`, and `BigQuery Table or View ID`. Once you are done entering those in, click the `IMPORT` button."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![AutoML](./assets/6_automl_tables_import_data.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 7: Wait for your data to be imported\n",
    "AutoML Tables should now be importing your data from BigQuery. Depending on the size of your dataset, this could take a while, so this step is about just waiting and being patient."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![AutoML](./assets/7_automl_tables_importing_data.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 8: Select target column\n",
    "Awesome! Our dataset has been successfully imported! You can now look at the dataset's schema which will show for each column the column name, the data type, and its nullability. Out of these columns we need to select which column is we want to be our target or label column. Clikc the drop down for `Target column` and choose `weight_pounds`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![AutoML](./assets/8_automl_tables_schema_target_column.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 9: Approve target and schema\n",
    "When you successfully choose your target column you will see a green checkmark and the target tag added to the column row on the right. It will also disable its nullability since machine learning doesn't do too well with null labels."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![AutoML](./assets/9_automl_tables_schema_continue.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Lab Task #3: Analyze AutoML Tables dataset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 10: Analyze\n",
    "We can analyze some basic statistics here. We can see that we have 6 features, 4 of which are numeric and 2 of which are categorical. We can also see that there are 0% missing and 0 invalid values across all of our columns, which is great! We can also see the number of distinct values which we can compare with our expectations. Additionally, the linear correlation with the target column, `weight_pounds` in this instance, is shown as well as the mean and standard deviation for each column. Once you are satisfied with the analysis, then click the **TRAIN MODEL**."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![AutoML](./assets/10_automl_tables_analyze.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Lab Task #4: Train AutoML Tables model"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 11: Setup training\n",
    "We are almost ready to train our model. It took a lot of steps to get here but those were mainly to import the data and make sure the data is alright. As we all know, data is extremely important for ML and if it is not what we expect then our model will also not perfom as we expect. Garbage in, garbage out. We need to set the `Training budget` which is the maxmimum number of node hours to spend training our model. Thankfully, if improvement stops before that, then the training will stop and you'll only be charged for the actual node hours you used. For this dataset, I got decent results with a budget of just 1 to 3 node hours. We also need to select which features we want to use in our model out of the superset of features by selecting the `Input feature selection` dropdown where we will see details in the next step below. Once all of that is set the click the `TRAIN MODEL` button."
   ]
{'fraction_non_alphanumeric': 0.12654109589041096, 'fraction_numerical': 0.005593607305936073, 'mean_word_length': 3.180625149129086, 'pattern_counts': {'":': 178, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2501223', 'n_tokens_mistral': 5541, 'n_tokens_neox': 5338, 'n_words': 2152}