Created
January 21, 2019 21:31
-
-
Save chrisking/2fd3b998b7fd25784bb8c6fb73e96e92 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "# Forecast Date Bug Exploration\n", | |
| "\n", | |
| "I suspect there is an issue with the format of your datetime values when creating your datasets. The goal of this notebook is to showcase that datetime values must be complete(all values exist) in order to work. If that is the case corrective action will need to take place in our docs to inform users of this requirement. Also the console and API need to provide clear feedback that the data has this particular problem.\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "## Set up Preview SDK<a class=\"anchor\" id=\"setup\"></a>" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "# Configures your AWS CLI to now understand our up and coming service Amazon Forecast\n", | |
| "!aws configure add-model --service-model file://../sdk/forecastquery-2018-06-26.normal.json --service-name forecastquery\n", | |
| "!aws configure add-model --service-model file://../sdk/forecast-2018-06-26.normal.json --service-name forecast" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Requirement already satisfied: boto3 in /home/ec2-user/anaconda3/envs/python3/lib/python3.6/site-packages (1.9.69)\n", | |
| "Requirement already satisfied: s3transfer<0.2.0,>=0.1.10 in /home/ec2-user/anaconda3/envs/python3/lib/python3.6/site-packages (from boto3) (0.1.13)\n", | |
| "Requirement already satisfied: jmespath<1.0.0,>=0.7.1 in /home/ec2-user/anaconda3/envs/python3/lib/python3.6/site-packages (from boto3) (0.9.3)\n", | |
| "Requirement already satisfied: botocore<1.13.0,>=1.12.69 in /home/ec2-user/anaconda3/envs/python3/lib/python3.6/site-packages (from boto3) (1.12.69)\n", | |
| "Requirement already satisfied: python-dateutil<3.0.0,>=2.1; python_version >= \"2.7\" in /home/ec2-user/anaconda3/envs/python3/lib/python3.6/site-packages (from botocore<1.13.0,>=1.12.69->boto3) (2.7.3)\n", | |
| "Requirement already satisfied: docutils>=0.10 in /home/ec2-user/anaconda3/envs/python3/lib/python3.6/site-packages (from botocore<1.13.0,>=1.12.69->boto3) (0.14)\n", | |
| "Requirement already satisfied: urllib3<1.25,>=1.20; python_version >= \"3.4\" in /home/ec2-user/anaconda3/envs/python3/lib/python3.6/site-packages (from botocore<1.13.0,>=1.12.69->boto3) (1.22)\n", | |
| "Requirement already satisfied: six>=1.5 in /home/ec2-user/anaconda3/envs/python3/lib/python3.6/site-packages (from python-dateutil<3.0.0,>=2.1; python_version >= \"2.7\"->botocore<1.13.0,>=1.12.69->boto3) (1.11.0)\n", | |
| "\u001b[33mYou are using pip version 10.0.1, however version 18.1 is available.\n", | |
| "You should consider upgrading via the 'pip install --upgrade pip' command.\u001b[0m\n", | |
| "Requirement already satisfied: pandas in /home/ec2-user/anaconda3/envs/python3/lib/python3.6/site-packages (0.22.0)\n", | |
| "Requirement already satisfied: pytz>=2011k in /home/ec2-user/anaconda3/envs/python3/lib/python3.6/site-packages (from pandas) (2018.4)\n", | |
| "Requirement already satisfied: python-dateutil>=2 in /home/ec2-user/anaconda3/envs/python3/lib/python3.6/site-packages (from pandas) (2.7.3)\n", | |
| "Requirement already satisfied: numpy>=1.9.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.6/site-packages (from pandas) (1.14.5)\n", | |
| "Requirement already satisfied: six>=1.5 in /home/ec2-user/anaconda3/envs/python3/lib/python3.6/site-packages (from python-dateutil>=2->pandas) (1.11.0)\n", | |
| "\u001b[33mYou are using pip version 10.0.1, however version 18.1 is available.\n", | |
| "You should consider upgrading via the 'pip install --upgrade pip' command.\u001b[0m\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "# Prerequisites : 1 time install only, remove the comments to execute the lines.\n", | |
| "!pip install boto3\n", | |
| "!pip install pandas" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import boto3\n", | |
| "from time import sleep\n", | |
| "import subprocess\n", | |
| "import matplotlib.pyplot as plt" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "session = boto3.Session(region_name='us-west-2') #us-east-1 is also supported\n", | |
| "\n", | |
| "forecast = session.client(service_name='forecast')\n", | |
| "forecastquery = session.client(service_name='forecastquery')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "## Test Setup <a class=\"anchor\" id=\"hello\"></a>\n", | |
| "\n", | |
| "Validate that your account has access to forecast" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": { | |
| "scrolled": true | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'RecipeNames': ['forecast_ARIMA',\n", | |
| " 'forecast_DEEP_AR',\n", | |
| " 'forecast_DEEP_AR_PLUS',\n", | |
| " 'forecast_ETS',\n", | |
| " 'forecast_MDN',\n", | |
| " 'forecast_MQRNN',\n", | |
| " 'forecast_NPTS',\n", | |
| " 'forecast_PROPHET',\n", | |
| " 'forecast_SQF'],\n", | |
| " 'ResponseMetadata': {'RequestId': '91b2eb8d-c068-4f8e-b50e-a082fd60c1c2',\n", | |
| " 'HTTPStatusCode': 200,\n", | |
| " 'HTTPHeaders': {'content-type': 'application/x-amz-json-1.1',\n", | |
| " 'date': 'Tue, 08 Jan 2019 19:50:12 GMT',\n", | |
| " 'x-amzn-requestid': '91b2eb8d-c068-4f8e-b50e-a082fd60c1c2',\n", | |
| " 'content-length': '174',\n", | |
| " 'connection': 'keep-alive'},\n", | |
| " 'RetryAttempts': 0}}" | |
| ] | |
| }, | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "forecast.list_recipes()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "\n", | |
| "### Preparing your Data" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "In Amazon Forecast , a dataset is a collection of file(s) which contain data that is relevant for a forecasting task. A dataset must conform to a schema provided by Amazon Forecast. " | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "For this exercise, we use the individual household electric power consumption dataset. (Dua, D. and Karra Taniskidou, E. (2017). UCI Machine Learning Repository [http://archive.ics.uci.edu/ml]. Irvine, CA: University of California, School of Information and Computer Science.) We aggregate the usage data hourly. " | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "# Corrected Data Type" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "This configuration is proven to work." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": [ | |
| "<div>\n", | |
| "<style scoped>\n", | |
| " .dataframe tbody tr th:only-of-type {\n", | |
| " vertical-align: middle;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe tbody tr th {\n", | |
| " vertical-align: top;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe thead th {\n", | |
| " text-align: right;\n", | |
| " }\n", | |
| "</style>\n", | |
| "<table border=\"1\" class=\"dataframe\">\n", | |
| " <thead>\n", | |
| " <tr style=\"text-align: right;\">\n", | |
| " <th></th>\n", | |
| " <th>2014-01-01 01:00:00</th>\n", | |
| " <th>38.34991708126038</th>\n", | |
| " <th>client_12</th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <th>0</th>\n", | |
| " <td>2014-01-01 02:00:00</td>\n", | |
| " <td>33.5820895522388</td>\n", | |
| " <td>client_12</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>1</th>\n", | |
| " <td>2014-01-01 03:00:00</td>\n", | |
| " <td>34.41127694859037</td>\n", | |
| " <td>client_12</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2</th>\n", | |
| " <td>2014-01-01 04:00:00</td>\n", | |
| " <td>39.800995024875625</td>\n", | |
| " <td>client_12</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "</div>" | |
| ], | |
| "text/plain": [ | |
| " 2014-01-01 01:00:00 38.34991708126038 client_12\n", | |
| "0 2014-01-01 02:00:00 33.5820895522388 client_12\n", | |
| "1 2014-01-01 03:00:00 34.41127694859037 client_12\n", | |
| "2 2014-01-01 04:00:00 39.800995024875625 client_12" | |
| ] | |
| }, | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "import pandas as pd\n", | |
| "df = pd.read_csv(\"../data/item-demand-time.csv\", dtype = object)\n", | |
| "df.head(3)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 7, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "s3 = session.client('s3')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 8, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "accountId = boto3.client('sts').get_caller_identity().get('Account')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 13, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "bucketName = 'amazon-forecast-chrisking-data'# Update the unique-value bit here.\n", | |
| "key=\"elec_data/item-demand-time-working.csv\"" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 14, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "s3.upload_file(Filename=\"../data/item-demand-time.csv\", Bucket=bucketName, Key=key)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 11, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'amazon-forecast-chrisking-data'" | |
| ] | |
| }, | |
| "execution_count": 11, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "bucketName" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 15, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "# One time setup only, uncomment the following command to create the role to provide to Amazon Forecast. \n", | |
| "# Save the generated role for all future calls to use for importing or exporting data. \n", | |
| "\n", | |
| "cmd = 'python ../setup_forecast_permissions.py '+bucketName\n", | |
| "p = subprocess.Popen(cmd.split(' '), stdout=subprocess.PIPE, stderr=subprocess.PIPE)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 16, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "roleArn = 'arn:aws:iam::%s:role/amazonforecast'%accountId" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### CreateDataset" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "More details about `Domain` and dataset type can be found on the [documentation](https://docs.aws.amazon.com/forecast/latest/dg/howitworks-domains-ds-types.html) . For this example, we are using [CUSTOM](https://docs.aws.amazon.com/forecast/latest/dg/custom-domain.html) domain with 3 required attributes `timestamp`, `target_value` and `item_id`. Also for your project name, update it to reflect your name in a lowercase format.\n", | |
| "\n", | |
| "Note here we are using a fully padded dataset" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 17, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "DATASET_FREQUENCY = \"H\" \n", | |
| "TIMESTAMP_FORMAT = \"yyyy-MM-dd hh:mm:ss\"" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 19, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "project = 'workshop_demo_cd' # Replace this with a unique name here, make sure the entire name is < 30 characters.\n", | |
| "datasetName= project+'_ds'\n", | |
| "datasetGroupName= project +'_gp'\n", | |
| "s3DataPath = \"s3://\"+bucketName+\"/\"+key" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 20, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'workshop_demo_cd_ds'" | |
| ] | |
| }, | |
| "execution_count": 20, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "datasetName" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 21, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "# Specify the schema of your dataset here. Make sure the order of columns matches the raw data files.\n", | |
| "schema ={\n", | |
| " \"Attributes\":[\n", | |
| " {\n", | |
| " \"AttributeName\":\"timestamp\",\n", | |
| " \"AttributeType\":\"timestamp\"\n", | |
| " },\n", | |
| " {\n", | |
| " \"AttributeName\":\"target_value\",\n", | |
| " \"AttributeType\":\"float\"\n", | |
| " },\n", | |
| " {\n", | |
| " \"AttributeName\":\"item_id\",\n", | |
| " \"AttributeType\":\"string\"\n", | |
| " }\n", | |
| " ]\n", | |
| "}\n", | |
| "\n", | |
| "response=forecast.create_dataset(\n", | |
| " Domain=\"CUSTOM\",\n", | |
| " DatasetType='TARGET_TIME_SERIES',\n", | |
| " DataFormat='CSV',\n", | |
| " DatasetName=datasetName,\n", | |
| " DataFrequency=DATASET_FREQUENCY, \n", | |
| " TimeStampFormat=TIMESTAMP_FORMAT,\n", | |
| " Schema = schema\n", | |
| " )" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 22, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'DatasetName': 'workshop_demo_cd_ds',\n", | |
| " 'DatasetType': 'TARGET_TIME_SERIES',\n", | |
| " 'DataFormat': 'CSV',\n", | |
| " 'Domain': 'CUSTOM',\n", | |
| " 'ScheduleExpression': 'none',\n", | |
| " 'DatasetArn': 'arn:aws:forecast:us-west-2:059124553121:ds/workshop_demo_cd_ds',\n", | |
| " 'Status': 'ACTIVE',\n", | |
| " 'ResponseMetadata': {'RequestId': '857315bd-c0f6-4613-a2db-e839b98bbf63',\n", | |
| " 'HTTPStatusCode': 200,\n", | |
| " 'HTTPHeaders': {'content-type': 'application/x-amz-json-1.1',\n", | |
| " 'date': 'Sun, 20 Jan 2019 19:02:46 GMT',\n", | |
| " 'x-amzn-requestid': '857315bd-c0f6-4613-a2db-e839b98bbf63',\n", | |
| " 'content-length': '233',\n", | |
| " 'connection': 'keep-alive'},\n", | |
| " 'RetryAttempts': 0}}" | |
| ] | |
| }, | |
| "execution_count": 22, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "forecast.describe_dataset(DatasetName=datasetName)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 23, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'DatasetGroupName': 'workshop_demo_cd_gp',\n", | |
| " 'DatasetGroupArn': 'arn:aws:forecast:us-west-2:059124553121:dsgroup/workshop_demo_cd_gp',\n", | |
| " 'ResponseMetadata': {'RequestId': 'f17aaaed-bbb7-4629-b1dd-53f787198fe1',\n", | |
| " 'HTTPStatusCode': 200,\n", | |
| " 'HTTPHeaders': {'content-type': 'application/x-amz-json-1.1',\n", | |
| " 'date': 'Sun, 20 Jan 2019 19:02:49 GMT',\n", | |
| " 'x-amzn-requestid': 'f17aaaed-bbb7-4629-b1dd-53f787198fe1',\n", | |
| " 'content-length': '130',\n", | |
| " 'connection': 'keep-alive'},\n", | |
| " 'RetryAttempts': 0}}" | |
| ] | |
| }, | |
| "execution_count": 23, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "forecast.create_dataset_group(DatasetGroupName=datasetGroupName,RoleArn=roleArn,DatasetNames=[datasetName])" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "If you have an existing datasetgroup, you can update it" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 24, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'DatasetGroupName': 'workshop_demo_cd_gp',\n", | |
| " 'DatasetGroupArn': 'arn:aws:forecast:us-west-2:059124553121:dsgroup/workshop_demo_cd_gp',\n", | |
| " 'Datasets': ['workshop_demo_cd_ds'],\n", | |
| " 'RoleArn': 'arn:aws:iam::059124553121:role/amazonforecast',\n", | |
| " 'ResponseMetadata': {'RequestId': '18812db2-a414-4634-88da-0e9fb3536a14',\n", | |
| " 'HTTPStatusCode': 200,\n", | |
| " 'HTTPHeaders': {'content-type': 'application/x-amz-json-1.1',\n", | |
| " 'date': 'Sun, 20 Jan 2019 19:02:50 GMT',\n", | |
| " 'x-amzn-requestid': '18812db2-a414-4634-88da-0e9fb3536a14',\n", | |
| " 'content-length': '223',\n", | |
| " 'connection': 'keep-alive'},\n", | |
| " 'RetryAttempts': 0}}" | |
| ] | |
| }, | |
| "execution_count": 24, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "forecast.describe_dataset_group(DatasetGroupName=datasetGroupName)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Create Data Import Job\n", | |
| "Brings the data into Amazon Forecast system ready to forecast from raw data. " | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 25, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "ds_import_job_response=forecast.create_dataset_import_job(DatasetName=datasetName,Delimiter=',', DatasetGroupName =datasetGroupName ,S3Uri= s3DataPath)\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 26, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "2ea4b607\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "ds_versionId=ds_import_job_response['VersionId']\n", | |
| "print(ds_versionId)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Check the status of dataset, when the status change from **CREATING** to **ACTIVE**, we can continue to next steps. Depending on the data size. It can take 10 mins to be **ACTIVE**. This process will take 5 to 10 minutes." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 27, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "QUEUED\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "ACTIVE\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "while True:\n", | |
| " dataImportStatus = forecast.describe_dataset_import_job(DatasetName=datasetName,VersionId=ds_versionId)['Status']\n", | |
| " print(dataImportStatus)\n", | |
| " if dataImportStatus != 'ACTIVE' and dataImportStatus != 'FAILED':\n", | |
| " sleep(30)\n", | |
| " else:\n", | |
| " break" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 28, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'DatasetArn': 'arn:aws:forecast:us-west-2:059124553121:ds/workshop_demo_cd_ds',\n", | |
| " 'DatasetName': 'workshop_demo_cd_ds',\n", | |
| " 'VersionId': '2ea4b607',\n", | |
| " 'Status': 'ACTIVE',\n", | |
| " 'FieldStatistics': {'date': {'Count': 26280,\n", | |
| " 'CountDistinct': 8760,\n", | |
| " 'CountNull': 0,\n", | |
| " 'Min': '2014-01-01T01:00:00Z',\n", | |
| " 'Max': '2015-01-01T00:00:00Z'},\n", | |
| " 'item': {'Count': 26280, 'CountDistinct': 3, 'CountNull': 0},\n", | |
| " 'target': {'Count': 26280,\n", | |
| " 'CountDistinct': 5059,\n", | |
| " 'CountNull': 0,\n", | |
| " 'Min': '0.0',\n", | |
| " 'Max': '212.27197346600326',\n", | |
| " 'Avg': 50.82350576202014,\n", | |
| " 'Stddev': 37.9125549309785}},\n", | |
| " 'ResponseMetadata': {'RequestId': '8f9fa634-227b-424e-9035-05070d874baf',\n", | |
| " 'HTTPStatusCode': 200,\n", | |
| " 'HTTPHeaders': {'content-type': 'application/x-amz-json-1.1',\n", | |
| " 'date': 'Sun, 20 Jan 2019 19:14:12 GMT',\n", | |
| " 'x-amzn-requestid': '8f9fa634-227b-424e-9035-05070d874baf',\n", | |
| " 'content-length': '496',\n", | |
| " 'connection': 'keep-alive'},\n", | |
| " 'RetryAttempts': 0}}" | |
| ] | |
| }, | |
| "execution_count": 28, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "forecast.describe_dataset_import_job(DatasetName=datasetName,VersionId=ds_versionId)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Recipe" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 29, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'RecipeNames': ['forecast_ARIMA',\n", | |
| " 'forecast_DEEP_AR',\n", | |
| " 'forecast_DEEP_AR_PLUS',\n", | |
| " 'forecast_ETS',\n", | |
| " 'forecast_MDN',\n", | |
| " 'forecast_MQRNN',\n", | |
| " 'forecast_NPTS',\n", | |
| " 'forecast_PROPHET',\n", | |
| " 'forecast_SQF'],\n", | |
| " 'ResponseMetadata': {'RequestId': '44053e23-9e23-4c36-9b28-8ba200b10b0a',\n", | |
| " 'HTTPStatusCode': 200,\n", | |
| " 'HTTPHeaders': {'content-type': 'application/x-amz-json-1.1',\n", | |
| " 'date': 'Sun, 20 Jan 2019 19:14:13 GMT',\n", | |
| " 'x-amzn-requestid': '44053e23-9e23-4c36-9b28-8ba200b10b0a',\n", | |
| " 'content-length': '174',\n", | |
| " 'connection': 'keep-alive'},\n", | |
| " 'RetryAttempts': 0}}" | |
| ] | |
| }, | |
| "execution_count": 29, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "recipesResponse=forecast.list_recipes()\n", | |
| "recipesResponse" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Get details about each recipe." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 30, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'Recipe': {'Name': 'forecast_MQRNN',\n", | |
| " 'Train': [{'TrainingInfo': {'TrainedModelName': 'algorithm_MQRNN',\n", | |
| " 'AlgorithmName': 'MQRNN',\n", | |
| " 'TrainingParameters': {'epochs': '60',\n", | |
| " 'learning_rate': '3E-3',\n", | |
| " 'mini_batch_size': '32',\n", | |
| " 'quantiles': '[0.1,0.5,0.9]'}},\n", | |
| " 'BackTestWindowCount': 1,\n", | |
| " 'MetricsBuckets': []}]},\n", | |
| " 'ResponseMetadata': {'RequestId': '2bbd1fc9-461c-4211-9c87-898d2daaed1b',\n", | |
| " 'HTTPStatusCode': 200,\n", | |
| " 'HTTPHeaders': {'content-type': 'application/x-amz-json-1.1',\n", | |
| " 'date': 'Sun, 20 Jan 2019 19:14:15 GMT',\n", | |
| " 'x-amzn-requestid': '2bbd1fc9-461c-4211-9c87-898d2daaed1b',\n", | |
| " 'content-length': '281',\n", | |
| " 'connection': 'keep-alive'},\n", | |
| " 'RetryAttempts': 0}}" | |
| ] | |
| }, | |
| "execution_count": 30, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "forecast.describe_recipe(RecipeName='forecast_MQRNN')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Create Solution with customer forecast horizon" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Forecast horizon is how long in future the forecast should be predicting. For weekly data, a value of 12 means 1 weeks. Our example is hourly data, we try forecast the next day, so we can set to 24." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 31, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "predictorName= project+'_mqrnn'" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 32, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "forecastHorizon = 24" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 33, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "createPredictorResponse=forecast.create_predictor(RecipeName='forecast_MQRNN',DatasetGroupName= datasetGroupName ,PredictorName=predictorName, \n", | |
| " ForecastHorizon = forecastHorizon)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 34, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "predictorVerionId=createPredictorResponse['VersionId']" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 35, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'PredictorVersions': [{'PredictorName': 'workshop_demo_cd_mqrnn',\n", | |
| " 'VersionId': '95e49094'}],\n", | |
| " 'ResponseMetadata': {'RequestId': '1d2b4019-c13e-4899-81a2-a5070b1c80dc',\n", | |
| " 'HTTPStatusCode': 200,\n", | |
| " 'HTTPHeaders': {'content-type': 'application/x-amz-json-1.1',\n", | |
| " 'date': 'Sun, 20 Jan 2019 19:14:19 GMT',\n", | |
| " 'x-amzn-requestid': '1d2b4019-c13e-4899-81a2-a5070b1c80dc',\n", | |
| " 'content-length': '89',\n", | |
| " 'connection': 'keep-alive'},\n", | |
| " 'RetryAttempts': 0}}" | |
| ] | |
| }, | |
| "execution_count": 35, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "forecast.list_predictor_versions(PredictorName=predictorName)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Check the status of solutions, when the status change from **CREATING** to **ACTIVE**, we can continue to next steps. Depending on data size, model selection and hyper parameters,it can take 10 mins to more than one hour to be **ACTIVE**." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 36, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "ACTIVE\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "while True:\n", | |
| " predictorStatus = forecast.describe_predictor(PredictorName=predictorName,VersionId=predictorVerionId)['Status']\n", | |
| " print(predictorStatus)\n", | |
| " if predictorStatus != 'ACTIVE' and predictorStatus != 'FAILED':\n", | |
| " sleep(30)\n", | |
| " else:\n", | |
| " break" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Get Error Metrics" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 37, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'ModelMetrics': {'MQRNN': {'Metrics': {'p10': '0.10786990807782745',\n", | |
| " 'p50': '0.27334018633122853',\n", | |
| " 'p90': '0.19193589801618308',\n", | |
| " 'rmse': '18.92621915098378'},\n", | |
| " 'MetricsByBucket': []}},\n", | |
| " 'ResponseMetadata': {'RequestId': '77386c45-be27-426e-b1fc-ce07889d5faa',\n", | |
| " 'HTTPStatusCode': 200,\n", | |
| " 'HTTPHeaders': {'content-type': 'application/x-amz-json-1.1',\n", | |
| " 'date': 'Sun, 20 Jan 2019 19:38:27 GMT',\n", | |
| " 'x-amzn-requestid': '77386c45-be27-426e-b1fc-ce07889d5faa',\n", | |
| " 'content-length': '172',\n", | |
| " 'connection': 'keep-alive'},\n", | |
| " 'RetryAttempts': 0}}" | |
| ] | |
| }, | |
| "execution_count": 37, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "forecastquery.get_accuracy_metrics(PredictorName=predictorName)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Deploy Predictor" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 38, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'PredictorName': 'workshop_demo_cd_mqrnn',\n", | |
| " 'VersionId': '95e49094',\n", | |
| " 'PredictorArn': 'arn:aws:forecast:us-west-2:059124553121:predictor/workshop_demo_cd_mqrnn',\n", | |
| " 'ResponseMetadata': {'RequestId': '58412a13-7931-4bdd-8a58-f67d194f7b3c',\n", | |
| " 'HTTPStatusCode': 200,\n", | |
| " 'HTTPHeaders': {'content-type': 'application/x-amz-json-1.1',\n", | |
| " 'date': 'Sun, 20 Jan 2019 19:38:28 GMT',\n", | |
| " 'x-amzn-requestid': '58412a13-7931-4bdd-8a58-f67d194f7b3c',\n", | |
| " 'content-length': '155',\n", | |
| " 'connection': 'keep-alive'},\n", | |
| " 'RetryAttempts': 0}}" | |
| ] | |
| }, | |
| "execution_count": 38, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "forecast.deploy_predictor(PredictorName=predictorName)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 39, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "{'PredictorNames': ['workshop_demo_cd_mqrnn', 'workshop_demo_mcd_mqrnn', 'workshop_mqrnn', 'workshop_mqrnn_pn', 'workshop_mqrnn_pn2', 'workshopsagemaker_mqrnn', 'workshopsagemakerclean_mqrnn'], 'ResponseMetadata': {'RequestId': 'e220c76d-1494-4c63-9cee-489dbff8c7c6', 'HTTPStatusCode': 200, 'HTTPHeaders': {'content-type': 'application/x-amz-json-1.1', 'date': 'Sun, 20 Jan 2019 19:38:30 GMT', 'x-amzn-requestid': 'e220c76d-1494-4c63-9cee-489dbff8c7c6', 'content-length': '186', 'connection': 'keep-alive'}, 'RetryAttempts': 0}}\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "deployedPredictorsResponse=forecast.list_deployed_predictors()\n", | |
| "print(deployedPredictorsResponse)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Please note that the following cell can also take 10 minutes or more to be fully operational. There's no output here, but that is fine as long as the * is there." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 40, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "ACTIVE\n", | |
| "ACTIVE\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "while True:\n", | |
| " deployedPredictorStatus = forecast.describe_deployed_predictor(PredictorName=predictorName)['Status']\n", | |
| " print(deployedPredictorStatus)\n", | |
| " if deployedPredictorStatus != 'ACTIVE' and deployedPredictorStatus != 'FAILED':\n", | |
| " sleep(30)\n", | |
| " else:\n", | |
| " break\n", | |
| "print(deployedPredictorStatus)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "# Now with just a date" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 52, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": [ | |
| "<div>\n", | |
| "<style scoped>\n", | |
| " .dataframe tbody tr th:only-of-type {\n", | |
| " vertical-align: middle;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe tbody tr th {\n", | |
| " vertical-align: top;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe thead th {\n", | |
| " text-align: right;\n", | |
| " }\n", | |
| "</style>\n", | |
| "<table border=\"1\" class=\"dataframe\">\n", | |
| " <thead>\n", | |
| " <tr style=\"text-align: right;\">\n", | |
| " <th></th>\n", | |
| " <th>timestamp</th>\n", | |
| " <th>target_value</th>\n", | |
| " <th>item_id</th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <th>0</th>\n", | |
| " <td>2014-01-01 01:00:00</td>\n", | |
| " <td>38.34991708126038</td>\n", | |
| " <td>client_12</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>1</th>\n", | |
| " <td>2014-01-01 02:00:00</td>\n", | |
| " <td>33.5820895522388</td>\n", | |
| " <td>client_12</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2</th>\n", | |
| " <td>2014-01-01 03:00:00</td>\n", | |
| " <td>34.41127694859037</td>\n", | |
| " <td>client_12</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "</div>" | |
| ], | |
| "text/plain": [ | |
| " timestamp target_value item_id\n", | |
| "0 2014-01-01 01:00:00 38.34991708126038 client_12\n", | |
| "1 2014-01-01 02:00:00 33.5820895522388 client_12\n", | |
| "2 2014-01-01 03:00:00 34.41127694859037 client_12" | |
| ] | |
| }, | |
| "execution_count": 52, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "import pandas as pd\n", | |
| "df = pd.read_csv(\"../data/item-demand-time.csv\", dtype = object, header=None, names=[\"timestamp\", \"target_value\", \"item_id\"], parse_dates=[0])\n", | |
| "df.head(3)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 53, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "df['timestamp'] = df['timestamp'].dt.date" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 55, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": [ | |
| "<div>\n", | |
| "<style scoped>\n", | |
| " .dataframe tbody tr th:only-of-type {\n", | |
| " vertical-align: middle;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe tbody tr th {\n", | |
| " vertical-align: top;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe thead th {\n", | |
| " text-align: right;\n", | |
| " }\n", | |
| "</style>\n", | |
| "<table border=\"1\" class=\"dataframe\">\n", | |
| " <thead>\n", | |
| " <tr style=\"text-align: right;\">\n", | |
| " <th></th>\n", | |
| " <th>timestamp</th>\n", | |
| " <th>target_value</th>\n", | |
| " <th>item_id</th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <th>0</th>\n", | |
| " <td>2014-01-01</td>\n", | |
| " <td>38.34991708126038</td>\n", | |
| " <td>client_12</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>1</th>\n", | |
| " <td>2014-01-01</td>\n", | |
| " <td>33.5820895522388</td>\n", | |
| " <td>client_12</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2</th>\n", | |
| " <td>2014-01-01</td>\n", | |
| " <td>34.41127694859037</td>\n", | |
| " <td>client_12</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>3</th>\n", | |
| " <td>2014-01-01</td>\n", | |
| " <td>39.800995024875625</td>\n", | |
| " <td>client_12</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>4</th>\n", | |
| " <td>2014-01-01</td>\n", | |
| " <td>41.044776119402975</td>\n", | |
| " <td>client_12</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "</div>" | |
| ], | |
| "text/plain": [ | |
| " timestamp target_value item_id\n", | |
| "0 2014-01-01 38.34991708126038 client_12\n", | |
| "1 2014-01-01 33.5820895522388 client_12\n", | |
| "2 2014-01-01 34.41127694859037 client_12\n", | |
| "3 2014-01-01 39.800995024875625 client_12\n", | |
| "4 2014-01-01 41.044776119402975 client_12" | |
| ] | |
| }, | |
| "execution_count": 55, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "df.head()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 57, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "# Write the file to disk.\n", | |
| "\n", | |
| "df.to_csv(\"../data/item-demand-date.csv\", header=None)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 58, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "# upload to s3\n", | |
| "s3 = session.client('s3')\n", | |
| "accountId = boto3.client('sts').get_caller_identity().get('Account')\n", | |
| "bucketName = 'amazon-forecast-chrisking-data'# Update the unique-value bit here.\n", | |
| "key=\"elec_data/item-demand-date.csv\"\n", | |
| "s3.upload_file(Filename=\"../data/item-demand-date.csv\", Bucket=bucketName, Key=key)\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 59, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "DATASET_FREQUENCY = \"H\" \n", | |
| "TIMESTAMP_FORMAT = \"yyyy-MM-dd\"" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 60, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "project = 'workshop_demo_date' # Replace this with a unique name here, make sure the entire name is < 30 characters.\n", | |
| "datasetName= project+'_ds'\n", | |
| "datasetGroupName= project +'_gp'\n", | |
| "s3DataPath = \"s3://\"+bucketName+\"/\"+key" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 61, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "# Specify the schema of your dataset here. Make sure the order of columns matches the raw data files.\n", | |
| "schema ={\n", | |
| " \"Attributes\":[\n", | |
| " {\n", | |
| " \"AttributeName\":\"timestamp\",\n", | |
| " \"AttributeType\":\"timestamp\"\n", | |
| " },\n", | |
| " {\n", | |
| " \"AttributeName\":\"target_value\",\n", | |
| " \"AttributeType\":\"float\"\n", | |
| " },\n", | |
| " {\n", | |
| " \"AttributeName\":\"item_id\",\n", | |
| " \"AttributeType\":\"string\"\n", | |
| " }\n", | |
| " ]\n", | |
| "}\n", | |
| "\n", | |
| "response=forecast.create_dataset(\n", | |
| " Domain=\"CUSTOM\",\n", | |
| " DatasetType='TARGET_TIME_SERIES',\n", | |
| " DataFormat='CSV',\n", | |
| " DatasetName=datasetName,\n", | |
| " DataFrequency=DATASET_FREQUENCY, \n", | |
| " TimeStampFormat=TIMESTAMP_FORMAT,\n", | |
| " Schema = schema\n", | |
| " )" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 62, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'DatasetName': 'workshop_demo_date_ds',\n", | |
| " 'DatasetType': 'TARGET_TIME_SERIES',\n", | |
| " 'DataFormat': 'CSV',\n", | |
| " 'Domain': 'CUSTOM',\n", | |
| " 'ScheduleExpression': 'none',\n", | |
| " 'DatasetArn': 'arn:aws:forecast:us-west-2:059124553121:ds/workshop_demo_date_ds',\n", | |
| " 'Status': 'ACTIVE',\n", | |
| " 'ResponseMetadata': {'RequestId': 'fad4c66a-158f-4420-b688-dbafbdb8f207',\n", | |
| " 'HTTPStatusCode': 200,\n", | |
| " 'HTTPHeaders': {'content-type': 'application/x-amz-json-1.1',\n", | |
| " 'date': 'Mon, 21 Jan 2019 00:26:24 GMT',\n", | |
| " 'x-amzn-requestid': 'fad4c66a-158f-4420-b688-dbafbdb8f207',\n", | |
| " 'content-length': '237',\n", | |
| " 'connection': 'keep-alive'},\n", | |
| " 'RetryAttempts': 0}}" | |
| ] | |
| }, | |
| "execution_count": 62, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "forecast.describe_dataset(DatasetName=datasetName)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 63, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'DatasetGroupName': 'workshop_demo_date_gp',\n", | |
| " 'DatasetGroupArn': 'arn:aws:forecast:us-west-2:059124553121:dsgroup/workshop_demo_date_gp',\n", | |
| " 'ResponseMetadata': {'RequestId': '2dbdd164-d931-4e48-ac40-3ac1ae88e7bf',\n", | |
| " 'HTTPStatusCode': 200,\n", | |
| " 'HTTPHeaders': {'content-type': 'application/x-amz-json-1.1',\n", | |
| " 'date': 'Mon, 21 Jan 2019 00:26:32 GMT',\n", | |
| " 'x-amzn-requestid': '2dbdd164-d931-4e48-ac40-3ac1ae88e7bf',\n", | |
| " 'content-length': '134',\n", | |
| " 'connection': 'keep-alive'},\n", | |
| " 'RetryAttempts': 0}}" | |
| ] | |
| }, | |
| "execution_count": 63, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "forecast.create_dataset_group(DatasetGroupName=datasetGroupName,RoleArn=roleArn,DatasetNames=[datasetName])" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 64, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'DatasetGroupName': 'workshop_demo_date_gp',\n", | |
| " 'DatasetGroupArn': 'arn:aws:forecast:us-west-2:059124553121:dsgroup/workshop_demo_date_gp',\n", | |
| " 'Datasets': ['workshop_demo_date_ds'],\n", | |
| " 'RoleArn': 'arn:aws:iam::059124553121:role/amazonforecast',\n", | |
| " 'ResponseMetadata': {'RequestId': '6b2afd40-995a-4ffb-9a54-42d17ebe1442',\n", | |
| " 'HTTPStatusCode': 200,\n", | |
| " 'HTTPHeaders': {'content-type': 'application/x-amz-json-1.1',\n", | |
| " 'date': 'Mon, 21 Jan 2019 00:26:39 GMT',\n", | |
| " 'x-amzn-requestid': '6b2afd40-995a-4ffb-9a54-42d17ebe1442',\n", | |
| " 'content-length': '229',\n", | |
| " 'connection': 'keep-alive'},\n", | |
| " 'RetryAttempts': 0}}" | |
| ] | |
| }, | |
| "execution_count": 64, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "forecast.describe_dataset_group(DatasetGroupName=datasetGroupName)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 65, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "ds_import_job_response=forecast.create_dataset_import_job(DatasetName=datasetName,Delimiter=',', DatasetGroupName =datasetGroupName ,S3Uri= s3DataPath)\n", | |
| "\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 66, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "f623bbe8\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "ds_versionId=ds_import_job_response['VersionId']\n", | |
| "print(ds_versionId)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 67, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "CREATING\n", | |
| "FAILED\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "while True:\n", | |
| " dataImportStatus = forecast.describe_dataset_import_job(DatasetName=datasetName,VersionId=ds_versionId)['Status']\n", | |
| " print(dataImportStatus)\n", | |
| " if dataImportStatus != 'ACTIVE' and dataImportStatus != 'FAILED':\n", | |
| " sleep(30)\n", | |
| " else:\n", | |
| " break" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "conda_python3", | |
| "language": "python", | |
| "name": "conda_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.6.5" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 2 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment