Created
May 27, 2020 02:56
-
-
Save chezou/d986ce3e908bb6e355ac4eaf97d23447 to your computer and use it in GitHub Desktop.
Ex2-3_plot_saled_prediction.ipynb
This file contains 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
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "Ex2-3_plot_saled_prediction.ipynb", | |
"provenance": [], | |
"collapsed_sections": [], | |
"authorship_tag": "ABX9TyMeNqSBh9oOcCUuEddfbezz", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/chezou/d986ce3e908bb6e355ac4eaf97d23447/ex2-3_plot_saled_prediction.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "b5_NIT3Oag7A", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"# 演習 2-3 売上データの予測結果をグラフに描画しよう\n", | |
"\n", | |
"Facebook Prophetで予測をした売上をJupyter notebookを使いプロットしてみましょう。" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "SAC2PQKqbPin", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"## pytdをインストールする\n", | |
"\n", | |
"このセルはローカル環境では実行しなくても構いませんが、Google Colaboratoryなどの環境では初回実行時に必ず実行してください。" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "jUOS9-D-Z1UD", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"# Install pytd\n", | |
"!pip install -q pytd" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "kswk78nEbbWG", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"## TD API keyを設定する\n", | |
"\n", | |
"TDの読み書きができるmaster API keyを設定します。\n", | |
"\n", | |
"API keyは以下のURLから確認できます。\n", | |
"https://console.treasuredata.com/app/mp/ak\n", | |
"\n", | |
"\"Input your Treasure Data API key\"の下のボックスにAPIキーをコピー&ペーストをしてください。" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "Pd5rPRW3ZnQX", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"# Set your TD API key\n", | |
"import getpass\n", | |
"\n", | |
"print(\"Input your Treasure Data API key\")\n", | |
"apikey = getpass.getpass()\n", | |
"print(\"Succeeded\")" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "vE4VfgoTcOQt", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"## 各種設定を変更する\n", | |
"\n", | |
"下のセルで `db_name` の値を `timeseries` から `timeseries_YOURNAME` のようにYOURNAMEの部分を名前などに置き換えてください。\n", | |
"また、USリージョン以外の場合は、 `endpoint` も適宜書き換えてください。\n", | |
"\n", | |
"- 東京: \"https://api.treasuredata.co.jp\"\n", | |
"- EU: \"https://api.eu01.treasuredata.com\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "b6YL8NJNZt8z", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"# Set configurations\n", | |
"db_name = \"timeseries\"\n", | |
"endpoint = \"https://api.treasuredata.com\"" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "MRr5djaHc5Td", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"## グラフ描画用の関数を定義する\n", | |
"\n", | |
"次のセルではProphetのグラフを描画するための関数を定義しています。詳細は飛ばしてしまって構いません。" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "D3aRc87kZK-K", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"%matplotlib inline\n", | |
"\n", | |
"# Define function for plot\n", | |
"\n", | |
"from matplotlib import pyplot as plt\n", | |
"from matplotlib.dates import (\n", | |
" AutoDateLocator,\n", | |
" AutoDateFormatter,\n", | |
")\n", | |
"\n", | |
"# Equivalent code with model.plot()\n", | |
"# https://github.com/facebook/prophet/blob/ca9a49d328ab1f2a991f246a3ebc37a7f9c896c5/python/fbprophet/plot.py#L41-L88\n", | |
"def plot(df, df_prev, xlabel='ds', ylabel='y'):\n", | |
" fig = plt.figure(facecolor=\"w\", figsize=(10,6))\n", | |
" ax = fig.add_subplot(111)\n", | |
"\n", | |
" fcst_t = pd.to_datetime(df[\"ds\"])\n", | |
" ax.plot(pd.to_datetime(df_prev[\"ds\"]), df_prev[\"y\"], \"k.\")\n", | |
" ax.plot(fcst_t, df[\"yhat\"], ls=\"-\", c=\"#0072B2\")\n", | |
" if \"cap\" in df:\n", | |
" ax.plot(fcst_t, df[\"cap\"], ls=\"--\", c=\"k\")\n", | |
"\n", | |
" if \"floor\" in df:\n", | |
" ax.plot(fcst_t, df[\"floor\"], ls=\"--\", c=\"k\")\n", | |
"\n", | |
" ax.fill_between(fcst_t, df[\"yhat_lower\"], df[\"yhat_upper\"], color=\"#0072B2\", alpha=0.2)\n", | |
" locator = AutoDateLocator(interval_multiples=False)\n", | |
" formatter = AutoDateFormatter(locator)\n", | |
" ax.xaxis.set_major_locator(locator)\n", | |
" ax.xaxis.set_major_formatter(formatter)\n", | |
" ax.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2)\n", | |
" ax.set_xlabel(xlabel)\n", | |
" ax.set_ylabel(ylabel)\n", | |
" fig.tight_layout()\n", | |
"\n", | |
" return fig" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "buV3aaGkdCoY", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"## TDから過去の売上データを取得する\n", | |
"\n", | |
"過去の売上データをpytdを使い取得します。また、取得しデータを折れ線グラフでプロットします。" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "ZrTi8NGYZSaH", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"# Load past sales data\n", | |
"import pytd\n", | |
"import pandas as pd\n", | |
"\n", | |
"pytd_client = pytd.Client(database=db_name, apikey=apikey, endpoint=endpoint)\n", | |
"df_prev = pd.DataFrame(**pytd_client.query(\"select * from retail_sales order by ds\"))\n", | |
"df_prev" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "ZwZR3dK_Za2a", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"# Plot past sales data\n", | |
"df_prev.plot(x=\"ds\", y=\"y\", kind=\"line\", figsize=(10, 6))" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "1IBt5i0KdRP5", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"## 予測結果をTDから取得する\n", | |
"\n", | |
"Custom Scripts環境で予測した結果をpytdで取得し、その結果をプロットします。" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "ofL1wDVkZZr4", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"# Load predicted sales data\n", | |
"df = pd.DataFrame(**pytd_client.query(\"select * from predicted_sales order by ds\"))\n", | |
"df" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "hjWipINnZd60", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"# Plot predicted sales data\n", | |
"fig = plot(df, df_prev)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
URL for binder: https://mybinder.org/v2/gist/chezou/d986ce3e908bb6e355ac4eaf97d23447/master?filepath=ex2-3_plot_saled_prediction.ipynb