{ "cells": [ { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "3o8Qof7Cy165" }, "source": [ "# LAB 01: Applying Feature Engineering to BigQuery ML Models \n", "\n", "**Learning Objectives**\n", "\n", "* Setup up the environment\n", "* Create the project dataset\n", "* Create the feature engineering training table\n", "* Create and evaluate the benchmark/baseline model\n", "* Extract numeric features\n", "* Perform a feature cross\n", "* Evaluate model performance\n", "\n", "\n", "## Introduction \n", "In this notebook, we utilize feature engineering to improve the prediction of the fare amount for a taxi ride in New York City. We will use BigQuery ML to build a taxifare prediction model, using feature engineering to improve and create a final model.\n", "\n", "In this lab we set up the environment, create the project dataset, create a feature engineering table, create and evaluate a benchmark model, extract numeric features, perform a feature cross and evaluate model performance.\n", "\n", "Each learning objective will correspond to a __#TODO__ in this student lab notebook -- try to complete this notebook first and then review the [solution notebook](../solution/feateng-solution_bqml.ipynb). **NOTE TO SELF**: UPDATE HYPERLINK." ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "hJ7ByvoXzpVI" }, "source": [ "### Set up environment variables and load necessary libraries" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Your current GCP Project Name is: cloud-training-demos\n" ] } ], "source": [ "%%bash\n", "export PROJECT=$(gcloud config list project --format \"value(core.project)\")\n", "echo \"Your current GCP Project Name is: \"$PROJECT" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "PROJECT = \"cloud-training-demos\" # REPLACE WITH YOUR PROJECT NAME\n", "REGION = \"us-west1-b\" # REPLACE WITH YOUR BUCKET REGION e.g. us-central1\n", "\n", "# Do not change these\n", "os.environ[\"PROJECT\"] = PROJECT\n", "os.environ[\"REGION\"] = REGION\n", "os.environ[\"BUCKET\"] = PROJECT # DEFAULT BUCKET WILL BE PROJECT ID\n", "\n", "if PROJECT == \"your-gcp-project-here\":\n", " print(\"Don't forget to update your PROJECT name! Currently:\", PROJECT)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "mC9K9Dpx1ztf" }, "source": [ "Check that the Google BigQuery library is installed and if not, install it. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 609 }, "colab_type": "code", "id": "RZUQtASG10xO", "outputId": "5612d6b0-9730-476a-a28f-8fdc14f4ecde" }, "outputs": [], "source": [ "!pip freeze | grep google-cloud-bigquery==1.6.1 || pip install google-cloud-bigquery==1.6.1" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "L0-vOB4y2BJM" }, "source": [ "## The source dataset\n", "\n", "Our dataset is hosted in [BigQuery](https://cloud.google.com/bigquery/). The taxi fare data is a publically available dataset, meaning anyone with a GCP account has access. Click [here](https://console.cloud.google.com/bigquery?project=bigquery-public-data&p=nyc-tlc&d=yellow&t=trips&page=table) to acess the dataset.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create a BigQuery Dataset and Google Cloud Storage Bucket \n", "\n", "A BigQuery dataset is a container for tables, views, and models built with BigQuery ML. Let's create one called __feat_eng__ if we have not already done so in an earlier lab. We'll do the same for a GCS bucket for our project too." ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "BigQuery dataset already exists, let's not recreate it.\n", "Bucket exists, let's not recreate it.\n" ] } ], "source": [ "%%bash\n", "\n", "## Create a BigQuery dataset for feat_eng_TEST if it doesn't exist\n", "datasetexists=$(bq ls -d | grep -w feat_eng)\n", "\n", "if [ -n \"$datasetexists\" ]; then\n", " echo -e \"BigQuery dataset already exists, let's not recreate it.\"\n", "\n", "else\n", " echo \"Creating BigQuery dataset titled: feat_eng\"\n", " \n", " bq --location=US mk --dataset \\\n", " --description 'Taxi Fare' \\\n", " $PROJECT:feat_eng\n", " echo \"\\nHere are your current datasets:\"\n", " bq ls\n", "fi \n", " \n", "## Create GCS bucket if it doesn't exist already...\n", "exists=$(gsutil ls -d | grep -w gs://${PROJECT}/)\n", "\n", "if [ -n \"$exists\" ]; then\n", " echo -e \"Bucket exists, let's not recreate it.\"\n", " \n", "else\n", " echo \"Creating a new GCS bucket.\"\n", " gsutil mb -l ${REGION} gs://${PROJECT}\n", " echo \"\\nHere are your current buckets:\"\n", " gsutil ls\n", "fi" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "b2TuS1s9vREL" }, "source": [ "## Create the training data table\n", "\n", "Since there is already a publicly available dataset, we can simply create the training data table. Note the WHERE clause in the below query: This clause allows us to TRAIN a portion of the data (e.g. one million rows versus a billion rows), which keeps your query costs down. \n", "\n", "* Note: The dataset in the create table code below is the one created previously, e.g. \"feat_eng\". The table name is \"feateng_training_data\"." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### **Exercise**: **RUN** the query to create the table." ] }, { "cell_type": "code", "execution_count": 76, "metadata": { "colab": {}, "colab_type": "code", "id": "CMNRractvREL" }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "
" ], "text/plain": [ "Empty DataFrame\n", "Columns: []\n", "Index: []" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "CREATE OR REPLACE TABLE feat_eng.feateng_training_data \n", "AS\n", "SELECT\n", " (tolls_amount + fare_amount) AS fare_amount,\n", " passenger_count*1.0 AS passengers,\n", " pickup_datetime,\n", " pickup_longitude AS pickuplon,\n", " pickup_latitude AS pickuplat,\n", " dropoff_longitude AS dropofflon,\n", " dropoff_latitude AS dropofflat\n", "\n", "\n", "FROM `nyc-tlc.yellow.trips`\n", "WHERE MOD(ABS(FARM_FINGERPRINT(CAST(pickup_datetime AS STRING))), 10000) = 1\n", " \n", " AND fare_amount >= 2.5\n", " AND passenger_count > 0\n", " AND pickup_longitude > -78\n", " AND pickup_longitude < -70\n", " AND dropoff_longitude > -78\n", " AND dropoff_longitude < -70\n", " AND pickup_latitude > 37\n", " AND pickup_latitude < 45\n", " AND dropoff_latitude > 37\n", " AND dropoff_latitude < 45\n", " \n", " " ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "clnaaqQsXkwC" }, "source": [ "## Verify table creation\n", "\n", "Verify that you created the dataset.\n" ] }, { "cell_type": "code", "execution_count": 154, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
fare_amountpassengerspickup_datetimepickuplonpickuplatdropofflondropofflat
\n", "
" ], "text/plain": [ "Empty DataFrame\n", "Columns: [fare_amount, passengers, pickup_datetime, pickuplon, pickuplat, dropofflon, dropofflat]\n", "Index: []" ] }, "execution_count": 154, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "-- LIMIT 0 is a free query; this allows us to check that the table exists.\n", "SELECT * FROM feat_eng.feateng_training_data \n", "LIMIT 0" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "RhgXan8wvREN" }, "source": [ "### Benchmark Model: Create the benchmark/baseline Model\n", "\n", "Next, you create a linear regression baseline model with no feature engineering. Recall that a model in BigQuery ML represents what an ML system has learned from the training data. A baseline model is a solution to a problem without applying any machine learning techniques. \n", "\n", "When creating a BQML model, you must specify the model type (in our case linear regression) and the input label (fare_amount). Note also that we are using the training data table as the data source." ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "kb_5NlfU7oyT" }, "source": [ "#### Exercise: Create the SQL statement to create the model \"Benchmark Model\"." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "2odQpUn_7yCe" }, "outputs": [], "source": [ "%%bigquery\n", "# TODO: " ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "
" ], "text/plain": [ "Empty DataFrame\n", "Columns: []\n", "Index: []" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "# SOLUTION\n", "CREATE OR REPLACE MODEL feat_eng.benchmark_model\n", "\n", "OPTIONS\n", " (model_type='linear_reg',\n", " input_label_cols=['fare_amount']) \n", "AS\n", "\n", "SELECT \n", " fare_amount,\n", " passengers,\n", " pickup_datetime,\n", " pickuplon,\n", " pickuplat,\n", " dropofflon,\n", " dropofflat FROM feat_eng.feateng_training_data" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "Tq2KYJOM9ULC" }, "source": [ "\n", "REMINDER: The query takes several minutes to complete. After the first iteration is complete, your model (benchmark_model) appears in the navigation panel of the BigQuery web UI. Because the query uses a CREATE MODEL statement to create a model, you do not see query results.\n", "\n", "You can observe the model as it's being trained by viewing the Model stats tab in the BigQuery web UI. As soon as the first iteration completes, the tab is updated. The stats continue to update as each iteration completes." ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "HO5d50Eic-X1" }, "source": [ "Once the training is done, visit the [BigQuery Cloud Console](https://console.cloud.google.com/bigquery) and look at the model that has been trained. Then, come back to this notebook." ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "RSgIJqN6vREV" }, "source": [ "### Evaluate the benchmark model\n", "Note that BigQuery automatically split the data we gave it, and trained on only a part of the data and used the rest for evaluation. After creating your model, you evaluate the performance of the regressor using the ML.EVALUATE function. The ML.EVALUATE function evaluates the predicted values against the actual data.\n", "\n", "NOTE: The results are also displayed in the [BigQuery Cloud Console](https://console.cloud.google.com/bigquery) under the **Evaluation** tab." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Exercise: Review the learning and eval statistics for the benchmark_model." ] }, { "cell_type": "code", "execution_count": 115, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
training_runiterationlosseval_losslearning_rateduration_msrmse
00074.43480668.880152None131648.627561
\n", "
" ], "text/plain": [ " training_run iteration loss eval_loss learning_rate duration_ms \\\n", "0 0 0 74.434806 68.880152 None 13164 \n", "\n", " rmse \n", "0 8.627561 " ] }, "execution_count": 115, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "#Eval statistics on the held out data.\n", "SELECT *, SQRT(loss) AS rmse FROM ML.TRAINING_INFO(MODEL feat_eng.benchmark_model)" ] }, { "cell_type": "code", "execution_count": 112, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
mean_absolute_errormean_squared_errormean_squared_log_errormedian_absolute_errorr2_scoreexplained_variance
05.21353568.8801520.2581073.7902490.2260680.226133
\n", "
" ], "text/plain": [ " mean_absolute_error mean_squared_error mean_squared_log_error \\\n", "0 5.213535 68.880152 0.258107 \n", "\n", " median_absolute_error r2_score explained_variance \n", "0 3.790249 0.226068 0.226133 " ] }, "execution_count": 112, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "SELECT * FROM ML.EVALUATE(MODEL feat_eng.benchmark_model)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "xJGbfYuD8a9d" }, "source": [ "**NOTE:** Because you performed a linear regression, the results include the following columns:\n", "\n", "* mean_absolute_error\n", "* mean_squared_error\n", "* mean_squared_log_error\n", "* median_absolute_error\n", "* r2_score\n", "* explained_variance\n", "\n", "**Resource** for an explanation of the regression metrics: [Regression Metrics](https://https://joshlawman.com/metrics-regression/)\n", "\n", "**Mean squared error** (MSE) - Measures the difference between the values our model predicted using the test set and the actual values. You can also think of it as the distance between your regression (best fit) line and the predicted values. \n", "\n", "**Root mean squared error** (RMSE) - The primary evaluation metric for this ML problem is the root mean-squared error. RMSE measures the difference between the predictions of a model, and the observed values. A large RMSE is equivalent to a large average error, so smaller values of RMSE are better. One nice property of RMSE is that the error is given in the units being measured, so you can tell very directly how incorrect the model might be on unseen data.\n", "\n", "**R2**: An important metric in the evaluation results is the R2 score. The R2 score is a statistical measure that determines if the linear regression predictions approximate the actual data. 0 indicates that the model explains none of the variability of the response data around the mean. 1 indicates that the model explains all the variability of the response data around the mean." ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "p_21sAIR7LZw" }, "source": [ "#### Exercise: Write a SQL query to take the SQRT() of the mean squared error as your loss metric for evaluation for the benchmark_model." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%bigquery\n", "# TODO " ] }, { "cell_type": "code", "execution_count": 110, "metadata": { "cellView": "form", "colab": {}, "colab_type": "code", "id": "8mAXRTvbvRES" }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
rmse
08.299407
\n", "
" ], "text/plain": [ " rmse\n", "0 8.299407" ] }, "execution_count": 110, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "#SOLUTION\n", "SELECT SQRT(mean_squared_error) AS rmse FROM ML.EVALUATE(MODEL feat_eng.benchmark_model)\n" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "nW6fnqAW8vyI" }, "source": [ "#### Model 1: EXTRACT DayOfWeek from the pickup_datetime feature.\n", "\n", "* As you recall, DayOfWeek is an enum representing the 7 days of the week. This factory allows the enum to be obtained from the int value. The int value follows the ISO-8601 standard, from 1 (Monday) to 7 (Sunday). " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Exercise: EXTRACT DayOfWeek from the pickup_datetime feature.\n", "* Create a model titled \"model_1\" from the benchmark model and extract out the DayofWeek." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "hkXTdLM--vpj" }, "outputs": [], "source": [ "# TODO" ] }, { "cell_type": "code", "execution_count": 103, "metadata": { "cellView": "both", "colab": {}, "colab_type": "code", "id": "ZQ0kT2jN-vpm" }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "
" ], "text/plain": [ "Empty DataFrame\n", "Columns: []\n", "Index: []" ] }, "execution_count": 103, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "#SOLUTION\n", "CREATE OR REPLACE MODEL feat_eng.model_1\n", "\n", "OPTIONS\n", " (model_type='linear_reg',\n", " input_label_cols=['fare_amount']) \n", "AS\n", "\n", "SELECT \n", " fare_amount,\n", " passengers,\n", " pickup_datetime,\n", " EXTRACT(DAYOFWEEK FROM pickup_datetime) AS dayofweek,\n", " pickuplon,\n", " pickuplat,\n", " dropofflon,\n", " dropofflat FROM feat_eng.feateng_training_data" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "T24XjIJgdLCH" }, "source": [ "Once the training is done, visit the [BigQuery Cloud Console](https://console.cloud.google.com/bigquery) and look at the model that has been trained. Then, come back to this notebook." ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "SRLxpccX_Tin" }, "source": [ "#### Exercise: Create two distinct SQL statements to see the TRAINING and EVALUATION metrics of model_1." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "Km_c9Ioq_Ti3" }, "outputs": [], "source": [ "#Create the SQL statements to extract Model_1 TRAINING metrics. \n", "# TODO: Your code goes here" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Create the SQL statements to extract Model_1 EVALUATION metrics. \n", "# TODO: Your code goes here" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
training_runiterationlosseval_losslearning_rateduration_msrmse
00072.44072488.953232None172518.511212
\n", "
" ], "text/plain": [ " training_run iteration loss eval_loss learning_rate duration_ms \\\n", "0 0 0 72.440724 88.953232 None 17251 \n", "\n", " rmse \n", "0 8.511212 " ] }, "execution_count": 104, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "SELECT *, SQRT(loss) AS rmse FROM ML.TRAINING_INFO(MODEL feat_eng.model_1)" ] }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
mean_absolute_errormean_squared_errormean_squared_log_errormedian_absolute_errorr2_scoreexplained_variance
05.28707688.9532320.2609323.7134390.084810.084811
\n", "
" ], "text/plain": [ " mean_absolute_error mean_squared_error mean_squared_log_error \\\n", "0 5.287076 88.953232 0.260932 \n", "\n", " median_absolute_error r2_score explained_variance \n", "0 3.713439 0.08481 0.084811 " ] }, "execution_count": 108, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "SELECT * FROM ML.EVALUATE(MODEL feat_eng.model_1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#Create the SQL statements to review model_1 training information. " ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "wf-9FBmL_Ti_" }, "source": [ "#### Exercise: Write a SQL query to take the SQRT() of the mean squared error as your loss metric for evaluation for model_1." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "7n2kdbiH_TjA" }, "outputs": [], "source": [ "#Create the SQL statement to EVALUATE Model_1 here. \n", "# TODO: Your code goes here" ] }, { "cell_type": "code", "execution_count": 117, "metadata": { "cellView": "both", "colab": {}, "colab_type": "code", "id": "CsVBzNef_TjC" }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
rmse
09.431502
\n", "
" ], "text/plain": [ " rmse\n", "0 9.431502" ] }, "execution_count": 117, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "#SOLUTION\n", "SELECT SQRT(mean_squared_error) AS rmse FROM ML.EVALUATE(MODEL feat_eng.model_1)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "Lw30UexH8v9P" }, "source": [ "### Model 2: EXTRACT hourofday from the pickup_datetime feature\n", "\n", "As you recall, **pickup_datetime** is stored as a TIMESTAMP, where the Timestamp format is retrieved in the standard output format – year-month-day hour:minute:second (e.g. 2016-01-01 23:59:59). Hourofaday returns the integer number representing the hour number of the given date." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Exercise: EXTRACT hourofday from the pickup_datetime feature.\n", "* Create a model titled \"model_2\"\n", "* EXTRACT the hourofday from the pickup_datetime feature to improve our model's rmse." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "Fa520N8KCIgp" }, "outputs": [], "source": [ "# TODO: " ] }, { "cell_type": "code", "execution_count": 121, "metadata": { "cellView": "both", "colab": {}, "colab_type": "code", "id": "FHeqcYz-B9F1" }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "
" ], "text/plain": [ "Empty DataFrame\n", "Columns: []\n", "Index: []" ] }, "execution_count": 121, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "#SOLUTION\n", "CREATE OR REPLACE MODEL feat_eng.model_2\n", "OPTIONS\n", " (model_type='linear_reg',\n", " input_label_cols=['fare_amount']) \n", "AS\n", "SELECT\n", " fare_amount,\n", " passengers,\n", " #pickup_datetime,\n", " EXTRACT(DAYOFWEEK FROM pickup_datetime) AS dayofweek,\n", " EXTRACT(HOUR FROM pickup_datetime) AS hourofday,\n", " pickuplon,\n", " pickuplat,\n", " dropofflon,\n", " dropofflat\n", " \n", "FROM `feat_eng.feateng_training_data`" ] }, { "cell_type": "markdown", "metadata": { "colab": {}, "colab_type": "code", "id": "u2TU5nG6CiZe" }, "source": [ "#### Exercise: Create two SQL statements to evaluate the model." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# TODO: Your code goes here" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# TODO: Your code goes here" ] }, { "cell_type": "code", "execution_count": 122, "metadata": { "cellView": "both", "colab": {}, "colab_type": "code", "id": "h2yjF6uGCiZh" }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
mean_absolute_errormean_squared_errormean_squared_log_errormedian_absolute_errorr2_scoreexplained_variance
05.25642170.7095180.2623993.8955090.2366680.236768
\n", "
" ], "text/plain": [ " mean_absolute_error mean_squared_error mean_squared_log_error \\\n", "0 5.256421 70.709518 0.262399 \n", "\n", " median_absolute_error r2_score explained_variance \n", "0 3.895509 0.236668 0.236768 " ] }, "execution_count": 122, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "#SOLUTION\n", "SELECT * FROM ML.EVALUATE(MODEL feat_eng.model_2)" ] }, { "cell_type": "code", "execution_count": 123, "metadata": { "cellView": "both", "colab": {}, "colab_type": "code", "id": "bhfabG8XCiZm" }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
rmse
08.408895
\n", "
" ], "text/plain": [ " rmse\n", "0 8.408895" ] }, "execution_count": 123, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "SELECT SQRT(mean_squared_error) AS rmse FROM ML.EVALUATE(MODEL feat_eng.model_2)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "vbOSxv6BDqB-" }, "source": [ "### Model 3: Feature cross dayofweek and hourofday \n", "\n", "First, let’s allow the model to learn traffic patterns by creating a new feature that combines the time of day and day of week (this is called a feature cross). \n", "\n", "* Modify model_2 to create a feature cross that combines the time of day and day of week. Note: CAST DAYOFWEEK and HOUR as strings. Name the model \"model_3\". \n", "\n", "* In this lab, we will modify the SQL to first use the CONCAT function to concatenate (feature cross) the dayofweek and hourofday features. Then, we will use the ML.FEATURE_CROSS, BigQuery's new pre-processing feature cross function.\n", "\n", "Note: BQML by default assumes that numbers are numeric features, and strings are categorical features. We need to convert these features to strings because the Neural Network will treat 1,2,3,4,5,6,7 as numeric values. Thus, there is no way to distinguish the time of day and day of week \"numerically.\"\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Exercise: Create the SQL statement to feature cross the dayofweek and hourofday using the CONCAT function." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "R_i3w3H7FXUW" }, "outputs": [], "source": [ "# TODO: Your code goes here" ] }, { "cell_type": "code", "execution_count": 124, "metadata": { "cellView": "both", "colab": {}, "colab_type": "code", "id": "F7l02C9KFMy7" }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "
" ], "text/plain": [ "Empty DataFrame\n", "Columns: []\n", "Index: []" ] }, "execution_count": 124, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "#SOLUTION\n", "CREATE OR REPLACE MODEL feat_eng.revised_model_3\n", "OPTIONS\n", " (model_type='linear_reg',\n", " input_label_cols=['fare_amount']) \n", "AS\n", "SELECT\n", " fare_amount,\n", " passengers,\n", " #pickup_datetime,\n", " #EXTRACT(DAYOFWEEK FROM pickup_datetime) AS dayofweek,\n", " #EXTRACT(HOUR FROM pickup_datetime) AS hourofday,\n", " CONCAT(CAST(EXTRACT(DAYOFWEEK FROM pickup_datetime) AS STRING), \n", " CAST(EXTRACT(HOUR FROM pickup_datetime) AS STRING)) AS hourofday,\n", " pickuplon,\n", " pickuplat,\n", " dropofflon,\n", " dropofflat\n", " \n", "FROM `feat_eng.feateng_training_data`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Exercise: Create two SQL statements to evaluate the model. " ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
mean_absolute_errormean_squared_errormean_squared_log_errormedian_absolute_errorr2_scoreexplained_variance
05.50002993.2605770.274453.8114640.0802630.080492
\n", "
" ], "text/plain": [ " mean_absolute_error mean_squared_error mean_squared_log_error \\\n", "0 5.500029 93.260577 0.27445 \n", "\n", " median_absolute_error r2_score explained_variance \n", "0 3.811464 0.080263 0.080492 " ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "#SOLUTION\n", "SELECT * FROM ML.EVALUATE(MODEL feat_eng.model_3)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
rmse
09.657152
\n", "
" ], "text/plain": [ " rmse\n", "0 9.657152" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "SELECT SQRT(mean_squared_error) AS rmse FROM ML.EVALUATE(MODEL feat_eng.model_3)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "FbSRbuJ-fYtK" }, "source": [ "### Model 4: Apply the ML.FEATURE_CROSS clause to categorical features\n", "\n", "BigQuery ML now has ML.FEATURE_CROSS, a pre-processing function that performs a feature cross. \n", "\n", "* ML.FEATURE_CROSS generates a STRUCT feature with all combinations of crossed categorical features, except for 1-degree items (the original features) and self-crossing items. \n", "\n", "* Syntax: ML.FEATURE_CROSS(STRUCT(features), degree)\n", "\n", "* The feature parameter is a categorical features separated by comma to be crossed. The maximum number of input features is 10. Unnamed feature is not allowed in features. Duplicates are not allowed in features.\n", "\n", "* Degree(optional): The highest degree of all combinations. Degree should be in the range of [1, 4]. Default to 2.\n", "\n", "Output: The function outputs a STRUCT of all combinations except for 1-degree items (the original features) and self-crossing items, with field names as concatenation of original feature names and values as the concatenation of the column string values." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Exercise: The ML.Feature_Cross statement contains errors. Correct the errors and run the query." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "both", "colab": {}, "colab_type": "code", "id": "Z3U2FxVklrlU" }, "outputs": [], "source": [ "%%bigquery\n", "CREATE OR REPLACE MODEL feat_eng.model_4\n", "OPTIONS\n", " (model_type='linear_reg',\n", " input_label_cols=['fare_amount']) \n", "AS\n", "SELECT\n", " fare_amount,\n", " passengers,\n", " #pickup_datetime,\n", " #EXTRACT(DAYOFWEEK FROM pickup_datetime) AS dayofweek,\n", " #EXTRACT(HOUR FROM pickup_datetime) AS hourofday,\n", " #CONCAT(CAST(EXTRACT(DAYOFWEEK FROM pickup_datetime) AS STRING), \n", " #CAST(EXTRACT(HOUR FROM pickup_datetime) AS STRING)) AS hourofday,\n", " ML.FEATURE_CROSS(CAST(EXTRACT(DAYOFWEEK FROM pickup_datetime) AS STRING),\n", " CAST(EXTRACT(HOUR FROM pickup_datetime) AS STRING) AS hourofday)) AS day_hr,\n", " pickuplon,\n", " pickuplat,\n", " dropofflon,\n", " dropofflat\n", " \n", "FROM `feat_eng.feateng_training_data`\n" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "
" ], "text/plain": [ "Empty DataFrame\n", "Columns: []\n", "Index: []" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "#SOLUTION\n", "CREATE OR REPLACE MODEL feat_eng.model_4\n", "OPTIONS\n", " (model_type='linear_reg',\n", " input_label_cols=['fare_amount']) \n", "AS\n", "SELECT\n", " fare_amount,\n", " passengers,\n", " #pickup_datetime,\n", " #EXTRACT(DAYOFWEEK FROM pickup_datetime) AS dayofweek,\n", " #EXTRACT(HOUR FROM pickup_datetime) AS hourofday,\n", " #CONCAT(CAST(EXTRACT(DAYOFWEEK FROM pickup_datetime) AS STRING), \n", " #CAST(EXTRACT(HOUR FROM pickup_datetime) AS STRING)) AS hourofday,\n", " ML.FEATURE_CROSS(STRUCT(CAST(EXTRACT(DAYOFWEEK FROM pickup_datetime) AS STRING) AS dayofweek,\n", " CAST(EXTRACT(HOUR FROM pickup_datetime) AS STRING) AS hourofday)) AS day_hr,\n", " pickuplon,\n", " pickuplat,\n", " dropofflon,\n", " dropofflat\n", " \n", "FROM `feat_eng.feateng_training_data`\n" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "G6tpoYhcIgs4" }, "source": [ "#### Exercise: Create two SQL statements to evaluate the model." ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "cellView": "both", "colab": {}, "colab_type": "code", "id": "t10fGqSfIgtA" }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
mean_absolute_errormean_squared_errormean_squared_log_errormedian_absolute_errorr2_scoreexplained_variance
05.50002993.2605770.274453.8075480.0802630.080492
\n", "
" ], "text/plain": [ " mean_absolute_error mean_squared_error mean_squared_log_error \\\n", "0 5.500029 93.260577 0.27445 \n", "\n", " median_absolute_error r2_score explained_variance \n", "0 3.807548 0.080263 0.080492 " ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "SELECT * FROM ML.EVALUATE(MODEL feat_eng.model_4)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "cellView": "both", "colab": {}, "colab_type": "code", "id": "uC-gyvAmIgtE" }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
rmse
09.657152
\n", "
" ], "text/plain": [ " rmse\n", "0 9.657152" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "SELECT SQRT(mean_squared_error) AS rmse FROM ML.EVALUATE(MODEL feat_eng.model_4)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "kSsEA8hrEg8t" }, "source": [ "# LAB 02: Applying Feature Engineering to BigQuery ML Models\n", "\n", "**Learning Objectives**\n", "\n", "* Derive coordinate features\n", "* Feature cross coordinate features\n", "* Evalute model performance\n", "* Code cleanup\n", "\n", "\n", "\n", "## Introduction \n", "In this notebook, we derive coordinate features, feature cross coordinate features, evaluate model performance, and cleanup the code." ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "2NAPkAlEEg6C" }, "source": [ "### Model 5: Feature cross coordinate features to create a Euclidean feature\n", "\n", "\n", "Pickup coordinate:\n", "* pickup_longitude AS pickuplon\n", "* pickup_latitude AS pickuplat\n", " \n", "\n", "\n", "Dropoff coordinate:\n", "* #dropoff_longitude AS dropofflon\n", "* #dropoff_latitude AS dropofflat\n", "\n", "**NOTES**:\n", "* The pick-up and drop-off longitude and latitude data are crucial to predicting the fare amount as fare amounts in NYC taxis are largely determined by the distance traveled. Assuch, we need to teach the model the Euclidean distance between the pick-up and drop-off points. \n", "\n", "* Recall that latitude and longitude allows us to specify any location on Earth using a set of coordinates. In our training data set, we restricted our data points to only pickups and drop offs within NYC. NYC has an approximate longitude range of -74.05 to -73.75 and a latitude range of 40.63 to 40.85.\n", "\n", "* The dataset contains information regarding the pickup and drop off coordinates. However, there is no information regarding the distance between the pickup and drop off points. Therefore, we create a new feature that calculates the distance between each pair of pickup and drop off points. We can do this using the Euclidean Distance, which is the straight-line distance between any two coordiante points.\n", "\n", "* We need to convert those coordinates into a single column of a spatial data type. We will use the The ST_Distance function, which returns the minimum distance between two spatial objects. \n" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "kCYniJnejNz0" }, "source": [ "#### Exercise: Derive a coordinate feature.\n", "\n", "* Convert the feature coordinates into a single column of a spatial data type. Use the The ST_Distance function, which returns the minimum distance between two spatial objects.\n", "SAMPLE CODE:\n", "ST_Distance(ST_GeogPoint(value1,value2), ST_GeogPoint(value3, value4)) AS euclidean\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "B6e4JoqrjvdI" }, "outputs": [], "source": [ "# TODO" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "cellView": "both", "colab": {}, "colab_type": "code", "id": "P8mFocaKj9oA" }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "
" ], "text/plain": [ "Empty DataFrame\n", "Columns: []\n", "Index: []" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "#Solution\n", "CREATE OR REPLACE MODEL feat_eng.model_5\n", "OPTIONS\n", " (model_type='linear_reg',\n", " input_label_cols=['fare_amount']) \n", "AS\n", "SELECT\n", " fare_amount,\n", " passengers,\n", " #pickup_datetime,\n", " #EXTRACT(DAYOFWEEK FROM pickup_datetime) AS dayofweek,\n", " #EXTRACT(HOUR FROM pickup_datetime) AS hourofday,\n", " #CONCAT(CAST(EXTRACT(DAYOFWEEK FROM pickup_datetime) AS STRING), \n", " #CAST(EXTRACT(HOUR FROM pickup_datetime) AS STRING)) AS hourofday,\n", " ML.FEATURE_CROSS(STRUCT(CAST(EXTRACT(DAYOFWEEK FROM pickup_datetime) AS STRING) AS dayofweek,\n", " CAST(EXTRACT(HOUR FROM pickup_datetime) AS STRING) AS hourofday)) AS day_hr,\n", " #pickuplon,\n", " #pickuplat,\n", " #dropofflon,\n", " #dropofflat,\n", " ST_Distance(ST_GeogPoint(pickuplon, pickuplat), ST_GeogPoint(dropofflon, dropofflat)) \n", " AS euclidean\n", " \n", "FROM `feat_eng.feateng_training_data`" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "uy7eK6iXlYPu" }, "source": [ "#### Exercise: Create two SQL statements to evaluate the model. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "2n-hmfy7lYP2" }, "outputs": [], "source": [ " # TODO: Your code goes here" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "cellView": "both", "colab": {}, "colab_type": "code", "id": "9atctYyGlYP7" }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
mean_absolute_errormean_squared_errormean_squared_log_errormedian_absolute_errorr2_scoreexplained_variance
03.12131131.2297170.1069232.2181790.6614920.661507
\n", "
" ], "text/plain": [ " mean_absolute_error mean_squared_error mean_squared_log_error \\\n", "0 3.121311 31.229717 0.106923 \n", "\n", " median_absolute_error r2_score explained_variance \n", "0 2.218179 0.661492 0.661507 " ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "SELECT * FROM ML.EVALUATE(MODEL feat_eng.model_5)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "cellView": "both", "colab": {}, "colab_type": "code", "id": "Lk42mvjzlYQE" }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
rmse
05.588356
\n", "
" ], "text/plain": [ " rmse\n", "0 5.588356" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "SELECT SQRT(mean_squared_error) AS rmse FROM ML.EVALUATE(MODEL feat_eng.model_5)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "uUoQmADkmlnV" }, "source": [ "### Model 6: Feature cross pick-up and drop-off locations features\n", "\n", "In this section, we feature cross the pick-up and drop-off locations so that the model can learn pick-up-drop-off pairs that will require tolls.\n", "\n", "This step takes the geographic point corresponding to the pickup point and grids to a 0.1-degree-latitude/longitude grid (approximately 8km x 11km in New York—we should experiment with finer resolution grids as well). Then, it concatenates the pickup and dropoff grid points to learn “corrections” beyond the Euclidean distance associated with pairs of pickup and dropoff locations.\n", "\n", "Because the lat and lon by themselves don't have meaning, but only in conjunction, it may be useful to treat the fields as a pair instead of just using them as numeric values. However, lat and lon are continuous numbers, so we have to discretize them first. That's what SnapToGrid does. \n", "\n", "**REMINDER**: The ST_GEOGPOINT creates a GEOGRAPHY with a single point. ST_GEOGPOINT creates a point from the specified FLOAT64 longitude and latitude parameters and returns that point in a GEOGRAPHY value. The ST_Distance function returns the minimum distance between two spatial objectsa. It also returns meters for geographies and SRID units for geometrics. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Excercise: The following SQL statement is incorrect. Modify the code to feature cross the pick-up and drop-off locations features. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "iVBfDDECoSBD" }, "outputs": [], "source": [ "%%bigquery\n", "#TODO \n", "CREATE OR REPLACE MODEL feat_eng.model_6\n", "OPTIONS\n", " (model_type='linear_reg',\n", " input_label_cols=['fare_amount']) \n", "AS\n", "SELECT\n", " fare_amount,\n", " passengers,\n", " #pickup_datetime,\n", " #EXTRACT(DAYOFWEEK FROM pickup_datetime) AS dayofweek,\n", " #EXTRACT(HOUR FROM pickup_datetime) AS hourofday,\n", " #CONCAT(CAST(EXTRACT(DAYOFWEEK FROM pickup_datetime) AS STRING), \n", " #CAST(EXTRACT(HOUR FROM pickup_datetime) AS STRING)) AS hourofday,\n", " ML.FEATURE_CROSS(STRUCT(CAST(EXTRACT(DAYOFWEEK FROM pickup_datetime) AS STRING) AS dayofweek,\n", " CAST(EXTRACT(HOUR FROM pickup_datetime) AS STRING) AS hourofday)) AS day_hr,\n", " #pickuplon,\n", " #pickuplat,\n", " #dropofflon,\n", " #dropofflat,\n", " ST_AsText(ST_SnapToGrid(ST_GeogPoint(pickuplat,pickuplon,pickuplat), 0.05)), \n", " ST_AsText(ST_GeogPoint(dropofflon, dropofflat,dropofflon), 0.04)) AS pickup_and_dropoff\n", " \n", "FROM `feat_eng.feateng_training_data`\n" ] }, { "cell_type": "code", "execution_count": 142, "metadata": { "cellView": "both", "colab": {}, "colab_type": "code", "id": "7VjZawfZpQ7Y" }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "
" ], "text/plain": [ "Empty DataFrame\n", "Columns: []\n", "Index: []" ] }, "execution_count": 142, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "#SOLUTION\n", "CREATE OR REPLACE MODEL feat_eng.model_6\n", "OPTIONS\n", " (model_type='linear_reg',\n", " input_label_cols=['fare_amount']) \n", "AS\n", "SELECT\n", " fare_amount,\n", " passengers,\n", " #pickup_datetime,\n", " #EXTRACT(DAYOFWEEK FROM pickup_datetime) AS dayofweek,\n", " #EXTRACT(HOUR FROM pickup_datetime) AS hourofday,\n", " ML.FEATURE_CROSS(STRUCT(CAST(EXTRACT(DAYOFWEEK FROM pickup_datetime) AS STRING) AS dayofweek,\n", " CAST(EXTRACT(HOUR FROM pickup_datetime) AS STRING) AS hourofday)) AS day_hr,\n", " #pickuplon,\n", " #pickuplat,\n", " #dropofflon,\n", " #dropofflat,\n", " ST_Distance(ST_GeogPoint(pickuplon, pickuplat), ST_GeogPoint(dropofflon, dropofflat)) \n", " AS euclidean,\n", " CONCAT(ST_AsText(ST_SnapToGrid(ST_GeogPoint(pickuplon, pickuplat), 0.01)),\n", " ST_AsText(ST_SnapToGrid(ST_GeogPoint(dropofflon, dropofflat), 0.01)))\n", " AS pickup_and_dropoff\n", " \n", "FROM `feat_eng.feateng_training_data`" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "zvOfj_k1qijv" }, "source": [ "#### Exercise: Create two SQL statements to evaluate the model." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "cellView": "both", "colab": {}, "colab_type": "code", "id": "G8rM09jLqij4" }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
mean_absolute_errormean_squared_errormean_squared_log_errormedian_absolute_errorr2_scoreexplained_variance
02.67875234.8856920.0885981.4850650.6409790.642637
\n", "
" ], "text/plain": [ " mean_absolute_error mean_squared_error mean_squared_log_error \\\n", "0 2.678752 34.885692 0.088598 \n", "\n", " median_absolute_error r2_score explained_variance \n", "0 1.485065 0.640979 0.642637 " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "#SOLUTION\n", "SELECT * FROM ML.EVALUATE(MODEL feat_eng.model_6)" ] }, { "cell_type": "code", "execution_count": 144, "metadata": { "cellView": "both", "colab": {}, "colab_type": "code", "id": "CQUfOjPlqij8" }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
rmse
05.906411
\n", "
" ], "text/plain": [ " rmse\n", "0 5.906411" ] }, "execution_count": 144, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "SELECT SQRT(mean_squared_error) AS rmse FROM ML.EVALUATE(MODEL feat_eng.model_6)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "I1dW3JLHrP5Z" }, "source": [ "### Code Clean Up" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Exercise: Clean up the code to see where we are\n", "\n", "Remove all the commented statements in the SQL statement. We should now have a total of five input features for our model. \n", "1. fare_amount\n", "2. passengers\n", "3. day_hr\n", "4. euclidean\n", "5. pickup_and_dropoff" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "colab": {}, "colab_type": "code", "id": "-UxZXY18rWG8" }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "
" ], "text/plain": [ "Empty DataFrame\n", "Columns: []\n", "Index: []" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "#Solution\n", "CREATE OR REPLACE MODEL feat_eng.model_6\n", "OPTIONS\n", " (model_type='linear_reg',\n", " input_label_cols=['fare_amount']) \n", "AS\n", "SELECT\n", " fare_amount,\n", " passengers,\n", " ML.FEATURE_CROSS(STRUCT(CAST(EXTRACT(DAYOFWEEK FROM pickup_datetime) AS STRING) AS dayofweek,\n", " CAST(EXTRACT(HOUR FROM pickup_datetime) AS STRING) AS hourofday)) AS day_hr,\n", " ST_Distance(ST_GeogPoint(pickuplon, pickuplat), ST_GeogPoint(dropofflon, dropofflat)) \n", " AS euclidean,\n", " CONCAT(ST_AsText(ST_SnapToGrid(ST_GeogPoint(pickuplon, pickuplat), 0.01)),\n", " ST_AsText(ST_SnapToGrid(ST_GeogPoint(dropofflon, dropofflat), 0.01)))\n", " AS pickup_and_dropoff\n", " \n", "FROM `feat_eng.feateng_training_data`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# LAB 03: Applying Feature Engineering to BigQuery ML Models\n", "\n", "**Learning Objectives**\n", "\n", "* Apply the BUCKETIZE function\n", "* Apply the TRANSFORM clause\n", "* Apply L2 Regularization\n", "* Model evaluation\n", "\n", "\n", "## Introduction \n", "In this notebook, we apply the BUCKETIZE function, the TRANSFORM clause, L2 Regularization, and perform model evaluation." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## BQML's Pre-processing functions:\n", "\n", "Here are some of the preprocessing functions in BigQuery ML:\n", "* ML.FEATURE_CROSS(STRUCT(features)) does a feature cross of all the combinations\n", "* ML.POLYNOMIAL_EXPAND(STRUCT(features), degree) creates x, x^2, x^3, etc.\n", "* ML.BUCKETIZE(f, split_points) where split_points is an array " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Model 7: Apply the BUCKETIZE Function \n", "\n", "\n", "##### BUCKETIZE \n", "Bucketize is a pre-processing function that creates \"buckets\" (e.g bins) - e.g. it bucketizes a continuous numerical feature into a string feature with bucket names as the value.\n", "\n", "* ML.BUCKETIZE(feature, split_points)\n", "\n", "* feature: A numerical column.\n", "\n", "* split_points: Array of numerical points to split the continuous values in feature into buckets. With n split points (s1, s2 … sn), there will be n+1 buckets generated. \n", "\n", "* Output: The function outputs a STRING for each row, which is the bucket name. bucket_name is in the format of bin_, where bucket_number starts from 1.\n", "\n", "* Currently, our model uses the ST_GeogPoint function to derive the pickup and dropoff feature. In this lab, we use the BUCKETIZE function to create the pickup and dropoff feature." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Exercise: Apply the BUCKETIZE function.\n", "* Hint: Create a model_7." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#TODO " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "
" ], "text/plain": [ "Empty DataFrame\n", "Columns: []\n", "Index: []" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "#SOLUTION\n", "CREATE OR REPLACE MODEL feat_eng.model_7\n", "OPTIONS\n", " (model_type='linear_reg',\n", " input_label_cols=['fare_amount']) \n", "AS\n", "SELECT\n", " fare_amount,\n", " passengers,\n", " SQRT( (pickuplon-dropofflon)*(pickuplon-dropofflon) + (pickuplat-dropofflat)*(pickuplat-dropofflat) ) AS euclidean, \n", " ML.FEATURE_CROSS(STRUCT(CAST(EXTRACT(DAYOFWEEK FROM pickup_datetime) AS STRING) AS dayofweek,\n", " CAST(EXTRACT(HOUR FROM pickup_datetime) AS STRING) AS hourofday)) AS day_hr,\n", " CONCAT(\n", " ML.BUCKETIZE(pickuplon, GENERATE_ARRAY(-78, -70, 0.01)),\n", " ML.BUCKETIZE(pickuplat, GENERATE_ARRAY(37, 45, 0.01)),\n", " ML.BUCKETIZE(dropofflon, GENERATE_ARRAY(-78, -70, 0.01)),\n", " ML.BUCKETIZE(dropofflat, GENERATE_ARRAY(37, 45, 0.01))\n", " ) AS pickup_and_dropoff\n", " \n", "FROM `feat_eng.feateng_training_data`" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "AVPXGKZ374v7" }, "source": [ "#### Exercise: Create three SQL statements to EVALUATE the model." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
training_runiterationlosseval_losslearning_rateduration_msrmse
00311.23183533.1723600.464343.351393
10213.26389935.9199360.858983.641964
20120.10847536.5590060.465334.484247
30070.49052481.7196790.237468.395864
\n", "
" ], "text/plain": [ " training_run iteration loss eval_loss learning_rate duration_ms \\\n", "0 0 3 11.231835 33.172360 0.4 6434 \n", "1 0 2 13.263899 35.919936 0.8 5898 \n", "2 0 1 20.108475 36.559006 0.4 6533 \n", "3 0 0 70.490524 81.719679 0.2 3746 \n", "\n", " rmse \n", "0 3.351393 \n", "1 3.641964 \n", "2 4.484247 \n", "3 8.395864 " ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "SELECT *, SQRT(loss) AS rmse FROM ML.TRAINING_INFO(MODEL feat_eng.model_7)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
mean_absolute_errormean_squared_errormean_squared_log_errormedian_absolute_errorr2_scoreexplained_variance
02.67418733.172360.0899131.518210.6357280.637299
\n", "
" ], "text/plain": [ " mean_absolute_error mean_squared_error mean_squared_log_error \\\n", "0 2.674187 33.17236 0.089913 \n", "\n", " median_absolute_error r2_score explained_variance \n", "0 1.51821 0.635728 0.637299 " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "SELECT * FROM ML.EVALUATE(MODEL feat_eng.model_7)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
rmse
05.759545
\n", "
" ], "text/plain": [ " rmse\n", "0 5.759545" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "SELECT SQRT(mean_squared_error) AS rmse FROM ML.EVALUATE(MODEL feat_eng.model_7)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Final Model: Apply the TRANSFORM clause and L2 Regularization\n", "\n", "Before we perform our prediction, we should encapsulate the entire feature set in a TRANSFORM clause. BigQuery ML now supports defining data transformations during model creation, which will be automatically applied during prediction and evaluation. This is done through the TRANSFORM clause in the existing CREATE MODEL statement. By using the TRANSFORM clause, user specified transforms during training will be automatically applied during model serving (prediction, evaluation, etc.) \n", "\n", "In our case, we are using the TRANSFORM clause to separate out the raw input data from the TRANSFORMED features. The input columns of the TRANSFORM clause is the query_expr (AS SELECT part). The output columns of TRANSFORM from select_list are used in training. These transformed columns are post-processed with standardization for numerics and one-hot encoding for categorical variables by default. \n", "\n", "The advantage of encapsulating features in the TRANSFORM is the client code doing the PREDICT doesn't change. Our model improvement is transparent to client code. Note that the TRANSFORM clause MUST be placed after the CREATE statement.\n", "\n", "##### L2 Regularization\n", "Sometimes, the training RMSE is quite reasonable, but the evaluation RMSE illustrate more error. Given the severity of the delta between the EVALUATION RMSE and the TRAINING RMSE, it may be an indication of overfitting. When we do feature crosses, we run into the risk of overfitting (for example, when a particular day-hour combo doesn't have enough taxirides)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Exercise: Apply the TRANSFORM clause and L2 Regularization to the final model and run the query." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#TODO " ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "
" ], "text/plain": [ "Empty DataFrame\n", "Columns: []\n", "Index: []" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "#SOLUTION\n", "CREATE OR REPLACE MODEL feat_eng.final_model\n", "TRANSFORM(\n", " fare_amount, \n", " SQRT( (pickuplon-dropofflon)*(pickuplon-dropofflon) + (pickuplat-dropofflat)*(pickuplat-dropofflat) ) AS euclidean, \n", " ML.FEATURE_CROSS(STRUCT(CAST(EXTRACT(DAYOFWEEK FROM pickup_datetime) AS STRING) AS dayofweek,\n", " CAST(EXTRACT(HOUR FROM pickup_datetime) AS STRING) AS hourofday)) AS day_hr,\n", " CONCAT(\n", " ML.BUCKETIZE(pickuplon, GENERATE_ARRAY(-78, -70, 0.01)),\n", " ML.BUCKETIZE(pickuplat, GENERATE_ARRAY(37, 45, 0.01)),\n", " ML.BUCKETIZE(dropofflon, GENERATE_ARRAY(-78, -70, 0.01)),\n", " ML.BUCKETIZE(dropofflat, GENERATE_ARRAY(37, 45, 0.01))\n", " ) AS pickup_and_dropoff\n", ")\n", "OPTIONS(input_label_cols=['fare_amount'], model_type='linear_reg', l2_reg=0.1) \n", "AS\n", "\n", "SELECT * FROM feat_eng.feateng_training_data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Exercise: Create three SQL statements to EVALUATE the final model." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
training_runiterationlosseval_losslearning_rateduration_msrmse
00011.12062821.654845None2326853.334761
\n", "
" ], "text/plain": [ " training_run iteration loss eval_loss learning_rate duration_ms \\\n", "0 0 0 11.120628 21.654845 None 232685 \n", "\n", " rmse \n", "0 3.334761 " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "SELECT *, SQRT(loss) AS rmse FROM ML.TRAINING_INFO(MODEL feat_eng.final_model)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
mean_absolute_errormean_squared_errormean_squared_log_errormedian_absolute_errorr2_scoreexplained_variance
02.26281621.6548450.0687151.3582590.7566880.756689
\n", "
" ], "text/plain": [ " mean_absolute_error mean_squared_error mean_squared_log_error \\\n", "0 2.262816 21.654845 0.068715 \n", "\n", " median_absolute_error r2_score explained_variance \n", "0 1.358259 0.756688 0.756689 " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "SELECT * FROM ML.EVALUATE(MODEL feat_eng.final_model)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
rmse
04.653477
\n", "
" ], "text/plain": [ " rmse\n", "0 4.653477" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "SELECT SQRT(mean_squared_error) AS rmse FROM ML.EVALUATE(MODEL feat_eng.final_model)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "Sa5UfRE2Az1w" }, "source": [ "# LAB 04: Applying Feature Engineering to BigQuery ML ModelS\n", "\n", "**Learning Objectives**\n", "\n", "* Create a prediction model\n", "* Evalute model performance\n", "* Examine the role of feature engineering on the ML problem\n", "\n", "\n", "\n", "## Introduction \n", "In this notebook, we create prediction models, evaluate model performance, and examine the role of feature engineering on the ML problem." ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "DIuFS56o_F2a" }, "source": [ "### Prediction Model\n", "\n", "\n", "Now that you have evaluated your model, the next step is to use it to predict an outcome. You use your model to predict the taxifare amount. \n", "The ML.PREDICT function is used to predict results using your model: feat_eng.final_model. \n", "\n", "Since this is a regression model (predicting a continuous numerical value), the best way to see how it performed is to evaluate the difference between the value predicted by the model and the benchmark score. We can do this with an ML.PREDICT query.\n", "\n", "#### Exercise: Modify **THIS INCORRECT SQL STRATEMENT** before running the query." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "qys4liHN_cI4" }, "outputs": [], "source": [ "%%bigquery\n", "#TODO\n", "SELECT * FROM ML.EVALUATE(MODEL feat_eng.benchmark_model, (\n", " -73.982683 AS pickuplon,\n", " 40.742104 AS pickuplat,\n", " -73.983766 AS dropofflon,\n", " 40.755174 AS dropofflat,\n", " 3.0 AS passengers,\n", " TIMESTAMP('2019-06-03 04:21:29.769443 UTC) AS pickup_datetime\n", "))" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "colab": {}, "colab_type": "code", "id": "29cuS1CbADE3" }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
predicted_fare_amountpickuplonpickuplatdropofflondropofflatpassengerspickup_datetime
06.081999-73.98268340.742104-73.98376640.7551743.02019-06-03 04:21:29.769443+00:00
\n", "
" ], "text/plain": [ " predicted_fare_amount pickuplon pickuplat dropofflon dropofflat \\\n", "0 6.081999 -73.982683 40.742104 -73.983766 40.755174 \n", "\n", " passengers pickup_datetime \n", "0 3.0 2019-06-03 04:21:29.769443+00:00 " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "#SOLUTION\n", "# This is the prediction query FOR heading 1.3 miles uptown in New York City on 2019-06-03 at 04:21:29.769443 UTC time with 3 passengers.\n", "SELECT * FROM ML.PREDICT(MODEL feat_eng.final_model, (\n", " SELECT \n", " -73.982683 AS pickuplon,\n", " 40.742104 AS pickuplat,\n", " -73.983766 AS dropofflon,\n", " 40.755174 AS dropofflat,\n", " 3.0 AS passengers,\n", " TIMESTAMP('2019-06-03 04:21:29.769443 UTC') AS pickup_datetime\n", "))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Exercise: Remove passengers from the prediction model." ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "#TODO " ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
predicted_fare_amountpickuplonpickuplatdropofflondropofflatpickup_datetime
06.081999-73.98268340.742104-73.98376640.7551742019-06-03 04:21:29.769443+00:00
\n", "
" ], "text/plain": [ " predicted_fare_amount pickuplon pickuplat dropofflon dropofflat \\\n", "0 6.081999 -73.982683 40.742104 -73.983766 40.755174 \n", "\n", " pickup_datetime \n", "0 2019-06-03 04:21:29.769443+00:00 " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "#SOLUTION - remove passengers\n", "\n", "SELECT * FROM ML.PREDICT(MODEL feat_eng.final_model, (\n", " SELECT \n", " -73.982683 AS pickuplon,\n", " 40.742104 AS pickuplat,\n", " -73.983766 AS dropofflon,\n", " 40.755174 AS dropofflat,\n", " TIMESTAMP('2019-06-03 04:21:29.769443 UTC') AS pickup_datetime\n", "))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### What can you conclude when the feature passengers is removed from the prediction model?\n", "ANSWER: Number of passengers at this pickup_datetime and location does not affect fare.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Lab Summary: \n", "Our ML problem: Develop a model to predict taxi fare based on distance -- from one point to another in New York City. \n", "Using feature engineering, we were able to predict a taxi fare of $6.08 in New York City, with an R2 score of .75, and an RMSE of 4.653 based upon the distance travelled." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Exercise: Create a RMSE summary table." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#Markdown table generator: http://www.tablesgenerator.com/markdown_tables\n", "Create a RMSE summary table:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "| Model | RMSE | Description |\n", "|-----------------|-------|---------------------------------------------------------------------------------|\n", "| benchmark_model | 8.29 | --Benchmark model - no feature engineering |\n", "| model_1 | 9.431 | --EXTRACT DayOfWeek from the pickup_datetime feature |\n", "| model_2 | 8.408 | --EXTRACT hourofday from the pickup_datetime feature |\n", "| model_3 | 9.657 | --Feature cross dayofweek and hourofday -Feature Cross does lead ot overfitting |\n", "| model_4 | 9.657 | --Apply the ML.FEATURE_CROSS clause to categorical features |\n", "| model_5 | 5.588 | --Feature cross coordinate features to create a Euclidean feature |\n", "| model_6 | 5.906 | --Feature cross pick-up and drop-off locations features |\n", "| model_7 | 5.75 | --Apply the BUCKETIZE function |\n", "| final_model | 4.653 | --Apply the TRANSFORM clause and L2 Regularization |" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Excercise: Visualization - Plot a bar chart." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAjYAAAGzCAYAAAA8I13DAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAPYQAAD2EBqD+naQAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAIABJREFUeJzt3Xt8zvX/x/HnZbg2h81hm5GZc0gOOYX6jpxy+KYDEmUI0ZxLWTkmLV9nKqfKWUSUVCRfpUKKEunn9C32k2PYzGHa9v798b25fl1t2DXbrmvvHvfb7XPL5/15fz7X6701e3pf78/nchhjjAAAACyQx9sFAAAAZBWCDQAAsAbBBgAAWINgAwAArEGwAQAA1iDYAAAAaxBsAACANQg2AADAGgQbAABgDYINAJ9UtmxZde/ePVPnOhwOjRkzJkvrAZA7EGyALLRgwQI5HA7XljdvXt12223q3r27jh07lqZ/kyZN5HA4VKlSpXSvt3HjRte1Vq1a5XZsz5496tChgyIiIuTv76/bbrtNLVq00MyZM936lS1b1q2mP2/333//Dcfz+eefu/ouWbIk3T6NGzeWw+FQ9erVb3gtX3T69GkNGjRIVapUUUBAgEJDQ1W/fn09//zzSkxM9HZ5ADIhr7cLAGz00ksvqVy5crpy5Yq2b9+uBQsW6KuvvtLevXvl7+/v1tff31+HDh3Sjh07VL9+fbdjS5culb+/v65cueLWvnXrVjVt2lRlypRR7969FRYWpri4OG3fvl3Tp0/XgAED3PrXqlVLzzzzTJo6S5UqlaHx+Pv7a9myZXr88cfd2n/99Vdt3bo1zZhyg7Nnz6pu3bpKSEhQz549VaVKFf3+++/68ccfNWvWLPXr10+FChXydpkAPESwAbJB69atVbduXUlSr169FBwcrAkTJmjt2rXq1KmTW98KFSooOTlZ77zzjluwuXLlitasWaO2bdvqvffecztn/PjxCgoK0rfffqsiRYq4HTt16lSaem677bY0ocQTbdq00dq1a3XmzBkFBwe72pctW6YSJUqoUqVKOnfuXKav7w1vvfWWjh49qq+//lqNGjVyO5aQkKD8+fN7qTLvuXjxogoWLOjtMoBbwltRQA649957JUmHDx9O9/hjjz2mFStWKDU11dX24Ycf6tKlS2mC0LXr3HHHHWlCjSSFhoZmUdX/r3379nI6nVq5cqVb+7Jly9SpUyf5+fmlOSc5OVnjxo1ThQoV5HQ6VbZsWb3wwgtKSkpy62eM0csvv6zSpUurQIECatq0qX766ad06zh//rwGDx6s8PBwOZ1OVaxYURMmTHD7umXU4cOH5efnp7vvvjvNscDAQLdZqOut92nSpImaNGni2r/21t27776rsWPH6rbbblPhwoXVoUMHxcfHKykpSYMHD1ZoaKgKFSqkHj16pPl6OBwO9e/fXytXrlS1atUUEBCghg0bas+ePZKkOXPmqGLFivL391eTJk3066+/up3/5ZdfqmPHjipTpoycTqfCw8M1ZMgQXb582a1f9+7dVahQIR0+fFht2rRR4cKF1bVrV40ePVr58uXT6dOn04y3T58+KlKkSJoZRMCXEGyAHHDtl0/RokXTPd6lSxcdP35cn3/+uatt2bJlatasWbpBJSIiQjt37tTevXsz9Pp//PGHzpw5k2b76y+76ylQoIDat2+vd955x9W2e/du/fTTT+rSpUu65/Tq1UujRo3SXXfdpalTpyoyMlKxsbHq3LmzW79Ro0Zp5MiRqlmzpiZOnKjy5curZcuWunjxolu/S5cuKTIyUkuWLFG3bt00Y8YMNW7cWDExMRo6dGiGxvFnERERSklJ0eLFiz0+92ZiY2O1YcMGDR8+XD179tTq1avVt29f9ezZUwcOHNCYMWP08MMPa8GCBZowYUKa87/88ks988w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ebMmW7j+et5ktSpUydt27ZNGzZsSHPs/Pnzadap3Mw333yT5s4rSdqxY4d+//13t69hhQoVtH37dl29etXVtm7dOsXFxXn0mtnt2ozOn7+WxhhNnz7d42u1bt3a9fylL774gtka5BrM2AA5ZNiwYerYsaMWLFigvn37ptsnKCgoQ59xNGDAAF26dEkPPfSQqlSpoqtXr2rr1q1asWKFypYtqx49erj1P3bsWLofiVCoUCE9+OCDHo2jffv2at++/Q371KxZU1FRUZo7d67Onz+vyMhI7dixQwsXLtSDDz6opk2bSvrvWppnn31WsbGxateundq0aaPvv/9en3zyiduDAKX/fv3Wrl2rdu3aqXv37qpTp44uXryoPXv2aNWqVfr111/TnHMjixcv1tKlS/XQQw+pTp06yp8/v37++We9/fbb8vf31wsvvODq26tXL61atUr333+/OnXqpMOHD2vJkiWqUKGCB1+57FelShVVqFBBzz77rI4dO6bAwEC99957mXp4Yr58+dS5c2e99tpr8vPz82ihOeBNBBsghzz88MOqUKGCJk2apN69e193vURGTJo0SStXrtTHH3+suXPn6urVqypTpoyefvppjRgxIs2D+3744Yd0Z1giIiI8DjYZ9eabb6p8+fJasGCB1qxZo7CwMMXExKRZ+/Pyyy/L399fs2fP1ubNm9WgQQN9+umnrlmdawoUKKAvvvhCr7zyilauXKlFixYpMDBQlStX1tixY10LaTPqqaeeUoECBbRp0yZ98MEHSkhIUEhIiFq2bKmYmBjVrl3b1bdVq1aaPHmypkyZosGDB6tu3bpat25duh9T4U358uXThx9+qIEDByo2Nlb+/v566KGH1L9/f9WsWdPj63Xr1k2vvfaamjVrppIlS2ZDxUDWc5hbuacRAGCt3bt3q1atWlq0aNEN33oEfAlrbAAA6Zo3b54KFSqkhx9+2NulABnGW1EAADcffvih9u3bp7lz56p///58MCZyFd6KAgC4KVu2rE6ePKlWrVpp8eLFN3xQJOBrCDYAAMAarLEBAADWINgAAABrWL94ODU1Vb/99psKFy7Mh7cBAJBLGGN04cIFlSpVKt3PNLse64PNb7/9luYTcQEAQO4QFxeX7qfQX49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QYAAFiDYAMAAKxBsAEAANYg2AAAAGv4fLA5duyYHn/8cRUvXlwBAQG688479d1333m7LAAA4IPyeruAGzl37pwaN26spk2b6pNPPlFISIgOHjyookWLers0AADgg3w62EyYMEHh4eGaP3++q61cuXJerAgAAPgyn34rau3atapbt646duyo0NBQ1a5dW/PmzbvhOUlJSUpISHDbAADA34NPB5v//Oc/mjVrlipVqqQNGzaoX79+GjhwoBYuXHjdc2JjYxUUFOTawsPDc7BiAADgTT4dbFJTU3XXXXfplVdeUe3atdWnTx/17t1bs2fPvu45MTExio+Pd21xcXE5WDEAAPAmnw42JUuWVLVq1dzaqlatqqNHj173HKfTqcDAQLcNAAD8Pfj04uHGjRtr//79bm0HDhxQRESElyoCADtN3XjA2yXc1JAWlb1dAnIBn56xGTJkiLZv365XXnlFhw4d0rJlyzR37lxFR0d7uzQAAOCDfDrY1KtXT2vWrNE777yj6tWra9y4cZo2bZq6du3q7dIAAIAP8um3oiSpXbt2ateunbfLAAAAuYBPz9gAAAB4wudnbAAA8AQLof/emLEBAADWINgAAABrEGwAAIA1CDYAAMAaBBsAAGANgg0AALAGt3sDQCZxWzHge5ixAQAA1iDYAAAAa3gUbE6dOnXD48nJydqxY8ctFQQAAJBZHgWbkiVLuoWbO++8U3Fxca7933//XQ0bNsy66gAAADzgUbAxxrjt//rrr/rjjz9u2AcAACCnZPkaG4fDkdWXBAAAyBAWDwMAAGt49Bwbh8OhCxcuyN/fX8YYORwOJSYmKiEhQZJc/wUAAPAGj4KNMUaVK1d2269du7bbPm9FAQAAb/Eo2GzevDm76gAAALhlHgWbyMjI7KoDAADglnkUbJKTk5WSkiKn0+lqO3nypGbPnq2LFy/qgQce0D333JPlRQIAAGSER8Gmd+/eyp8/v+bMmSNJunDhgurVq6crV66oZMmSmjp1qj744AO1adMmW4oFAAC4EY9u9/7666/1yCOPuPYXLVqklJQUHTx4ULt379bQoUM1ceLELC8SAAAgIzwKNseOHVOlSpVc+5s2bdIjjzyioKAgSVJUVJR++umnr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++6Go7cuSIFyvKGgSbLHD06FENHTpUTz31lHbt2qWZM2dq8uTJqly5srp27apu3bpp8uTJql27tk6fPq1NmzapRo0aatu2bYauP2bMGPXt21ehoaFq3bq1Lly4oK+//loDBgzI5pFlnUOHDikxMVEnTpzQ5cuX9cMPP0j677/i8ufP7+XqPLN3717dd999atWqlYYOHaoTJ05I+u8UdkhIiJer88zHH3+skydPql69eipUqJB++uknDRs2TI0bN1bZsmW9XZ5H/hpezpw5I0mqWrVqrrtN+sMPP9TJkyd19913y9/fXxs3btQrr7zidlNBblG5cmW1b99egwYN0ty5cxUYGKiYmBhVqVIlV/9jbcWKFUpOTvbZtz4zolKlSjp69KiWL1+uevXq6aOPPtKaNWu8XdYtI9hkgW7duuny5cuqX7++/Pz8NGjQIPXp00fSf59D8fLLL+uZZ57RsWPHFBwcrLvvvlvt2rXL8PWjoqJ05coVTZ06Vc8++6yCg4NzfAX+rerVq5e++OIL137t2rUlSb/88kuu+wW6atUqnT59WkuWLNGSJUtc7REREbnu2RwBAQGaN2+ehgwZoqSkJIWHh+vhhx/W8OHDvV3a31q+fPn0+uuva8iQITLGqGLFipoyZYp69+7t7dIyZdGiRRoyZIjatm2rPHnyKDIyUuvXr0/zNklu8tZbb+nhhx/OdaH5zx544AENGTJE/fv3V1JSktq2bauRI0dqzJgx3i7tljiM8ZH7BwEAAG6Rbz3oAQAA4BYQbAAAgDUINgAAwBoEGwAAYA2CDQAAsAbBBgAAWINgAwAArEGwAQAA1iDYAAAAaxBsAACANQg2AADAGv8Ht+m4eMR4yqwAAAAASUVORK5CYII=\n", "text/plain": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import matplotlib.pyplot as plt; plt.rcdefaults()\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "models = ('bench','m1', 'm2', 'm3', 'm4', 'm5', 'm6','m7', 'final')\n", "y_pos = np.arange(len(models))\n", "rmse = [8.29,9.431,8.408,9.657,9.657,5.588,5.906,5.759,4.653]\n", "\n", "plt.bar(y_pos, rmse, align='center', alpha=0.5)\n", "plt.xticks(y_pos, models)\n", "plt.ylabel('RMSE')\n", "plt.title('RMSE Model Summary')\n", "\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYUAAAEaCAYAAAD+E0veAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAH8dJREFUeJzt3Xl0FGXi9fHbScAEshCyAElABAIGFxTZFSIkMsCwzVEZYEA2kfXHAVlcGSLjYEAQiQaUHYGDIg44OoCYYQkEkX1XkYjsGBJkiQFi0vX+wfC8tCwJSLpa8v2ck3Os6qerbrfATT3VVe2wLMsSAACSvOwOAADwHJQCAMCgFAAABqUAADAoBQCAQSkAAAxKAQBgUApwm+7du8vhcMjhcMjb21tRUVF65plndPToUZdxjz/+uBwOh55//vmrtjFp0iQ5HA5Vq1bNZf2sWbP0yCOPKDAwUAEBAYqJiVHv3r3N46tXrzb7/u3PokWLrpv5/PnzGjlypKKjo+Xn56eyZcuqbt26SkpK+p3vBuCZfOwOgOKlcePGWrhwofLz85Wenq4BAwbo6aef1vr1613GVapUSXPnzlViYqJKlixp1k+dOlV33323y9jZs2erX79+Gj9+vJo3by5J+uabb/Tpp59etf+tW7eqQoUKLuuCg4Ovm7dfv35atWqVJk2apFq1auns2bPatm2bDh06dNOv3dPl5ua6vNcopizATbp162bFxcW5rEtKSrIkWWfOnDHrYmNjrR49eliVK1e2PvzwQ7N+7dq1VkBAgDVs2DCratWqZn27du2sJ5988ob7XrVqlSXJOnz48E1lDgoKst55550bjrnW65o7d6515V+vUaNGWVWrVrU++ugjq1q1apafn5/Vrl0768yZM9Ynn3xiVa9e3fL397eefPJJ6/Tp01dtOykpyYqMjLRKly5t9erVy8rNzbWmTJliVapUySpTpozVu3dv6+LFi+Z5K1assGJjY63g4GArMDDQatKkifX111+7ZJRkTZo0yerUqZMVGBhodejQwYqNjbV69+7tMs7pdFpVqlSxRo8efVPvHf6YmD6CbY4dO6ZFixbJ29tb3t7eLo95eXmpV69emjZtmlk3depUde7cWaVLl3YZW6FCBW3evFn79u277RkrVKig5cuX69SpU797W8ePH9ecOXP0ySefaNmyZUpLS9NTTz2l6dOna+HChVq2bJnWrl2rMWPGuDxv48aN2rx5s7788kstWLBA8+bNU9u2bbV+/XotX75c8+bN09y5czVjxgzznOzsbPXv319fffWV1q9fr+joaLVo0UJZWVku237ttdfUqFEjbd26Va+//rr69OmjBQsWKDs724xZuXKlDh48qF69ev3u9wB/AHa3EoqPbt26Wd7e3lbp0qUtPz8/S5IlyRo6dKjLuNjYWKtXr17W0aNHrRIlSljp6enWqVOnLD8/P2vLli3mt+7Ljh8/bj366KOWJOvuu++2OnToYL3//vtWdna2GXP5SKFUqVJW6dKlXX6OHj163czr1q2zKlWqZHl5eVkPPPCA1bt3b2vx4sWW0+l0eV2FOVLw9va2Tp48adb179/f8vLysjIyMsy6QYMGWY888ojLtsPCwlyOAlq1amWFhIRYFy5cMOvatm17w6Ol/Px8q0yZMta8efPMOklWz549XcZduHDBCg0NtaZNm2bWdezY0Wrbtu11t407C0cKcKv69etr+/bt2rhxo0aOHKmGDRvq9ddfv+bYiIgItWrVStOnT9fcuXMVExOj2rVrXzWufPnyWrdunfbu3auXXnpJpUuX1ogRI3T//fcrIyPDZewXX3yh7du3u/yUK1fuunkfffRRpaena+3aterWrZt++uknPfXUU2rbtq2sm7yXZGRkpEJDQ11yly9fXmFhYS7rfps5JibGZa6/fPnyqlGjhu66667rPu/AgQPq2rWrqlWrpsDAQAUGBurMmTM6ePCgy7br1avnsnzXXXepe/fu5ggtKytLixcvdjlpjzsbJ5rhVn5+fuaTQ/fff7/S09P1f//3fy7TRFd67rnn1KtXL5UtW1aDBg264bZjYmIUEx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/atGmj3bt3m+cOHDhQ8+fP1/PPP6/z58+rXLlyateunVteD3Az+D4FAIDB9BEAwKAUAAAGpQAAMCgFAIBBKQAADEoBAGBQCgAAg1IAABj/D9wO+BJLQDDcAAAAAElFTkSuQmCC\n", "text/plain": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import matplotlib.pyplot as plt\n", "%matplotlib inline\n", "plt.style.use('ggplot')\n", "\n", "x = ['bench','m1', 'm2', 'm3', 'm4', 'm5', 'm6','m7', 'final']\n", "RMSE = [8.29,9.431,8.408,9.657,9.657,5.588,5.906,5.759,4.653]\n", "\n", "x_pos = [i for i, _ in enumerate(x)]\n", "\n", "plt.bar(x_pos, RMSE, color='green')\n", "plt.xlabel(\"Model\")\n", "plt.ylabel(\"RMSE\")\n", "plt.title(\"RMSE Model Summary\")\n", "\n", "plt.xticks(x_pos, x)\n", "\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "
" ], "text/plain": [ "Empty DataFrame\n", "Columns: []\n", "Index: []" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "CREATE OR REPLACE MODEL feat_eng.challenge_model\n", "TRANSFORM(fare_amount, \n", " SQRT( (pickuplon-dropofflon)*(pickuplon-dropofflon) + (pickuplat-dropofflat)*(pickuplat-dropofflat) ) AS euclidean, \n", " IF(EXTRACT(dayofweek FROM pickup_datetime) BETWEEN 2 and 6, 'weekday', 'weekend') AS dayofweek,\n", " ML.BUCKETIZE(EXTRACT(HOUR FROM pickup_datetime), [5, 10, 17]) AS day_hr,\n", " CONCAT(\n", " ML.BUCKETIZE(pickuplon, GENERATE_ARRAY(-78, -70, 0.01)),\n", " ML.BUCKETIZE(pickuplat, GENERATE_ARRAY(37, 45, 0.01)),\n", " ML.BUCKETIZE(dropofflon, GENERATE_ARRAY(-78, -70, 0.01)),\n", " ML.BUCKETIZE(dropofflat, GENERATE_ARRAY(37, 45, 0.01))\n", " ) AS pickup_and_dropoff\n", ")\n", "OPTIONS(input_label_cols=['fare_amount'], model_type='linear_reg', l2_reg=0.1) \n", "\n", "AS\n", "SELECT \n", "*\n", "FROM `feat_eng.feateng_training_data`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Exercise: Create three SQL statements to EVALUATE the challenge model." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
training_runiterationlosseval_losslearning_rateduration_msrmse
00011.42479222.052031None1798403.380058
\n", "
" ], "text/plain": [ " training_run iteration loss eval_loss learning_rate duration_ms \\\n", "0 0 0 11.424792 22.052031 None 179840 \n", "\n", " rmse \n", "0 3.380058 " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "SELECT *, SQRT(loss) AS rmse FROM ML.TRAINING_INFO(MODEL feat_eng.challenge_model)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
mean_absolute_errormean_squared_errormean_squared_log_errormedian_absolute_errorr2_scoreexplained_variance
02.31978222.0520310.0703761.4124580.7522250.752226
\n", "
" ], "text/plain": [ " mean_absolute_error mean_squared_error mean_squared_log_error \\\n", "0 2.319782 22.052031 0.070376 \n", "\n", " median_absolute_error r2_score explained_variance \n", "0 1.412458 0.752225 0.752226 " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "SELECT * FROM ML.EVALUATE(MODEL feat_eng.challenge_model)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
rmse
04.695959
\n", "
" ], "text/plain": [ " rmse\n", "0 4.695959" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "SELECT SQRT(mean_squared_error) AS rmse FROM ML.EVALUATE(MODEL feat_eng.challenge_model)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
predicted_fare_amountpickuplonpickuplatdropofflondropofflatpickup_datetime
06.367442-73.98268340.742104-73.98376640.7551742019-06-03 04:21:29.769443+00:00
\n", "
" ], "text/plain": [ " predicted_fare_amount pickuplon pickuplat dropofflon dropofflat \\\n", "0 6.367442 -73.982683 40.742104 -73.983766 40.755174 \n", "\n", " pickup_datetime \n", "0 2019-06-03 04:21:29.769443+00:00 " ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%bigquery\n", "#PREDICTION on the CHALLENGE MODEL\n", "#In this model, we do not show a pickup time because the bucketize has put pickup time in three buckets:\n", "#5,10,17\n", "#How do we not show pickup datetime?\n", "\n", "SELECT * FROM ML.PREDICT(MODEL feat_eng.challenge_model, (\n", " SELECT \n", " -73.982683 AS pickuplon,\n", " 40.742104 AS pickuplat,\n", " -73.983766 AS dropofflon,\n", " 40.755174 AS dropofflat,\n", " TIMESTAMP('2019-06-03 04:21:29.769443 UTC') AS pickup_datetime\n", "))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "colab": { "collapsed_sections": [], "name": "10.3.2019-DRAFT-_1 - FeatEnG - LABS.ipynb", "provenance": [], "toc_visible": true }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.3" } }, "nbformat": 4, "nbformat_minor": 4 }