{"cells":[{"metadata":{"_uuid":"b962f5efb171eaccb9850bb7431de04f1ee1958b"},"cell_type":"markdown","source":"# INTRODUCTION \n@ Team winner_winner_chicken_dinner <br/>\n[Smitha_Acha](https://www.kaggle.com/smithaachar)r and [Arjun_Singh ](https://www.kaggle.com/arjunsinghyadav2)\n## ELO Is trying to predict the Customer Loyalty Score\n## We will analyze the data here and try to come up with a strategy to use. "},{"metadata":{"trusted":true,"_uuid":"f650a5b8826349210206fcda0447a6446f07cfaf"},"cell_type":"code","source":"from IPython.display import HTML\n\nHTML('''\n<script>\ncode_show=true; \nfunction code_toggle() {\n if (code_show){\n $('div.input').hide();\n } else {\n $('div.input').show();\n }\n code_show = !code_show\n} \n$( document ).ready(code_toggle);\n</script>\n<form action=\"javascript:code_toggle()\"><input type=\"submit\" value=\"Click here to toggle on/off the raw code.\"></form>\n''')\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"_kg_hide-output":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 \nimport matplotlib.pyplot as plt                   #For graphics\nimport seaborn as sns #For better looking graphics\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":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"# Reading the Train data\ntrain = pd.read_csv('../input/train.csv')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f5b27bbecdeef74de589560fd7658c4f02a39681"},"cell_type":"markdown","source":"# Training data overview"},{"metadata":{"trusted":true,"_uuid":"844b2f380104276c064b769c63ff1f96d79fb112"},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0cdb872564111dffc895e50cfab6af5dd01e5254"},"cell_type":"markdown","source":"### Total unique Customers"},{"metadata":{"trusted":true,"_uuid":"e0ca667c37b735f73724bb77255edc624d37a2d0"},"cell_type":"code","source":"# Total number of unique customers\ntrain.card_id.nunique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"17bccc7574b1098d7a24030316b83335e41ea465"},"cell_type":"code","source":"# Getting the right format of date\ntrain['first_active_month'] = pd.to_datetime(train['first_active_month'],format='%Y%m',infer_datetime_format=True)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"26382e08bf00f0e079af93e975a53283258c3339"},"cell_type":"markdown","source":"### Data Types"},{"metadata":{"trusted":true,"_uuid":"7b31bd4021822e096e524a4e7073f7e5215d2a8a"},"cell_type":"code","source":"# Checking the data type\ntrain.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3aa3f720cc3630d2e3f04cd1dd55b1902c651378"},"cell_type":"code","source":"# Converting features to String type\ntrain['feature_1'] = train['feature_1'].apply(lambda x:str(x))\ntrain['feature_2'] = train['feature_2'].apply(lambda x:str(x))\ntrain['feature_3'] = train['feature_3'].apply(lambda x:str(x))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"381dd133ad461c0ea862d5079d02b6291b8b8be5"},"cell_type":"markdown","source":"## How has been the popularity of ELO across the years?"},{"metadata":{"trusted":true,"_uuid":"4ab6652ab628af51b74013a2817f9b066bcc661c"},"cell_type":"code","source":"temp_series = train[['first_active_month','card_id']].groupby('first_active_month').aggregate({'card_id':'count'}).reset_index()\nplt.plot(temp_series['first_active_month'],temp_series['card_id'])\nplt.xlabel(('Time'))\nplt.ylabel(('Count of Customers'))\nplt.title('Popularity over time for ELO products')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d79dfabd16a6fba657dc1573eaf40afe48cd3377"},"cell_type":"markdown","source":"### The number of customers have grown exponentially since the loyalty program started in the end of 2011\n#### 2018 hardly has any data, for now it maybe a good idea to drop it completely, we can come back it later."},{"metadata":{"_uuid":"4e4f85234beb4455dba75a30a437be6cbe6eecf8"},"cell_type":"markdown","source":"## How has the outlook of all new customers been towards ELO products?"},{"metadata":{"trusted":true,"_uuid":"14fb805850fd1e7e0bf51a6286c330e090f1dc70"},"cell_type":"code","source":"temp_series1 = train[['first_active_month','target']].groupby('first_active_month').aggregate({'target':'sum'}).reset_index()\nplt.plot(temp_series1['first_active_month'],temp_series1['target'])\nplt.xlabel('Time')\nplt.ylabel('customers loyalty score')\nplt.title('Outlook of ELO Products')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"36a5dfe3dab2a8579a4c294d2f8f2f11599a242d"},"cell_type":"markdown","source":"### We can see that it has been fairly negative, the uplift in 2018 is almost a false flag as this could be totally due to small sample with mostly positive score."},{"metadata":{"_uuid":"cf02566bb7759e747b975269d58682433a8190f8"},"cell_type":"markdown","source":"# Type of Cards\n### The train and test data has three loyalty card features\n\n### Feature 1 | Feature 2 | Feature 3\n### The feature are annonamized but they need to be infered in oder to understand loyalty programs"},{"metadata":{"_uuid":"9114235f92fcc09031eed5fad5d53d0d6ca10fcf"},"cell_type":"markdown","source":"## Two type of Feature 3 cards \n***To make things easy to relate to, I like to name stuff<br/>\nSo from now on we will use following terminology<br/>\nFeature 3 - Label 1 is our Economy Card<br/>\nFeature 3 - Label 0 is our Premium Card<br/>***"},{"metadata":{"trusted":true,"_uuid":"038618c9fcace1d8cfe37f666447c6e3fb57f253"},"cell_type":"code","source":"temp_series4 = train[['feature_3','card_id']].groupby('feature_3').aggregate({'card_id':'count'}).reset_index()\ntemp_series4 = temp_series4.sort_values('card_id',ascending=False)\nplt.bar(temp_series4['feature_3'],temp_series4['card_id'])\nplt.xlabel(('Economy Card    &     Premium Card'))\nplt.ylabel(('Count of unique card id'))\nplt.title('Two type in Feature 3')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"17ab5fc1d1d760d082d02e1de6ae989cd5b36506"},"cell_type":"markdown","source":"## Card feature 1 is of 5 category where 3 being the most popular label\n## I wonder if feature 1 category are related to feature 3 in some way"},{"metadata":{"trusted":true,"_uuid":"ecf794b025d92557c41e68e53d511283df827a7c"},"cell_type":"code","source":"temp_series2 = train[['feature_1','card_id']].groupby('feature_1').aggregate({'card_id':'count'}).reset_index()\ntemp_series2 = temp_series2.sort_values('card_id',ascending=False)\nplt.bar(temp_series2['feature_1'],temp_series2['card_id'])\nplt.xlabel(('Type of Feature 1'))\nplt.ylabel(('count of card id'))\nplt.title('Distribution of Feature 1')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1e4cad34a9cfde4e49d7506aa87872fdc585f97b","_kg_hide-output":true},"cell_type":"code","source":"# Where feature 3 is Economy what is feature 1\ntrain[train['feature_3']=='1'].feature_1.unique()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9430ab1abed82b4453242ee0afcf8bc06bcf9d45"},"cell_type":"markdown","source":"## So Economy card has two categories\n***Feature 1 - Label 5 - Copper<br/>\nFeature 1 - Label 3 - Bronze***"},{"metadata":{"trusted":true,"_uuid":"5b93c33e722de90b4c973a973f234241691677eb"},"cell_type":"code","source":"## And lets check Premium card for Feature 1 labels \ntrain[train['feature_3']=='0'].feature_1.unique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f8ee42c1faf447a5735c7b70f80dde92ebab1e7a"},"cell_type":"code","source":"# Frequncy of each feature 1 in Premium Cards\ntrain[train['feature_3']=='0'].feature_1.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6eddff89371f97e80b9717ef691df44a686d00d8"},"cell_type":"markdown","source":"## Premium Card has 3 Features\n*** Feature 1 - Label 1 - Platinum<br/> \nFeature 1 - Label 2 - Silver<br/>\nFeature 1 - Label 3 - Gold***"},{"metadata":{"trusted":true,"_uuid":"96f6dbaadb6a85f7d109c315d59046ac0753cc9f"},"cell_type":"code","source":"temp_series3 = train[['feature_2','card_id']].groupby('feature_2').aggregate({'card_id':'count'}).reset_index()\ntemp_series3 = temp_series3.sort_values('card_id',ascending=False)\nplt.bar(temp_series3['feature_2'],temp_series3['card_id'])\nplt.xlabel('Silver      Gold       Platinum')\nplt.ylabel('Count of card id')\nplt.title('Premium Card Type in Feature 1')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"deb3991fe31c3ce1e817d3bcf82ee51f59ef913b"},"cell_type":"markdown","source":"### Feature 2 has 3 type of labels 1 is most popular type"},{"metadata":{"trusted":true,"_uuid":"76ca382aee959022fb3ee8d4d9c5f6e1376a2709"},"cell_type":"markdown","source":"## For Economy vs Premium Card \n## Let's visualize how the distribution has been for feature 2"},{"metadata":{"trusted":true,"_uuid":"4248d47f5018ac64d3a58ede6c01f4e59feaa22f"},"cell_type":"code","source":"# Frequncy of each feature 2 in Economy Cards\neco_fe2 = train[train['feature_3']=='1'].feature_2.value_counts().reset_index().rename(columns={'feature_2':'Economy'})\n# Frequncy of each feature 2 in Premium Cards\nprem_fe2 = train[train['feature_3']=='0'].feature_2.value_counts().reset_index().rename(columns={'feature_2':'Premium'})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5c55c64a9f13775aa3ce6ffb0027deccd7e0114a"},"cell_type":"code","source":"#merge the two\neco_prem = pd.merge(eco_fe2,prem_fe2,on='index',how='inner')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dd969d3439b7936d7e082c96cd9a4243b810e873"},"cell_type":"code","source":"ind = eco_prem.index.tolist()  # the x locations for the groups\n\nfig = plt.figure(figsize=(12,8))\nax = fig.add_subplot(111)\n\nyvals = eco_prem.Economy.tolist()\nrects1 = ax.bar(ind, yvals, width= 0.25, color='r')\nzvals = eco_prem.Premium.tolist()\nrects2 = ax.bar(ind, zvals, width=0.25, color='g')\n\nax.set_ylabel('Frequency')\nax.set_xlabel('Feature_2')\nax.set_xticks(ind)\nax.set_xticklabels( ('1', '2', '3') )\nax.set_title('Feature 2 type distribution in Economy and Premium Card')\nax.legend(('Economy', 'Premium') )","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0779c99bc15f57c78e6139947cf407c64e805167"},"cell_type":"markdown","source":"## Feature two seems to be the only one common across all cutomers \nSo it has to be something related to the personality or difference in the consumer age<br/>\nGiven label 3 in lowest frequecy:<br/>\n***Lets say Label - 3 is Entreprenuers <br/>\nLabel- 2 is Students <br/>\nLabel -1 is Working Professionals***"},{"metadata":{"trusted":true,"_uuid":"903a190770d18d05cf629e95e2d312f273345aef"},"cell_type":"code","source":"train['year'] = train['first_active_month'].dt.year\ntarin_back = train\ntrain = train[train['year']!=2018]\n# Loyalty Card behaviour by Students, Working Professionals and Entreprenuers\nwork_pro = train[train['feature_1']=='1'][['year','target']].groupby('year').aggregate({'target':'sum'}).reset_index()\n\nstud = train[train['feature_1']=='2'][['year','target']].groupby('year').aggregate({'target':'sum'}).reset_index()\nentre = train[train['feature_1']=='3'][['year','target']].groupby('year').aggregate({'target':'sum'}).reset_index()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"43c090a6dd7e073f63016dffb21b005e4068d268"},"cell_type":"markdown","source":"### Finally lets look at the outlook of these three customer bases differently throughout the history of ELO"},{"metadata":{"trusted":true,"_uuid":"8e30a94af44a0925161651091748f978cf43a70d"},"cell_type":"code","source":"entre = entre.rename(columns={'target':'entrepreneur'})\nwork_pro = work_pro.rename(columns={'target':'working professional'})\nstud = stud.rename(columns={'target':'student'})\nentre_work = pd.merge(entre,work_pro,on='year',how='outer')\nentre_work_stud = pd.merge(entre_work,stud,on='year',how='outer')\nentre_work_stud.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9b19fcbe57e1b9cae4751164463443b19c856423"},"cell_type":"code","source":"entre_work_stud.set_index('year',inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5f451c5e56baabaa87a7f492b176f7a1ab7dcdfe"},"cell_type":"code","source":"plt.figure(figsize=(15,7))\nplt.xlabel('Target for New Joins by Feature 2')\n\nax1 = entre_work_stud['working professional'].plot(color='blue', grid=True, label='Working Professional')\nax2 = entre_work_stud['student'].plot(color='red', grid=True, secondary_y=True, label='Student')\nax3 = entre_work_stud['entrepreneur'].plot(color='green', grid=True, secondary_y=True, label='Entrepreneur')\n\nh1, l1 = ax1.get_legend_handles_labels()\nh2, l2 = ax2.get_legend_handles_labels()\nh3, l3 = ax3.get_legend_handles_labels()\n\nplt.legend(h1+h2, l1+l2, loc=2)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"06432b7540b1cf92172b0a8fc209af0edc6898e1"},"cell_type":"markdown","source":"### The Working Professionals have been quite negative about ELO service compared to Student and Entrepreneurs, Ideally we should treat them in two seperate groups and build seperate models."},{"metadata":{"trusted":true,"_uuid":"d9640bcb013315d9b4a58c27491948432927355a"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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}