{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Train a model and use it for prediction\r\n",
        "\r\n",
        "Before running this notebook, run the auto-ml-sql-setup.ipynb notebook."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "![Impressions](https://PixelServer20190423114238.azurewebsites.net/api/impressions/MachineLearningNotebooks/how-to-use-azureml/automated-machine-learning/sql-server/energy-demand/auto-ml-sql-energy-demand.png)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Set the default database"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "USE [automl]\r\n",
        "GO"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Use the AutoMLTrain stored procedure to create a forecasting model for the nyc_energy dataset."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "INSERT INTO dbo.aml_model(RunId, ExperimentName, Model, LogFileText, WorkspaceName)\r\n",
        "EXEC dbo.AutoMLTrain @input_query='\r\n",
        "SELECT CAST(timeStamp as NVARCHAR(30)) as timeStamp,\r\n",
        "       demand,\r\n",
        "\t   precip,\r\n",
        "\t   temp,\r\n",
        "\t   CASE WHEN timeStamp < ''2017-01-01'' THEN 0 ELSE 1 END AS is_validate_column\r\n",
        "FROM nyc_energy\r\n",
        "WHERE demand IS NOT NULL AND precip IS NOT NULL AND temp IS NOT NULL\r\n",
        "and timeStamp < ''2017-02-01''',\r\n",
        "@label_column='demand',\r\n",
        "@task='forecasting',\r\n",
        "@iterations=10,\r\n",
        "@iteration_timeout_minutes=5,\r\n",
        "@time_column_name='timeStamp',\r\n",
        "@is_validate_column='is_validate_column',\r\n",
        "@experiment_name='automl-sql-forecast',\r\n",
        "@primary_metric='normalized_root_mean_squared_error'"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Use the AutoMLPredict stored procedure to predict using the forecasting model for the nyc_energy dataset."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "DECLARE @Model NVARCHAR(MAX) = (SELECT TOP 1 Model FROM dbo.aml_model\r\n",
        "                                WHERE ExperimentName = 'automl-sql-forecast'\r\n",
        "\t\t\t\t\t\t\t\tORDER BY CreatedDate DESC)\r\n",
        "\r\n",
        "EXEC dbo.AutoMLPredict @input_query='\r\n",
        "SELECT CAST(timeStamp AS NVARCHAR(30)) AS timeStamp,\r\n",
        "       demand,\r\n",
        "\t   precip,\r\n",
        "\t   temp\r\n",
        "FROM nyc_energy\r\n",
        "WHERE demand IS NOT NULL AND precip IS NOT NULL AND temp IS NOT NULL\r\n",
        "AND timeStamp >= ''2017-02-01''',\r\n",
        "@label_column='demand',\r\n",
        "@model=@model\r\n",
        "WITH RESULT SETS ((timeStamp NVARCHAR(30), actual_demand FLOAT, precip FLOAT, temp FLOAT, predicted_demand FLOAT))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## List all the metrics for all iterations for the most recent training run."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "DECLARE @RunId NVARCHAR(43)\r\n",
        "DECLARE @ExperimentName NVARCHAR(255)\r\n",
        "\r\n",
        "SELECT TOP 1 @ExperimentName=ExperimentName, @RunId=SUBSTRING(RunId, 1, 43)\r\n",
        "FROM aml_model\r\n",
        "ORDER BY CreatedDate DESC\r\n",
        "\r\n",
        "EXEC dbo.AutoMLGetMetrics @RunId, @ExperimentName"
      ]
    }
  ],
  "metadata": {
    "authors": [
      {
        "name": "jeffshep"
      }
    ],
    "kernelspec": {
      "display_name": "Python 3.6",
      "language": "sql",
      "name": "python36"
    },
    "language_info": {
      "name": "sql",
      "version": ""
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}