{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"75f1dda0d9884c7e5c85c500af65b0beaba02c15"},"cell_type":"code","source":"import os\nimport json\nimport numpy as np\nimport pandas as pd\nfrom pandas.io.json import json_normalize\nimport matplotlib.pyplot as plt\nimport seaborn as sns\ncolor = sns.color_palette()\n\n%matplotlib inline\n\nfrom plotly import tools\nimport plotly.offline as py\npy.init_notebook_mode(connected=True)\nimport plotly.graph_objs as go\n\n\npd.options.mode.chained_assignment = None\npd.options.display.max_columns = 999","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6dfd05f455c7a26f931b8dc6fff9c0bfe73d7d00"},"cell_type":"markdown","source":"# **Getting JSON data as Dataframe (Below solution was obtained from Kernel named Quick start: read csv and flatten json fields by Julian)**"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"def load_df(csv_path='../input/train.csv', nrows=None):\n    JSON_COLUMNS = ['device', 'geoNetwork', 'totals', 'trafficSource']\n    \n    df = pd.read_csv(csv_path, \n                     converters={column: json.loads for column in JSON_COLUMNS}, \n                     dtype={'fullVisitorId': 'str'}, # Important!!\n                     nrows=nrows)\n    \n    for column in JSON_COLUMNS:\n        column_as_df = json_normalize(df[column])\n        column_as_df.columns = [f\"{column}.{subcolumn}\" for subcolumn in column_as_df.columns]\n        df = df.drop(column, axis=1).merge(column_as_df, right_index=True, left_index=True)\n    print(f\"Loaded {os.path.basename(csv_path)}. Shape: {df.shape}\")\n    return df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d17eda4f5f2529eca87a4486dad049f606bcf5dd"},"cell_type":"code","source":"%%time\ntrain_df = load_df()\ntest_df = load_df(\"../input/test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"22068dc0dc7996386a0a4fa49559a2897af54347"},"cell_type":"markdown","source":"### ******Below solution looks for the use of browser mainly Chrome,Firefox and IE through the time period avaliable through data given******"},{"metadata":{"trusted":true,"_uuid":"e43cba7d10713b444decf520590ec366b5608177"},"cell_type":"code","source":"date_browser = train_df[['date','device.browser']]\nimport datetime\ndate_browser['date'] = date_browser['date'].apply(lambda x: datetime.date(int(str(x)[:4]), int(str(x)[4:6]), int(str(x)[6:])))\n\n\ndef custom_func(series):\n    Chrome = 0\n    Firefox = 0\n    IE = 0\n    custom_list = series.tolist()\n    for x in custom_list:\n        if x == 'Chrome':\n            Chrome = Chrome + 1\n        if x == 'Firefox':\n            Firefox = Firefox + 1\n        if x == 'Internet Explorer':\n            IE = IE + 1\n    return Chrome,Firefox,IE\n\n\ngrpd = date_browser.groupby('date')['device.browser'].agg([custom_func])\n\nchrome = []\nfirefox =[]\nie = []\n\nfor x in range(len(grpd)):\n    chrome.append(grpd['custom_func'][x][0])\n    firefox.append(grpd['custom_func'][x][1])\n    ie.append(grpd['custom_func'][x][2])\n    \nchrome = np.asarray(chrome)\nfirefox = np.asarray(firefox)\nie = np.asarray(ie)\n\nfrom sklearn.preprocessing import normalize\nchrome = normalize(chrome[:,np.newaxis], axis=0).ravel()\nfirefox = normalize(firefox[:,np.newaxis], axis=0).ravel()\nie = normalize(ie[:,np.newaxis], axis=0).ravel()\n\ngrpd['chrome'] = chrome\ngrpd['firefox'] = firefox\ngrpd['ie'] = ie","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4de8909d7e24c420a4c673ee12a920978683eec1"},"cell_type":"code","source":"def scatter_plot(cnt_srs, color):\n    trace = go.Scatter(\n        x=cnt_srs.index[::-1],\n        y=cnt_srs.values[::-1],\n        showlegend=False,\n        marker=dict(\n            color=color,\n        ),\n    )\n    return trace\n\ntrace1 = scatter_plot(grpd['chrome'], 'red')\ntrace2 = scatter_plot(grpd['firefox'], 'blue')\ntrace3 = scatter_plot(grpd['ie'], 'green')\n\nfig = tools.make_subplots(rows=3, cols=1, vertical_spacing=0.08,\n                          subplot_titles=[\"Chrome\", \"Firefox\",\"Internet Explorer\"])\nfig.append_trace(trace1, 1, 1)\nfig.append_trace(trace2, 2, 1)\nfig.append_trace(trace3, 3, 1)\nfig['layout'].update(height=800, width=1200, paper_bgcolor='rgb(233,233,233)', title=\"Date Plots\")\npy.iplot(fig, filename='date-plots')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"87c554153da0f6312ef750b8b6d8c721decae8e2"},"cell_type":"markdown","source":"**The solution below looks for the trend in the moving average of the usage of 3 browser to access G Store**"},{"metadata":{"trusted":true,"_uuid":"e1265c1116ccc0a70c2a15e85e282cb2eb94eb90"},"cell_type":"code","source":"grpd['chrome_rol'] = grpd['chrome'].rolling(10).mean()\ngrpd['firefox_rol'] = grpd['firefox'].rolling(10).mean()\ngrpd['ie_rol'] = grpd['ie'].rolling(10).mean()\n\ntrace1_rol = scatter_plot(grpd['chrome_rol'], 'red')\ntrace2_rol = scatter_plot(grpd['firefox_rol'], 'blue')\ntrace3_rol = scatter_plot(grpd['ie_rol'], 'green')\n\nfig = tools.make_subplots(rows=3, cols=1, vertical_spacing=0.08,\n                          subplot_titles=[\"Chrome\", \"Firefox\",\"Internet Explorer\"])\nfig.append_trace(trace1_rol, 1, 1)\nfig.append_trace(trace2_rol, 2, 1)\nfig.append_trace(trace3_rol, 3, 1)\nfig['layout'].update(height=800, width=1200, paper_bgcolor='rgb(233,233,233)', title=\"Date Plots\")\npy.iplot(fig, filename='date-plots')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1860823e368314c3d108998bae8ad370ef8d4760"},"cell_type":"markdown","source":"### All three browsers register a greater use in access of GStore between November 2016 and before January 2017. But they also register the least usage in January 2017 with a sudden drop from December 2016 to January 2017."},{"metadata":{"trusted":true,"_uuid":"84e26a6f6fbcf36a6d48984e9b2554aceb4017cf"},"cell_type":"markdown","source":"### ******Below solution looks at all  continents and the number of people accessing the G Store through the given time period ******"},{"metadata":{"trusted":true,"_uuid":"5814c39d33753888c5951262361c24f795e79de5"},"cell_type":"code","source":"date_continent = train_df[['date','geoNetwork.continent']]\nimport datetime\ndate_continent['date'] = date_continent['date'].apply(lambda x: datetime.date(int(str(x)[:4]), int(str(x)[4:6]), int(str(x)[6:])))\n\n\ndef custom_func(series):\n    Americas = 0\n    Asia = 0\n    Europe = 0\n    Oceania = 0\n    Africa = 0\n    custom_list = series.tolist()\n    for x in custom_list:\n        if x == 'Americas':\n            Americas = Americas + 1\n        if x == 'Asia':\n            Asia = Asia + 1\n        if x == 'Europe':\n            Europe = Europe + 1\n        if x == 'Oceania':\n            Oceania = Oceania + 1\n        if x == 'Africa':\n            Africa = Africa + 1\n    return Americas,Asia,Europe,Oceania,Africa\n\n\ncont_grpd = date_continent.groupby('date')['geoNetwork.continent'].agg([custom_func])\n\namericas = []\nasia =[]\neurope = []\noceania = []\nafrica = []\n\nfor x in range(len(cont_grpd)):\n    americas.append(cont_grpd['custom_func'][x][0])\n    asia.append(cont_grpd['custom_func'][x][1])\n    europe.append(cont_grpd['custom_func'][x][2])\n    oceania.append(cont_grpd['custom_func'][x][3])\n    africa.append(cont_grpd['custom_func'][x][4])\n    \namericas = np.asarray(americas)\nasia = np.asarray(asia)\neurope = np.asarray(europe)\noceania = np.asarray(oceania)\nafrica = np.asarray(africa)\n\n'''from sklearn.preprocessing import normalize\nchrome = normalize(chrome[:,np.newaxis], axis=0).ravel()\nfirefox = normalize(firefox[:,np.newaxis], axis=0).ravel()\nie = normalize(ie[:,np.newaxis], axis=0).ravel()'''\n\ngrpd['americas'] = americas\ngrpd['asia'] = asia\ngrpd['europe'] = europe\ngrpd['oceania'] = oceania\ngrpd['africa'] = africa\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a76265cb9204e8614d47f1af32e365b01f8dc4d4"},"cell_type":"code","source":"trace1 = scatter_plot(grpd['americas'], 'red')\ntrace2 = scatter_plot(grpd['asia'], 'blue')\ntrace3 = scatter_plot(grpd['europe'], 'green')\ntrace4 = scatter_plot(grpd['oceania'], 'black')\ntrace5 = scatter_plot(grpd['africa'], 'pink')\n\nfig = tools.make_subplots(rows=5, cols=1, vertical_spacing=0.08,\n                          subplot_titles=[\"americas\", \"asia\",\"europe\",\"oceania\",\"africa\"])\nfig.append_trace(trace1, 1, 1)\nfig.append_trace(trace2, 2, 1)\nfig.append_trace(trace3, 3, 1)\nfig.append_trace(trace4, 4, 1)\nfig.append_trace(trace5, 5, 1)\nfig['layout'].update(height=800, width=1200, paper_bgcolor='rgb(233,233,233)', title=\"Date Plots\")\npy.iplot(fig, filename='date-plots')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6d54d735f8cb38ac2a94f9e3e7dcf7637c8de39e"},"cell_type":"markdown","source":"### **The solution below looks for the trend in the moving average of the traffic coming from various continents to G Store**"},{"metadata":{"trusted":true,"_uuid":"2be1e54118bd635038e7ee8824fe53ef6ff70e88","scrolled":true},"cell_type":"code","source":"grpd['americas_rol'] = grpd['americas'].rolling(10).mean()\ngrpd['asia_rol'] = grpd['asia'].rolling(10).mean()\ngrpd['europe_rol'] = grpd['europe'].rolling(10).mean()\ngrpd['oceania_rol'] = grpd['oceania'].rolling(10).mean()\ngrpd['africa_rol'] = grpd['africa'].rolling(10).mean()\n\ntrace1_rol = scatter_plot(grpd['americas_rol'], 'red')\ntrace2_rol = scatter_plot(grpd['asia_rol'], 'blue')\ntrace3_rol = scatter_plot(grpd['europe_rol'], 'green')\ntrace4_rol = scatter_plot(grpd['oceania_rol'], 'black')\ntrace5_rol = scatter_plot(grpd['africa_rol'], 'pink')\n\n\nfig = tools.make_subplots(rows=5, cols=1, vertical_spacing=0.08,\n                          subplot_titles=[\"americas\", \"asia\",\"europe\",\"oceania\",\"africa\"])\nfig.append_trace(trace1_rol, 1, 1)\nfig.append_trace(trace2_rol, 2, 1)\nfig.append_trace(trace3_rol, 3, 1)\nfig.append_trace(trace4_rol, 4, 1)\nfig.append_trace(trace5_rol, 5, 1)\nfig['layout'].update(height=800, width=1200, paper_bgcolor='rgb(233,233,233)', title=\"Date Plots\")\npy.iplot(fig, filename='date-plots')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1a1da9923b5b3d80a0f036007dccc29d6b76f923"},"cell_type":"markdown","source":"### All continents register their least period of access to GStore during January 2017. All continents give a drop in traffic to G Store."},{"metadata":{"trusted":true,"_uuid":"6f3f105ad1443d32ce7e12789054ecd5bfe5f42e"},"cell_type":"markdown","source":"### ******Below solution looks at various devices used to  access the G Store through the given time period ******"},{"metadata":{"trusted":true,"_uuid":"e05da854bf1784e932fd65ec0642693b1c3db294"},"cell_type":"code","source":"date_device = train_df[['date','device.deviceCategory']]\nimport datetime\ndate_device['date'] = date_device['date'].apply(lambda x: datetime.date(int(str(x)[:4]), int(str(x)[4:6]), int(str(x)[6:])))\n\n\ndef custom_func(series):\n    desktop = 0\n    mobile = 0\n    tablet = 0\n    custom_list = series.tolist()\n    for x in custom_list:\n        if x == 'desktop':\n            desktop = desktop + 1\n        if x == 'mobile':\n            mobile = mobile + 1\n        if x == 'tablet':\n            tablet = tablet + 1\n    return desktop,mobile,tablet\n\n\ndev_grpd = date_device.groupby('date')['device.deviceCategory'].agg([custom_func])\n\ndesktop = []\nmobile = []\ntablet = []\n\nfor x in range(len(cont_grpd)):\n    desktop.append(cont_grpd['custom_func'][x][0])\n    mobile.append(cont_grpd['custom_func'][x][1])\n    tablet.append(cont_grpd['custom_func'][x][2])\n    \ndesktop = np.asarray(desktop)\nmobile = np.asarray(mobile)\ntablet = np.asarray(tablet)\n\n'''from sklearn.preprocessing import normalize\nchrome = normalize(chrome[:,np.newaxis], axis=0).ravel()\nfirefox = normalize(firefox[:,np.newaxis], axis=0).ravel()\nie = normalize(ie[:,np.newaxis], axis=0).ravel()'''\n\ngrpd['desktop'] = desktop\ngrpd['mobile'] = mobile\ngrpd['tablet'] = tablet\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c529ac05759badc2663319b6a89652adf38f2543"},"cell_type":"code","source":"trace1 = scatter_plot(grpd['desktop'], 'red')\ntrace2 = scatter_plot(grpd['mobile'], 'blue')\ntrace3 = scatter_plot(grpd['tablet'], 'green')\n\nfig = tools.make_subplots(rows=3, cols=1, vertical_spacing=0.08,\n                          subplot_titles=[\"desktop\", \"mobile\",\"tablet\"])\nfig.append_trace(trace1, 1, 1)\nfig.append_trace(trace2, 2, 1)\nfig.append_trace(trace3, 3, 1)\nfig['layout'].update(height=800, width=1200, paper_bgcolor='rgb(233,233,233)', title=\"Date Plots\")\npy.iplot(fig, filename='date-plots')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"48f13ef2bbf4e740147465f3ffe63b5e693fef5b"},"cell_type":"markdown","source":"### **The solution below looks for the trend in the moving average of the traffic coming from various devices to G Store**"},{"metadata":{"trusted":true,"_uuid":"bb1e1ea981359c00667c5ae1d39246d7dc8847bf"},"cell_type":"code","source":"grpd['desktop_rol'] = grpd['desktop'].rolling(10).mean()\ngrpd['mobile_rol'] = grpd['mobile'].rolling(10).mean()\ngrpd['tablet_rol'] = grpd['tablet'].rolling(10).mean()\n\ntrace1_rol = scatter_plot(grpd['desktop_rol'], 'red')\ntrace2_rol = scatter_plot(grpd['mobile_rol'], 'blue')\ntrace3_rol = scatter_plot(grpd['tablet_rol'], 'green')\n\n\nfig = tools.make_subplots(rows=3, cols=1, vertical_spacing=0.08,\n                          subplot_titles=[\"desktop\", \"mobile\",\"tablet\"])\nfig.append_trace(trace1_rol, 1, 1)\nfig.append_trace(trace2_rol, 2, 1)\nfig.append_trace(trace3_rol, 3, 1)\nfig['layout'].update(height=800, width=1200, paper_bgcolor='rgb(233,233,233)', title=\"Date Plots\")\npy.iplot(fig, filename='date-plots')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"77e77f675da0a81b82f3b250b19142b1da2b4f20"},"cell_type":"markdown","source":"> ***All graphs register a drop between November 2016 and  January 2017 . What major event caused this drop in traffic? Or is it seasonal? *****"},{"metadata":{"_uuid":"4eb69ac6c5705ad715bf1b8b1dfbdd508d350e47"},"cell_type":"markdown","source":""}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}