{"cells":[{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"4227ffa2-9df4-473e-bc67-d0800dd6ea50"},"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":{"_cell_guid":"d0d5088d-df30-410f-b2bf-88644deae5b1"},"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":{"_cell_guid":"8b6b0683-dc42-4d94-a976-d83d1c178bc5"},"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":{"_cell_guid":"47dcfba1-3607-4a7a-b098-432523e9a1f1"},"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":{"_cell_guid":"9839ae80-699a-4a35-a7a6-2752763e57fd"},"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":{"_cell_guid":"96d713c7-7a05-43d2-9bcb-2151a6f6c31f"},"outputs":[],"source":"train_agg.info()\n"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"8e5e3597-51ee-4f67-926e-0b8ddfdcc2a8"},"outputs":[],"source":"del(train)\n\ngc.collect()"},{"cell_type":"markdown","metadata":{"_cell_guid":"9f50965f-6355-45b3-ae44-5175e8095bdc"},"source":"## Bookings per day of week"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"e34b5408-7fcf-4706-8fda-a1399e820ea4"},"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":{"_cell_guid":"3540f113-a6ba-4cd3-935e-1c3defff6355"},"outputs":[],"source":"date_agg_1.plot( kind = 'bar', stacked = True )"},{"cell_type":"markdown","metadata":{"_cell_guid":"5b5266e8-5929-4f3c-bdd3-9cfffc0b3e6e"},"source":"## Bookings by year"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"067138f6-ed11-417d-96ed-806dbc16027e"},"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":{"_cell_guid":"33dcc314-fbce-4553-ab3d-9086b5bc7e30"},"source":"## Bookings by month"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"30e1c731-9101-4812-9529-a5b909882a3a"},"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":{"_cell_guid":"5f0694ef-49f2-4378-a2ab-6d1a1d0b8c8f"},"source":"## Interactive booking, click, and percentage of booking trends with Bokeh"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"989ad7ee-5e1e-4848-80a7-5f2bdac929b4"},"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":{"_cell_guid":"54055851-6b00-4b7d-bc57-3164723a7dde"},"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":{"_cell_guid":"94c73567-8926-42fb-91cb-f7d1e29415bb"},"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":{"_cell_guid":"913458d8-e092-491f-a796-52326e72165e"},"outputs":[],"source":"p = make_plot(date_agg_4['bookings'], 'Expedia daily bookings', 'bookings')\nshow(p)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"2c8c02d2-a337-4c73-8250-bdd6308260e1"},"outputs":[],"source":"p = make_plot(date_agg_4['bookings'] / date_agg_4['total'] *100, 'Expedia daily bookings %', 'Bookings, %')\nshow(p)"},{"cell_type":"markdown","metadata":{"_cell_guid":"2bea33e0-888c-4d2e-8219-51cc22f34013"},"source":"## Building interactive charts for hotel clusters, 5 clusters per chart"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"ef97bc03-2cfe-40c1-b28f-03738f92e810"},"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":{"_cell_guid":"d729afaf-878b-4db2-a7ce-cde70b1e2f79"},"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":{"_cell_guid":"bb80e990-5027-4b8a-a59c-4510a1af492f"},"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":{"_cell_guid":"2aef0e0d-aae6-4cbb-acb7-7229ad901da3"},"outputs":[],"source":"print('done')"}],"metadata":{"_change_revision":0,"_is_fork":false,"kernelspec":{"display_name":"Python 3","language":"python","name":"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.5.2"}},"nbformat":4,"nbformat_minor":0}