Skip to content

Commit

Permalink
add related notebook (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuzhoujianxia authored and akhilrazdan committed Sep 10, 2019
1 parent 6a19cdf commit 5b8b956
Show file tree
Hide file tree
Showing 17 changed files with 2,078 additions and 0 deletions.
1,968 changes: 1,968 additions & 0 deletions notebooks/6.Incorporating_Related_Time_Series_dataset_to_your_Predictor.ipynb

Large diffs are not rendered by default.

Binary file added notebooks/BlogImages/amazon_forecast.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added notebooks/BlogImages/backtest.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added notebooks/BlogImages/data_format.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added notebooks/BlogImages/forecast_workflow.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added notebooks/BlogImages/outline.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added notebooks/BlogImages/overview.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added notebooks/BlogImages/pred_details.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added notebooks/BlogImages/recipe.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added notebooks/BlogImages/recipes.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added notebooks/BlogImages/rts_viz.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added notebooks/BlogImages/timestamp.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added notebooks/BlogImages/types_data.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file added notebooks/util/__init__.py
Empty file.
Binary file added notebooks/util/__pycache__/__init__.cpython-37.pyc
Binary file not shown.
Binary file not shown.
110 changes: 110 additions & 0 deletions notebooks/util/fcst_utils.py
@@ -0,0 +1,110 @@
import time
import boto3
import json
import pandas as pd
import logging
import matplotlib.pyplot as plt

def wait_till_delete(callback, check_time = 5, timeout = 180):
elapsed_time = 0
while elapsed_time < timeout:
try:
out = callback()
except Exception as e:
# When given the resource not found exception, deletion has occured
if e.response['Error']['Code'] == 'ResourceNotFoundException':
logging.info('Successful delete\n')
return
# Fails with other error
logging.info(f'Deletion failed: {e}')
return(e)
time.sleep(check_time) # units of seconds
elapsed_time += check_time

def wait(callback, time_interval=30):
last_status = callback()['Status']
time.sleep(time_interval)
elapsed_time = time_interval
is_failed = True

while (last_status != 'ACTIVE'):
last_status = callback()['Status']
time.sleep(time_interval) # units of seconds
elapsed_time += time_interval
print('.', end='', flush=True)
if last_status == 'CREATE_FAILED':
break
if last_status == "ACTIVE":
is_failed = False
job_status = "failed" if is_failed else "success"
print('')
logging.info(f"Finished in {elapsed_time} seconds with status {job_status}")
return not is_failed

def load_exact_sol(fname, item_id):
exact = pd.read_csv(fname, header = None)
exact.columns = ['item_id', 'timestamp', 'target']
return exact.loc[exact['item_id'] == item_id]

def get_or_create_role_arn():
iam = boto3.client("iam")
role_name = "ForecastRoleDemo"
assume_role_policy_document = {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "forecast.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
role_arn = None
try:
create_role_response = iam.create_role(
RoleName = role_name,
AssumeRolePolicyDocument = json.dumps(assume_role_policy_document)
)
role_arn = create_role_response["Role"]["Arn"]
except iam.exceptions.EntityAlreadyExistsException:
print("The role " + role_name + "exists, ignore to create it")
role_arn = boto3.resource('iam').Role(role_name).arn
# AmazonPersonalizeFullAccess provides access to any S3 bucket with a name that includes "personalize" or "Personalize"
# if you would like to use a bucket with a different name, please consider creating and attaching a new policy
# that provides read access to your bucket or attaching the AmazonS3ReadOnlyAccess policy to the role
policy_arn = "arn:aws:iam::aws:policy/AmazonForecastFullAccess"
iam.attach_role_policy(
RoleName = role_name,
PolicyArn = policy_arn
)

# Now add S3 support

iam.attach_role_policy(
PolicyArn='arn:aws:iam::aws:policy/AmazonS3FullAccess',
RoleName=role_name
)
time.sleep(60) # wait for a minute to allow IAM role policy attachment to propagate
print(role_arn)
return role_arn


def plot_forecasts(fcsts, exact, freq = '1H', forecastHorizon=24, time_back = 80):
p10 = pd.DataFrame(fcsts['Forecast']['Predictions']['p10'])
p50 = pd.DataFrame(fcsts['Forecast']['Predictions']['p50'])
p90 = pd.DataFrame(fcsts['Forecast']['Predictions']['p90'])
pred_int = p50['Timestamp'].apply(lambda x: pd.Timestamp(x))
fcst_start_date = pred_int[0]
time_int = exact['timestamp'].apply(lambda x: pd.Timestamp(x))
plt.plot(time_int[-time_back:],exact['target'].values[-time_back:], color = 'r')
plt.plot(pred_int, p50['Value'].values, color = 'k');
plt.fill_between(p50['Timestamp'].values,
p10['Value'].values,
p90['Value'].values,
color='b', alpha=0.3);
plt.axvline(x=pd.Timestamp(fcst_start_date), linewidth=3, color='g', ls='dashed');
plt.axvline(x=pd.Timestamp(fcst_start_date, freq)+forecastHorizon-1, linewidth=3, color='g', ls='dashed');
plt.xticks(rotation=30);
plt.legend(['Target', 'Forecast'], loc = 'lower left')

0 comments on commit 5b8b956

Please sign in to comment.