{"cells":[
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n%matplotlib inline\n\nfrom bokeh.plotting import figure, output_notebook, show, vplot, ColumnDataSource\nfrom bokeh.charts import TimeSeries\nfrom bokeh.models import HoverTool, CrosshairTool\nfrom bokeh.palettes import brewer\nimport gc\nimport dask.dataframe as dd\n\noutput_notebook()"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "## This Notebook analyses trends of hotel bookings and clicks\n\nwe will start with reading the data, leaving just necessary columns, aggregating it to the day level and dropping the original dataframe"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": ""
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "train  =  pd.read_csv('../input/train.csv', usecols = ('date_time', 'hotel_cluster','is_booking'), \n                      parse_dates = ['date_time'])"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "train['dow'] = train.date_time.dt.weekday\ntrain['year'] = train.date_time.dt.year\ntrain['month'] = train.date_time.dt.month\ntrain['day'] = train.date_time.dt.day"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "train_agg = train.groupby(['dow','year','month','day', 'hotel_cluster']).agg(['sum', 'count'] )\ntrain_agg.columns = ('bookings', 'total')\ntrain_agg.head()"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "train_agg.info()\n"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "del(train)\n\ngc.collect()"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "## Bookings per day of week"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": ""
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "date_agg_1 = train_agg.groupby(level=0).agg(['sum'] )\ndate_agg_1.columns = ('bookings', 'total')\ndate_agg_1.head()"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "date_agg_1.plot( kind = 'bar', stacked = True )"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "## Bookings by year"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": ""
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "date_agg_2 = train_agg.groupby(level=1).sum()\ndate_agg_2.columns = ('bookings', 'total')\ndate_agg_2.index.name = 'Year'\ndate_agg_2.plot(kind='bar', stacked='True')"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "## Bookings by month"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": ""
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "date_agg_3 = train_agg.groupby(level=[1,2]).sum()\ndate_agg_3.columns = ('bookings', 'total')\ndate_agg_3.plot(kind='bar', stacked='True',figsize=(16,10))"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "## Interactive booking, click, and percentage of booking trends with Bokeh"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": ""
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "date_agg_4 = train_agg.groupby(level=[1,2,3]).sum()\ndate_agg_4.columns = ('bookings', 'total')\ndate_agg_4.reset_index(inplace=True)\ndate_agg_4['dt'] = pd.to_datetime(date_agg_4.year*10000 + date_agg_4.month*100 + date_agg_4.day\n                                  , format='%Y%m%d')\ndate_agg_4.head()"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "def make_plot(vals, title, ylab):\n    hover = HoverTool(\n        tooltips=[\n            (\"Date\", \"@day\"),\n            (\"Day of week\", \"@dow\"),\n            (\"clicks\", \"@clicks\"),\n            (\"bookings\", \"@bookings\"),\n        ]\n    )\n\n\n    ch = CrosshairTool(dimensions = ['height'], line_color='red')\n\n    src  = ColumnDataSource({'day': date_agg_4.dt.dt.strftime('%Y-%m-%d') ,\n                             'dow': date_agg_4.dt.dt.weekday.tolist(),\n                             'clicks': date_agg_4.total - date_agg_4.bookings,\n                             'bookings': date_agg_4.bookings})\n\n    p = figure(x_axis_type = 'datetime',plot_width=800, plot_height=400, tools=[hover, ch, \n                                                                                'pan,save, wheel_zoom,box_zoom,reset,resize'])\n    p.title = title\n    p.xaxis.axis_label = 'Date'\n    p.yaxis.axis_label = ylab\n\n    p.line((date_agg_4['dt']), vals, color='green',  source = src)\n    \n    return p"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "p = make_plot(date_agg_4['total'] - date_agg_4['bookings'], 'Expedia daily clicks', 'Clicks')\nshow(p)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "p = make_plot(date_agg_4['bookings'], 'Expedia daily bookings', 'bookings')\nshow(p)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "p = make_plot(date_agg_4['bookings'] / date_agg_4['total'] *100, 'Expedia daily bookings %', 'Bookings, %')\nshow(p)"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "## Building interactive charts for hotel clusters, 5 clusters per chart"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": ""
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "pv_agg = train_agg.reset_index()\npv_agg['dt'] = pd.to_datetime( pv_agg.year*10000 + pv_agg.month*100 + pv_agg.day\n                                  , format='%Y%m%d')\npv_agg = pv_agg.pivot(index = 'dt', columns = 'hotel_cluster', values = 'bookings')\npv_agg.columns = [str(i) for i in pv_agg.columns]\npv_agg['dt'] = pv_agg.index\npv_agg['dow'] = pv_agg.dt.dt.weekday\npv_agg.head()"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "def make_hc_plot(df, start, stop):\n    hover = HoverTool(\n        tooltips=[\n            (\"Date\", \"@day\"),\n            (\"Day of week\", \"@dow\"),\n            (\"cluster\", \"@cluster\"),\n            (\"bookings\", \"@bookings\"),\n        ]\n    )\n    \n    #colors = brewer['RdYlBu'][stop-start]\n    colors = ['red', 'darkmagenta', 'green', 'darkorange', 'blue']\n    ch = CrosshairTool(dimensions = ['height'], line_color='red')\n    p = figure(x_axis_type = 'datetime',plot_width=800, plot_height=400, tools=[hover, ch, 'pan,wheel_zoom,save,box_zoom,reset,resize'])\n    p.title = 'Expedia bookings for subset of clusters {} to {}'.format(start, stop)\n    p.xaxis.axis_label = 'Date'\n    p.yaxis.axis_label = 'Number of bookings'\n    \n    for i in range(start, stop):\n        src  = ColumnDataSource({'day': df.dt.dt.strftime('%Y-%m-%d'), \n                                 'dow': df.dow.tolist(),\n                                 'bookings': df[str(i)],\n                                 'cluster': [i]*df.shape[0]})\n        \n        p.line((df['dt']), df[str(i)], color=colors[i-start], legend = str(i), source = src)\n\n    return p"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "tslines = []\n\nfor i in range(0,100,5):\n    tsline = make_hc_plot(pv_agg, i, i+5)\n    tslines.append(tsline)\n    \nshow(vplot(*tslines))"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "print('done')"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": ""
 }
],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"}}, "nbformat": 4, "nbformat_minor": 0}