{"cells":[{"metadata":{"_uuid":"7fa3d496ba80b8c1b8441f5c00e04e717d43f2ec"},"cell_type":"markdown","source":"**Thanks for viewing my Kernel! If you like my work and find it useful, please leave an upvote! :)**\n\nSome columns of the data are in JSON format. I have written a separate [kernel](https://www.kaggle.com/arunsankar/import-data-and-convert-into-table-format/notebook) to parse the data and extract train and test tabular datasets. I will use the output files directly to save execution time. "},{"metadata":{"_uuid":"d5f835f0f6c51144f3347171bbce0cc88a8e645e"},"cell_type":"markdown","source":"**Key insights:**\n* Only 1.4% of the visitors in training set have resulted in revenue. That is why it's critical to identify the customers with revenue potential\n* Mobile device visits generate revenue only from 0.4% of transactions whereas non-mobile devices generate revenue from 1.6% of transactions. Despite the industries' focus on mobile, mobile has really not come closer to non-mobile devices. \n* Even the average revenue per transaction of non-mobile devices are twice more than that of mobile devices.\n* Chrome browser has the most number of transactions and the highest % of transactions with revenue (almost 3 times more than any other browser). On % of transactions with revenue, Chrome is followed by Internet Explorer, Edge, Firefox and then Safari. \n\nNote: I have commented the code for test and submission data explorations for faster execution. "},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nimport os\nprint(os.listdir(\"../input\"))","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"train = pd.read_csv('../input/import-data-and-convert-into-table-format/df_train.csv', low_memory=False)\ntest = pd.read_csv('../input/import-data-and-convert-into-table-format/df_test.csv', low_memory=False)\nsub = pd.read_csv('../input/ga-customer-revenue-prediction/sample_submission.csv', low_memory=False)\n\nprint('Train data: \\nRows: {}\\nCols: {}'.format(train.shape[0],train.shape[1]))\nprint(train.columns)\n\nprint('\\nTest data: \\nRows: {}\\nCols: {}'.format(test.shape[0],test.shape[1]))\nprint(test.columns)\n\nprint('\\nSubmission format: \\nRows: {}\\nCols: {}'.format(sub.shape[0],sub.shape[1]))\nprint(sub.columns)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"88d6de26bb088589247693bd57a0518df27e1316"},"cell_type":"markdown","source":"Submission has less records than test. That is because the objective is to predict log revenue for each unique 'fullVisitorId'."},{"metadata":{"trusted":true,"_uuid":"5e6c387acffa87d4d6d2d5daf14e7bdbf9da2185","_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true,"_uuid":"12acc01c12f5f8a123fe4a5a1902594b46f2d9f9"},"cell_type":"code","source":"test.head()","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true,"_uuid":"c9656eb349e2c6951e8ec52ae18b9437046dfe41"},"cell_type":"code","source":"sub.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f4902643395fd3a6dd1d897d58441375eafc1a12"},"cell_type":"code","source":"print('Unique visitor IDs in training data: {}'.format(train['fullVisitorId'].nunique()))\n#print('Unique visitor IDs in test data: {}'.format(test['fullVisitorId'].nunique()))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5502ca805f1a571454e9243bf6a41e43bf072c06"},"cell_type":"code","source":"train[\"totals.transactionRevenue\"] = train[\"totals.transactionRevenue\"].astype('float')\ntemp = train.groupby('fullVisitorId')['totals.transactionRevenue'].sum().reset_index()\nprint('Percentage of visitors with revenue is {:,.2%}'.format(temp[temp['totals.transactionRevenue'] > 0]['fullVisitorId'].count() / train['fullVisitorId'].nunique()))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"5495ee08df3ea78993612bdecf0c00f3a7324172"},"cell_type":"markdown","source":"Only 1.4% of the visitors have resulted in some revenue. That is why it's very important to identify the customers with revenue potential"},{"metadata":{"_uuid":"0c95b963a9ac8f7cff6b92956e82dfae9e926cf4"},"cell_type":"markdown","source":"Indicator to identify a non-revenue and revenue transaction."},{"metadata":{"trusted":true,"_uuid":"5afb1574778d0e6a092c903351bc2319995e1f76"},"cell_type":"code","source":"train['revenue_flag'] = train['totals.transactionRevenue'].apply(lambda x: 1 if x>0 else 0)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"103fb5c0485a09ee1c5259761b72846ee4159ab7"},"cell_type":"markdown","source":"Mobile device visits generate revenue only from 0.4% of transactions whereas non-mobile devices generate revenue from 1.6% of transactions. Despite the industries' focus on mobile, mobile has really not come closer to non-mobile devices. "},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"8e581839d2d33af3560156476ac341d102494831"},"cell_type":"code","source":"mobile_revenue = train.pivot_table(train, index=['device.isMobile'], columns=['revenue_flag'], aggfunc=len).reset_index()[['device.isMobile','totals.transactionRevenue']]\nmobile_revenue.columns = ['device.isMobile','No Revenue', 'Revenue']\nmobile_revenue['Revenue %'] = mobile_revenue['Revenue'] / (mobile_revenue['Revenue'] + mobile_revenue['No Revenue'])\n\nfig, ax = plt.subplots(figsize=(8,4))\nsns.barplot(x='Revenue %', y='device.isMobile', data=mobile_revenue, color=\"lightskyblue\", orient=\"h\")\n\nfor p in ax.patches:\n    ax.text(p.get_width() + 0.0006, \n            p.get_y() + (p.get_height()/2), \n            '{:,.1%}'.format(p.get_width()),\n            ha=\"center\")\n    \nax.set_ylabel('Device is Mobile?', size=14, color=\"#0D47A1\")\nax.set_xlabel('% of transactions with revenue', size=14, color=\"#0D47A1\")\nax.set_title('Percentage of transactions with revenue by device type', size=18, color=\"#0D47A1\")\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"764fabea70d58b5e2270c3fedb09b765f647139b"},"cell_type":"markdown","source":"Even the average revenue per transaction of non-mobile devices are twice more than that of mobile devices."},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"5ead462cbda8cdcc85b548c86d46af21ae750da4"},"cell_type":"code","source":"mobile_revenue = train.pivot_table(train, index=['device.isMobile'], columns=['revenue_flag'], aggfunc=np.mean).reset_index()[['device.isMobile','totals.transactionRevenue']]\nmobile_revenue.columns = ['device.isMobile','Avg Revenue']\n\nfig, ax = plt.subplots(figsize=(8,4))\nsns.barplot(x='Avg Revenue', y='device.isMobile', data=mobile_revenue, color=\"lightskyblue\", orient=\"h\")\n\nfor p in ax.patches:\n    ax.text(p.get_width() + 10000000, \n            p.get_y() + (p.get_height()/2), \n            '${:,.0f}'.format(p.get_width()),\n            ha=\"center\")\n    \nax.set_ylabel('Device is Mobile?', size=14, color=\"#0D47A1\")\nax.set_xlabel('Average revenue', size=14, color=\"#0D47A1\")\nax.set_title('Average revenue of by device type', size=18, color=\"#0D47A1\")\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6ef165bb58931079ae1f015aa25d61316619dabd"},"cell_type":"markdown","source":"Chrome browser has the most number of transactions and the highest % of transactions with revenue (almost 3 times more than any other browser). On % of transactions with revenue, Chrome is followed by Internet Explorer, Edge, Firefox and then Safari. "},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"7c6b4af06cfad9749dc6dab6e86f7ac74468a652"},"cell_type":"code","source":"browser_revenue = train.pivot_table(train, index=['device.browser'], columns=['revenue_flag'], aggfunc=len).reset_index()[['device.browser','totals.transactionRevenue']]\nbrowser_revenue.columns = ['device.browser','No Revenue', 'Revenue']\nbrowser_revenue.fillna(0, inplace=True)\nbrowser_revenue['Transactions'] = browser_revenue['Revenue'] + browser_revenue['No Revenue']\nbrowser_revenue['Revenue %'] = browser_revenue['Revenue'] / browser_revenue['Transactions']\nbrowser_revenue = browser_revenue.sort_values('Transactions',ascending=False).head(15)\n\nfig, ax = plt.subplots(1, 2, figsize=(8,8), sharey=True)\na = sns.barplot(x='Transactions', y='device.browser', data=browser_revenue, color=\"lightskyblue\", orient=\"h\", ax=ax[0])\nb = sns.barplot(x='Revenue %', y='device.browser', data=browser_revenue, color=\"lightskyblue\", orient=\"h\", ax=ax[1])\n\nfor p in ax[0].patches:\n    ax[0].text(p.get_width() + 75000, \n            p.get_y() + (p.get_height()/2), \n            '{:,.0f}'.format(p.get_width()),\n            ha=\"center\")\n    \nfor p in ax[1].patches:\n    ax[1].text(p.get_width() + 0.001, \n            p.get_y() + (p.get_height()/2), \n            '{:,.1%}'.format(p.get_width()),\n            ha=\"center\")\n    \nax[0].set_ylabel('Browsers', size=14, color=\"#0D47A1\")\nax[0].set_xlabel('Total Number of Transactions', size=14, color=\"#0D47A1\")\n\nax[1].set_ylabel('', size=14, color=\"#0D47A1\")\nax[1].set_xlabel('% of Transactions with revenue', size=14, color=\"#0D47A1\")\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"149e8d9e0e9c1dafc3eb1329f9a547b47913c632"},"cell_type":"markdown","source":"**More to come..**"}],"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}