{"cells":[{"metadata":{"_kg_hide-input":true,"trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Contents\n- [Introduction](#introduction)\n    * [Problem Statement](#problem-statement)\n- [Exploratory Data Analysis](#eda)\n- [Feature Engineering](#feature-engineering)\n- [Model Building](#model-building)\n    * [Linear Regression](#linear-regression)\n    * [Gradient Bososted Tree](#gradient-boosted-trees)\n- [Model Analysis](#model-analysis)\n- [Conclusion](#conclusion)\n\n\n"},{"metadata":{},"cell_type":"markdown","source":"## Introduction<a class=\"anchor\" id=\"introduction\"></a>"},{"metadata":{},"cell_type":"markdown","source":"\nElo, one of Brazil's largest payment brands, is partnered with many merchants to offer promotions and discounts to their cardholders. Elo aimed to reduce marketting that is irrelevant to members and offer them custom-tailored promotions, thereby providing an enjoyable experience and beneficial service. To that end, Elo launched a Kaggle competition, enlisting the Kaggle community's help, to produce a machine learning model that can find signal between trasaction data and loyalty. Such a model will help Elo gauge customer loyalty and how promotional strategies affect it.\n\n**The data provided is simulated and fictitious. It does not contain real customer data.*\n"},{"metadata":{},"cell_type":"markdown","source":"### Problem Statement<a class=\"anchor\" id=\"problem-statement\"></a>\nBuild a machine learning model that can effectively predict customer loyalty scores using trasaction data."},{"metadata":{},"cell_type":"markdown","source":"## Exploratory Data Analysis <a class=\"anchor\" id=\"eda\"></a>"},{"metadata":{},"cell_type":"markdown","source":"The data contained five main tables consisting **train data**, **test data**, **historical transactions**, **new merchant transactions** and **merchants**.\n\n**train data** table contained card_ids, loyalty scores and 3 arbitrary features provided by ELO. The aribtrary features were not very useful as they did not provide much signal in predicting loyalty scores.\n\n**test data** table contained the same arbitrary features as **train data** and card_id but did not contain loyalty scores.\n\n**historical transactions** contained details on purchases made by all the cardholders in the dataset. Details included purchase amount, merchant category, etc. Every card_id had atleaset 3 months of records.\n\n**new merchant transactions** contained transaction data from new merchants the cardholder had not yet purchased from in the historical transaction data. Every card_id had upto two months of new merchant data after a reference date (which differed for each card_id).\n\n**merchants** contained details on merchants seen in **historical transactions** and **new merchant transactions**\n\n\nThe data was heavily anonymized. Non-numerical data such as city of purchase and merchant category were reassigned with arbitrary value so it is difficult to connect real world knowledge and find insights.\n\n\n\n"},{"metadata":{"trusted":true,"_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"# load data\n\n# train data\ntraindf = pd.read_csv('../input/elo-merchant-category-recommendation/train.csv')\n\n# given test data with no loyaltly score\ngiventestdf = pd.read_csv('../input/elo-merchant-category-recommendation/test.csv')\ngiventestdf['card_id'].nunique()\n\n# historical transaction data\nhisttransdf = pd.read_csv('../input/elo-merchant-category-recommendation/historical_transactions.csv')\n\n# new merchant transactional data\nnewtransdf = pd.read_csv('../input/elo-merchant-category-recommendation/new_merchant_transactions.csv')\n\n# merchant data\nmerchdf = pd.read_csv('../input/elo-merchant-category-recommendation/merchants.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"# training dataset at glance\ntraindf.head()","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true,"collapsed":true},"cell_type":"code","source":"traindf.card_id.nunique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"histtransdf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"histtransdf['card_id'].nunique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"histtransdf.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"newtransdf.card_id.nunique()","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true},"cell_type":"code","source":"merchdf['merchant_id'].nunique()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"There are 201,917 card ids in the training data set.\n\nThere are 123,623 card ids in the test data. This data cannot be used to train the model as it does not have loyalty scores (the response variable)\n\nHisotrical transaction data has 325,540 card ids with 29,112,361 transactions. Train and test card ids are both included in this historical data.\n\nNew merchant transactional data set had 290,001 card ids.\n\nThere were 334,633 different merchants in the merchant data."},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"# plotting loyalty score distribution\ntraindf['target'].hist()\nplt.xlabel('Loyalty Score')\nplt.ylabel('Count')\nplt.title('Loyalty Score Distribution');","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Loyalty scores are normally distriubted ranging from -10 to 10. There are some outliers at -33."},{"metadata":{},"cell_type":"markdown","source":"## Feature Engineering<a class='anchor' id='feature-engineering'></a>"},{"metadata":{"trusted":true},"cell_type":"markdown","source":"`NDR` - (New Dollar Ratio) Calculated by dividing amount of dollars"},{"metadata":{"trusted":true},"cell_type":"code","source":"# concatenating all transaction data and adding indicator of new merchant\nhisttransdf['new'] = False\nnewtransdf['new'] = True\nalltransdf = pd.concat([histtransdf, newtransdf])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"alltransdf['purchase_date'] = pd.to_datetime(alltransdf['purchase_date'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"trx.authorized_flag = trx.authorized_flag.apply(lambda x: True if x == 'Y' else False)\ntrx.city_id = trx.city_id.apply(str)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Model Building<a class='anchor' id='model-building'></a>"},{"metadata":{},"cell_type":"markdown","source":"### Linear Regression <a class='anchor' id='linear-regression'></a>"},{"metadata":{},"cell_type":"markdown","source":"### Gradient Boosted Trees <a class=\"anchor\" id='gradient-boosted-trees'></a>"},{"metadata":{},"cell_type":"markdown","source":"## Conclusion <a class='anchor' id='conclusion'></a>"},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}