{"cells":[{"metadata":{},"cell_type":"markdown","source":"# GSTORE CUSTOMER REVENUE PREDICTION - COMPLETE EDA\n***BY HARISH JULAPALLI***"},{"metadata":{},"cell_type":"markdown","source":"The 80/20 rule has proven true for many businesses–only a small percentage of customers produce most of the revenue. As such, marketing teams are challenged to make appropriate investments in promotional strategies."},{"metadata":{},"cell_type":"markdown","source":" [](http://)[](http://) In this notebook, I've analysed GSTORE customers extensively based on various attributes. I have built visualzations to tell which specific customers made contribute to revenue of the GSTORE."},{"metadata":{},"cell_type":"markdown","source":"> Loading all the required libraries:"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom pandas.io.json import json_normalize\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport json\n\nimport plotly.graph_objs as go\nfrom plotly import tools\nfrom plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot\ninit_notebook_mode(connected=True)\n\n%matplotlib inline\nplt.style.use('ggplot')\n\nimport os\nprint(os.listdir(\"../input\"))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Loading the dataset:"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"df = pd.read_csv(\"../input/train_v2.csv\", nrows = 10000)\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"As seen from a small sample of the dataset, it is evident that many columns have JSON values in them, whcih inturn have many attributes. So, we have to find a way to extract these features as well."},{"metadata":{},"cell_type":"markdown","source":"This function load_df helps to extract features from these JSON columns:"},{"metadata":{"trusted":true},"cell_type":"code","source":"def load_df(csv_path='../input/train_v2.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\n        ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Now loading train and test sets:"},{"metadata":{"trusted":true},"cell_type":"code","source":"train = load_df(nrows = 200000)\ntrain.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test = load_df(csv_path='../input/test_v2.csv', nrows = 50000)\ntest.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.drop(['customDimensions','hits'], axis = 1, inplace = True)\n#train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test.drop(['customDimensions','hits'], axis = 1, inplace = True)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Remove columns which has only single category in them, as they does not add any value:"},{"metadata":{"trusted":true},"cell_type":"code","source":"single_cat_cols = [col for col in train.columns if train.nunique()[col] == 1]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.drop(single_cat_cols, axis =1, inplace = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"single_cat_cols_test = [col for col in test.columns if test.nunique()[col] == 1]\ntest.drop(single_cat_cols_test , axis =1, inplace = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Exploratory Analysis"},{"metadata":{},"cell_type":"markdown","source":"Number of customers who visited GSTORE:"},{"metadata":{"trusted":true},"cell_type":"code","source":"print(f\"Number of unique customers: {train.fullVisitorId.nunique()}\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Number of customers who visited GSTORE more than once:"},{"metadata":{"trusted":true},"cell_type":"code","source":"print(f\"Number of customers who visited the site more than once: {train.shape[0]-train.fullVisitorId.nunique()}\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Doing some conversions and filling missing values of transaction revenue with 0's:"},{"metadata":{"trusted":true},"cell_type":"code","source":"train['totals_transactionRevenue'] = train['totals_transactionRevenue'].astype('float')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train['totals_transactionRevenue'].fillna(0, inplace = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train['totals_transactionRevenue'].sum()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Isolating customers who made purchases on Gstore form the whole set:"},{"metadata":{"trusted":true},"cell_type":"code","source":"reven_group = train.groupby('fullVisitorId')['totals_transactionRevenue'].sum().reset_index()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Plotting customers who spent money with indices on horizontal axis and log revenue on vertical axis:[[](http://)](http://)**"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize = (8,6))\nplt.scatter(range(reven_group.shape[0]), np.sort(np.log1p(reven_group['totals_transactionRevenue'])))\nplt.xlabel('Index', fontsize = 14)\nplt.ylabel('Log Transaction Revenue', fontsize = 14)\nplt.title(\"Non-zero Revenue group\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"It can be observed that even in non-zero revenue group there are only a few customers who made large transactions. But the above plot is little obscure."},{"metadata":{},"cell_type":"markdown","source":"A Pie chart can show this variation very clearly."},{"metadata":{"trusted":true},"cell_type":"code","source":"print(f\"Percentage of Revenue generating customers: {(train[train['totals_transactionRevenue'] > 0].shape[0]/train.shape[0])*100}\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Pie-chart\n\nlabels = ['Non Revenue generating customers', 'Revenue generating customers']\nnon_revenue_perc = (train[train['totals_transactionRevenue'] == 0].shape[0]/train.shape[0])*100\nrevenue_perc = (train[train['totals_transactionRevenue'] > 0].shape[0]/train.shape[0])*100\nsizes = [non_revenue_perc, revenue_perc]\nexplode = (0, 1)\n\nfig1, ax1 = plt.subplots()\nax1.pie(sizes, explode = explode, labels = labels, autopct='%0.2f%%', colors = ['cornflowerblue','coral'], radius = 3)\nplt.figure(figsize=(10,10))\nax1.axis('equal')\nplt.tight_layout()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Only **1.08%** of total customers of Gstore actually made purchases."},{"metadata":{},"cell_type":"markdown","source":"**Distribution of target variable: totals_transactionRevenue**"},{"metadata":{"trusted":true},"cell_type":"code","source":"#Distribution of revenue generated\nrg = train[train['totals_transactionRevenue'] > 0]['totals_transactionRevenue']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nplt.figure(figsize = (8,6))\nsns.distplot(rg)\nplt.xlabel(\"Total Transaction Revenue\", fontsize = 14)\nplt.title(\"Distribution of Total Transaction Revenue\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"It is highly skewed to left side."},{"metadata":{},"cell_type":"markdown","source":"Applying log on target variable and plotting the distribution:"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize = (8,6))\nsns.distplot(rg.apply(np.log1p))\nplt.xlabel(\"Log Total Transaction Revenue\", fontsize = 14)\nplt.title(\"Distribution of Log Total Transaction Revenue\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Examining the skew and kurtosis if our log transformed target variable:"},{"metadata":{"trusted":true},"cell_type":"code","source":"from scipy.stats import kurtosis, skew\nprint(f\"Skewness of transction value: {skew(rg.apply(np.log1p))}\")\nprint(f\"Kurtosis of transction value: {kurtosis(rg.apply(np.log1p))}\")\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Skewness and Kurtosis values indicate log of target variable is almost approximately normally distributed."},{"metadata":{},"cell_type":"markdown","source":"**Customers come to Gstore through various channels. Exploring which channles brings more customers:**"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize = (8,6))\nsns.countplot(y = train['channelGrouping'], order = train['channelGrouping'].value_counts().index, palette='Blues_d')\nplt.xlabel(\"Count\", fontsize = 14)\nplt.ylabel(\"Channel Grouping\",fontsize = 14)\nplt.title(\"Channel Grouping\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Seems **Organic search** and **Social channels** bring most of the customers through Gstore."},{"metadata":{},"cell_type":"markdown","source":"**Exploring how revenue generating customers come to Gstore via various channels:**"},{"metadata":{"trusted":true},"cell_type":"code","source":"rg = train[train['totals_transactionRevenue'] > 0]\n\nplt.figure(figsize = (8,6))\nsns.countplot(y = rg['channelGrouping'], order = rg['channelGrouping'].value_counts().index, palette='Blues_d')\nplt.xlabel(\"Count of Revenue generating customers\", fontsize = 14)\nplt.ylabel(\"Channel Grouping\",fontsize = 14)\nplt.title(\"Channel Grouping\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Though Organic search brings more customers, it is through Refferals where revenue generating customers are coming from:**"},{"metadata":{},"cell_type":"markdown","source":"Grouping revenue generating customers based on channels and see which channels brought more revenue:"},{"metadata":{"trusted":true},"cell_type":"code","source":"channel_revenue_group = train.groupby('channelGrouping')['totals_transactionRevenue'].sum().apply(lambda x: x/10000)\n\nplt.figure(figsize = (8,6))\nsns.barplot(y = channel_revenue_group.index, x = channel_revenue_group.values,palette='Blues_d', order = channel_revenue_group.sort_values(ascending = False).index)\nplt.xlabel(\"Total Revenue\", fontsize = 14)\nplt.ylabel(\"Channel Grouping\",fontsize = 14)\nplt.title(\"Total Revenue through various channels\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**More the customers, more the revenue. Referral has the most revenue of all channels**"},{"metadata":{},"cell_type":"markdown","source":"Maximum revenue generated by various channels:"},{"metadata":{"trusted":true},"cell_type":"code","source":"train.groupby('channelGrouping')['totals_transactionRevenue'].max().apply(lambda x: x/10000).sort_values(ascending = False)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Exploring what **DEVICE BROWSER** are Gstore customers using and which brings more revenue:"},{"metadata":{},"cell_type":"markdown","source":"Now exploring what browsers does customers use for coming to Gstore:"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize = (8,6))\nsns.countplot(y = train['device_browser'], order = train['device_browser'].value_counts().head(8).index, palette='Blues_d')\nplt.xlabel(\"Count\", fontsize = 14)\nplt.ylabel(\"Device Browsers\",fontsize = 14)\nplt.title(\"Device Browsers\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Chrome and Safari are the top browsers that customers use to come to Gstore.**"},{"metadata":{},"cell_type":"markdown","source":"Which browsers are our revenue generating customers using?"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize = (8,6))\nsns.countplot(y = rg['device_browser'], order = rg['device_browser'].value_counts().index, palette='Blues_d')\nplt.xlabel(\"Count of revenue generating customers\", fontsize = 14)\nplt.ylabel(\"Device Browser\",fontsize = 14)\nplt.title(\"Browsers of revenue generating customers\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Through which browser does most of the revenue come from?"},{"metadata":{"trusted":true},"cell_type":"code","source":"browser_revenue_group = rg.groupby('device_browser')['totals_transactionRevenue'].sum().apply(lambda x: x/10000)\n\nplt.figure(figsize = (8,6))\nsns.barplot(y = browser_revenue_group.index, x = browser_revenue_group.values,palette='Blues_d', order = browser_revenue_group.sort_values(ascending = False).index)\nplt.xlabel(\"Revenue\", fontsize = 14)\nplt.ylabel(\"Device Browser\",fontsize = 14)\nplt.title(\"Revenues through various browsers\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**It's evident that Gstore users mostly use chrome to shop.**"},{"metadata":{},"cell_type":"markdown","source":"What Browsers and devices do revenue generating customers use to come to Gstore?"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize = (8,6))\nsns.countplot(y = rg['device_browser'], order = rg['device_browser'].value_counts().head(5).index, hue = rg['device_isMobile'], palette='Blues_d')\nplt.xlabel(\"Count\", fontsize = 14)\nplt.ylabel(\"Device Browser/Device type\",fontsize = 14)\nplt.title(\"Count of Device browsers and device types of revenue generating customers\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"It is evident that **Desktop users, using Chrome browser** spent more money than others."},{"metadata":{},"cell_type":"markdown","source":"What Devices do customers use to come to Gstore?"},{"metadata":{"trusted":true},"cell_type":"code","source":"\nplt.figure(figsize = (8,6))\nsns.countplot(x = train['device_deviceCategory'], order = train.device_deviceCategory.value_counts().index, palette='Blues_d')\nplt.xlabel(\"Device type\", fontsize = 14)\nplt.ylabel(\"Count\",fontsize = 14)\nplt.title(\"Device categories\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Most of the Gstore customers use their Desktop for shopping. **"},{"metadata":{},"cell_type":"markdown","source":"What devices does revenue generating customers use to come to Gstore?"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize = (8,6))\nsns.countplot(x = rg['device_deviceCategory'], order = rg['device_deviceCategory'].value_counts().index, palette='Blues_d')\nplt.xlabel(\"Device Category\", fontsize = 14)\nplt.ylabel(\"Count\",fontsize = 14)\nplt.title(\"Devices\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Most Revenue generating customers use Desktop.**"},{"metadata":{},"cell_type":"markdown","source":"What Operating systems does Gstore customers use?"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize = (8,6))\nsns.countplot(y = train['device_operatingSystem'], order = train.device_operatingSystem.value_counts().head(8).index, palette='Blues_d')\nplt.xlabel(\"Count\", fontsize = 14)\nplt.ylabel(\"OS\",fontsize = 14)\nplt.title(\"Operating systems\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Windows and Mac are dominant OS which Gstore customers use.**"},{"metadata":{},"cell_type":"markdown","source":"What OS are revenue generating customers usin?"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize = (8,6))\nsns.countplot(y = rg['device_operatingSystem'], order = rg['device_operatingSystem'].value_counts().head(8).index, palette='Blues_d')\nplt.xlabel(\"Count of revenue generating customers\", fontsize = 14)\nplt.ylabel(\"OS\",fontsize = 14)\nplt.title(\"Channel Grouping\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**There's a variation between above 2 plots. Above plot clearly shows users with MAC OS are buying from Gstore more rather than Windows which has more overall users.**"},{"metadata":{},"cell_type":"markdown","source":"What is the total revenue based on different operating systems does buyers used?"},{"metadata":{"trusted":true},"cell_type":"code","source":"os_revenue_group = train.groupby('device_operatingSystem')['totals_transactionRevenue'].sum().apply(lambda x: x/10000)\n\nplt.figure(figsize = (8,6))\nsns.barplot(y = os_revenue_group.index, x = os_revenue_group.values,palette='Blues_d',order = os_revenue_group.sort_values(ascending = False).head(7).index)\nplt.xlabel(\"Total Revenue\", fontsize = 14)\nplt.ylabel(\"OS\",fontsize = 14)\nplt.title(\"Total revenue by OS\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Users with Mac spend more total money on Gstore.**"},{"metadata":{},"cell_type":"markdown","source":"From which devices are buyers coming to Gstore?"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize = (8,6))\nsns.countplot(y = rg['device_operatingSystem'], order = rg['device_operatingSystem'].value_counts().head(6).index, hue = rg['device_deviceCategory'], palette='Blues_d')\nplt.xlabel(\"Count\", fontsize = 14)\nplt.ylabel(\"OS/Device\",fontsize = 14)\nplt.title(\"Customers across where devices/OS\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**It is interesting here to note that iOS and Android users from mobile are visting Gstore.**"},{"metadata":{},"cell_type":"markdown","source":"**Now from which different regions customers are from?**"},{"metadata":{"trusted":true},"cell_type":"code","source":"geo_cols = [col for col in train.columns if 'geoNetwork' in col]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Where are people who visit Gstore are located on the globe?"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize = (8,6))\nsns.countplot(y = train['geoNetwork_continent'], order = train['geoNetwork_continent'].value_counts().index)\nplt.xlabel(\"Count\", fontsize = 14)\nplt.ylabel(\"Continenet\",fontsize = 14)\nplt.title(\"users across various continents\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Most people coming to Gstore are from Americas and Asia.**"},{"metadata":{},"cell_type":"markdown","source":"What countries are Gstore customers from?"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize = (14,10))\nsns.countplot(x = train['geoNetwork_country'], order = train['geoNetwork_country'].value_counts().head(20).index)\nplt.xticks(rotation=90)\nplt.xlabel(\"Country\", fontsize = 14)\nplt.ylabel(\"Count\",fontsize = 14)\nplt.title(\"users across various countries\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Gstore sees its users mainly from **US,India,UK and Canada**."},{"metadata":{},"cell_type":"markdown","source":"Where are Gstore buyers mostly from?"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize = (8,6))\nsns.countplot(y = rg['geoNetwork_country'], order = rg['geoNetwork_country'].value_counts().head(10).index, palette='Blues_d')\nplt.xlabel(\"Country\", fontsize = 14)\nplt.ylabel(\"Count\",fontsize = 14)\nplt.title(\"revenue generating customers across various countries\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"It's **US and Canada** where most of the revenue generating customers are from."},{"metadata":{},"cell_type":"markdown","source":"Since most of Gstore customers are from US, it's only logical to see from which cities these customers are from."},{"metadata":{"trusted":true},"cell_type":"code","source":"us_traffic = train[train['geoNetwork_country'] == 'United States']\n\nplt.figure(figsize = (10,8))\nsns.countplot(y = us_traffic['geoNetwork_metro'], order = us_traffic['geoNetwork_metro'].value_counts().head(10).index)\nplt.xlabel(\"City\", fontsize = 14)\nplt.ylabel(\"Count\",fontsize = 14)\nplt.title(\"Customers across various US cities\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Most of the users of Gstore are from **San Jose, New York city and Los Angeles.**"},{"metadata":{},"cell_type":"markdown","source":"Now what does Gstore customers look on a map?"},{"metadata":{"trusted":true},"cell_type":"code","source":"tmp = train['geoNetwork_country'].value_counts()\ncountry_visits = pd.DataFrame(data={'geoNetwork_country': tmp.values}, index=tmp.index).reset_index()\ncountry_visits.columns = ['Country', 'Visits']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"colorscale = [[0, 'rgb(102,194,165)'], [0.005, 'rgb(102,194,165)'], \n              [0.01, 'rgb(171,221,164)'], [0.02, 'rgb(230,245,152)'], \n              [0.04, 'rgb(255,255,191)'], [0.05, 'rgb(254,224,139)'], \n              [0.10, 'rgb(253,174,97)'], [0.25, 'rgb(213,62,79)'], [1.0, 'rgb(158,1,66)']]\n\ndef plot_country_map(data, location, z, legend, title, colormap='Rainbow'):\n    data = dict(type = 'choropleth', \n                colorscale = colorscale,\n                autocolorscale = False,\n                reversescale = False,\n               locations = data[location],\n               locationmode = 'country names',\n               z = data[z], \n               text = data[z],\n               colorbar = {'title':legend})\n    layout = dict(title = title, \n                 geo = dict(showframe = False, \n                         projection = {'type': 'natural earth'}))\n    choromap = go.Figure(data = [data], layout=layout)\n    iplot(choromap)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_country_map(country_visits, 'Country', 'Visits', 'Visits', 'Visits per country')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Red indicates more users and green indiactes less number or no users."},{"metadata":{},"cell_type":"markdown","source":"How revenue generating customers would look on a map?"},{"metadata":{"trusted":true},"cell_type":"code","source":"revenue_by_country = rg.groupby('totals_transactionRevenue')['geoNetwork_country'].sum().reset_index()\n\nplot_country_map(revenue_by_country, 'geoNetwork_country', 'totals_transactionRevenue', 'Transaction Revenue', 'Revenue per country')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Red indicates more revenue as compared to whie."},{"metadata":{"trusted":true},"cell_type":"markdown","source":"Analysing the Time column might throw more light on consumer behaviour."},{"metadata":{"trusted":true},"cell_type":"code","source":"train['date'] = pd.to_datetime(train['date'], format = '%Y%m%d')\ntest['date'] = pd.to_datetime(test['date'], format = '%Y%m%d')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def plot_scatter_data(data, xtitle, ytitle, title, color='blue'):\n    trace = go.Scatter(\n        x = data.index,\n        y = data.values,\n        name=ytitle,\n        marker=dict(\n            color=color,\n        ),\n        mode='lines+markers'\n    )\n    data = [trace]\n    layout = dict(title = title,\n              xaxis = dict(title = xtitle), yaxis = dict(title = ytitle),\n             )\n    fig = dict(data=data, layout=layout)\n    iplot(fig, filename='lines')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Total visits to Gstore over the time:**"},{"metadata":{"trusted":true},"cell_type":"code","source":"count_all = train.groupby('date')['totals_transactionRevenue'].agg(['size'])\ncount_all.columns = [\"Total\"]\ncount_all = count_all.sort_index()\nplot_scatter_data(count_all['Total'],'Date', 'Total','Total count of visits (including zero transactions)','green')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"There's certainly a dip in transaction revenue during few months and a raise during others."},{"metadata":{"trusted":true},"cell_type":"code","source":"count_nonzero = train.groupby('date')['totals_transactionRevenue'].agg(['count'])\ncount_nonzero.columns = [\"Total\"]\ncount_nonzero = count_nonzero.sort_index()\nplot_scatter_data(count_nonzero['Total'],'Date', 'Total','Total non-zero transaction visits','darkblue')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"The above interactive plot explore the buyers trend over time."},{"metadata":{"trusted":true},"cell_type":"code","source":"total_nonzero = train.groupby('date')['totals_transactionRevenue'].agg(['sum'])\ntotal_nonzero.columns = [\"Total\"]\ntotal_nonzero = total_nonzero.sort_index()\nplot_scatter_data(total_nonzero['Total'],'Date', 'Total','Total non-zero transaction amounts','red')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"The above plot shows the total transaction revenue at any particular point of the time."},{"metadata":{},"cell_type":"markdown","source":"Now examining how the trends vary over months, days etc."},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df_date = train.set_index('date')\ntrain_df_date['year'] = train_df_date.index.year\ntrain_df_date['month'] = train_df_date.index.month\ntrain_df_date['weekday'] = train_df_date.index.weekday_name\n#train_df_date.head()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df_date_rg = train_df_date[train_df_date['totals_transactionRevenue'] > 0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize = (8,6))\nsns.countplot(train_df_date['year'], palette = 'Blues_d')\nplt.xlabel(\"Year\", fontsize = 14)\nplt.ylabel(\"Count\",fontsize = 14)\nplt.title(\"Users over the years\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Year 2017 saw more number of visits to the Gstore."},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize = (8,6))\nsns.countplot(train_df_date_rg['year'], palette = 'Blues_d')\nplt.xlabel(\"Year\", fontsize = 14)\nplt.ylabel(\"Count\",fontsize = 14)\nplt.title(\"Revenue generating users over the years\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Year 2017, had most revenue than 2017 and 2018."},{"metadata":{},"cell_type":"markdown","source":"Which month bought more users?"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize = (8,6))\nsns.countplot(train_df_date['month'], palette = 'Blues_d')\nplt.xlabel(\"Month\", fontsize = 14)\nplt.ylabel(\"Count\",fontsize = 14)\nplt.title(\"Users over the Months\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"November, August and February had more visitors to Gstore."},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize = (8,6))\nsns.countplot(train_df_date_rg['month'], palette = 'Blues_d')\nplt.xlabel(\"Month\", fontsize = 14)\nplt.ylabel(\"Count\",fontsize = 14)\nplt.title(\"Revenue generating users over the months\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**August bought more revenue to Gstore. Also, November and December also brought more revenue to Gstore. This may be because of the holiday season like Thanksgiving and Christmas, as most of the buyers are from US.**"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize = (8,6))\nsns.countplot(train_df_date_rg['weekday'], palette = 'Blues_d', order = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'])\nplt.xlabel(\"Weekday\", fontsize = 14)\nplt.ylabel(\"Count\",fontsize = 14)\nplt.title(\"Users over week days\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Friday had more number of users in the Gstore."},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize = (8,6))\nsns.countplot(train_df_date['weekday'], palette = 'Blues_d', order = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'])\nplt.xlabel(\"Weekday\", fontsize = 14)\nplt.ylabel(\"Count\",fontsize = 14)\nplt.title(\"Revenue generating users over week days\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Gstore users certainly spends more more money on Fridays and generally on Weekdays."},{"metadata":{},"cell_type":"markdown","source":"**Now let's further explore the adcontent, the ads/adwords which brought users to Gstore:**"},{"metadata":{"trusted":true},"cell_type":"code","source":"from wordcloud import WordCloud\n\nad_content = train['trafficSource_adContent'].fillna('')\nwordcloud_ad = WordCloud(width=800, height=400, background_color=\"white\").generate(' '.join(ad_content))\nplt.figure( figsize=(12,9))\nplt.imshow(wordcloud_ad)\nplt.axis(\"off\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Now through which sources did most users come from?**"},{"metadata":{"trusted":true},"cell_type":"code","source":"source = train['trafficSource_source'].fillna('')\nwordcloud_source = WordCloud(width=800, height=400, background_color=\"white\").generate(' '.join(source))\nplt.figure( figsize=(12,9))\nplt.imshow(wordcloud_source)\nplt.axis(\"off\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Conclusion"},{"metadata":{},"cell_type":"markdown","source":"* So, based on the plots above it can be inferred that, most revenue generating customers come from US who use Chrome browser on Mac. They are most likely to spend their money on Fridays and during holiday seasons. \n* Since, we saw referrals bought more revenue than other sources, it is wise to give referral coupons or similar referral bonuses to bring more revenue to Gstore. Similar referral strategies can be used in other potential revenue generating countries like Canada, UK and India.\n* From the word clouds above, it is seen that, more customers come to Gstore through google search/youtube. There is a good chance to attract customers by giving more ads in these platforms, preferably during holiday seasons of respective countries."}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}