%%bash
sudo pip freeze | grep google-cloud-bigquery==1.6.1 || \
sudo pip install google-cloud-bigquery==1.6.1

"""
Explanation: LAB 3a:  BigQuery ML Model Baseline.
Learning Objectives

Create baseline model with BQML
Evaluate baseline model
Calculate RMSE of baseline model

Introduction
In this notebook, we will create a baseline model to predict the weight of a baby before it is born.  We will use BigQuery ML to build a linear babyweight prediction model with the base features and no feature engineering, yet.
We will create a baseline model with BQML, evaluate our baseline model, and calculate the its RMSE.
Load necessary libraries
Check that the Google BigQuery library is installed and if not, install it.
End of explanation
"""


%%bigquery
-- LIMIT 0 is a free query; this allows us to check that the table exists.
SELECT * FROM babyweight.babyweight_data_train
LIMIT 0

%%bigquery
-- LIMIT 0 is a free query; this allows us to check that the table exists.
SELECT * FROM babyweight.babyweight_data_eval
LIMIT 0

"""
Explanation: Verify tables exist
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 to create them.
End of explanation
"""


%%bigquery
CREATE OR REPLACE MODEL
    babyweight.baseline_model

OPTIONS (
    MODEL_TYPE="LINEAR_REG",
    INPUT_LABEL_COLS=["weight_pounds"],
    DATA_SPLIT_METHOD="NO_SPLIT") AS

SELECT
    weight_pounds,
    is_male,
    mother_age,
    plurality,
    gestation_weeks
FROM
    babyweight.babyweight_data_train

"""
Explanation: Create the baseline model
Next, we'll create a linear regression baseline model with no feature engineering.  We'll use this to compare our later, more complex models against.
Train the "Baseline Model".
When creating a BQML model, you must specify the model type (in our case linear regression) and the input label (weight_pounds).  Note also that we are using the training data table as the data source and we don't need BQML to split the data because we have already split it ourselves.
End of explanation
"""


%%bigquery
-- Information from model training
SELECT * FROM ML.TRAINING_INFO(MODEL babyweight.baseline_model)

"""
Explanation: REMINDER:  The query takes several minutes to complete. After the first iteration is complete, your model (baseline_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.
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.
Once the training is done, visit the BigQuery Cloud Console and look at the model that has been trained. Then, come back to this notebook.
Evaluate the baseline model
Even though BigQuery can automatically split the data it is given, and training on only a part of the data and using the rest for evaluation, to compare with our custom models later we wanted to decide the split ourselves so that it is completely reproducible.
NOTE: The results are also displayed in the BigQuery Cloud Console under the Evaluation tab.
End of explanation
"""


%%bigquery
SELECT
    *
FROM
    ML.EVALUATE(MODEL babyweight.baseline_model,
    (
    SELECT
        weight_pounds,
        is_male,
        mother_age,
        plurality,
        gestation_weeks
    FROM
        babyweight.babyweight_data_eval
    ))

"""
Explanation: Get evaluation statistics for the baseline_model.
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.
End of explanation
"""


%%bigquery
SELECT
    SQRT(mean_squared_error) AS rmse
FROM
    ML.EVALUATE(MODEL babyweight.baseline_model,
    (
    SELECT
        weight_pounds,
        is_male,
        mother_age,
        plurality,
        gestation_weeks
    FROM
        babyweight.babyweight_data_eval
    ))

"""
Explanation: Resource for an explanation of the Regression Metrics.
Write a SQL query to find the RMSE of the evaluation data
Since this is regression, we typically use the RMSE, but natively this is not in the output of our evaluation metrics above. However, we can simply take the SQRT() of the mean squared error of our loss metric from evaluation of the baseline_model to get RMSE.
End of explanation
"""


