{"cells":[{"metadata":{"_uuid":"50292b445e7e87a29bfb949705354c768a5da401"},"cell_type":"markdown","source":"### What’s a Customer Worth?"},{"metadata":{"_uuid":"69c10f3853043807670e79aedf48ee6c475b594c"},"cell_type":"markdown","source":"![](https://cdn-images-1.medium.com/max/800/1*46_4Ps1cl9Ansa2f0BpaeQ.png)"},{"metadata":{"_uuid":"c4aa812d97e8ad92a39ed8f70212c684d42db70b"},"cell_type":"markdown","source":"### Objective"},{"metadata":{"_uuid":"36400eaeefa5d369a53528c9d4386a48032e306c"},"cell_type":"markdown","source":"Customers keep coming and going, but they do so silently. \n\n1. Is there a specific metric that weights the relationship between the customers and the business? \n2. What are the individual components that play a vital role in calculating this metric?\n3. Using the individual components, how do we calculate the metric? \n\nOne such metric is CLV (Customer Life Time Value). The objective of this kernal is to understand how CLV is calculated. "},{"metadata":{"trusted":true,"_uuid":"f132e1e32254ad69c12c1c5d08a1c3e1c7f9139d"},"cell_type":"code","source":"import numpy as np \nimport pandas as pd\n\nhist = pd.read_csv('../input/historical_transactions.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"160a091442fd6cf9ea63e848f8357237a854d873"},"cell_type":"code","source":"hist = hist[['card_id','purchase_date','purchase_amount']]\nhist = hist.sort_values(by=['card_id', 'purchase_date'], ascending=[True, True])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"95a58a04640df6cd6881b965028df61eb8fbd626"},"cell_type":"markdown","source":"### (R)ecency (F)requency (M)onitary Value"},{"metadata":{"_uuid":"94cec18a39317a6a892b1871624746eade98433f"},"cell_type":"markdown","source":"Why are we subsetting just three columns in historical transactions dataset? For the CLV models, the following components are used:\n\n* Recency - This represents the age of the customer when they made their latest transactions. (Current_date - last_transaction_date)\n* Frequency - This represents the total number of transactions/number of visits a customer has made. (Count of total transactions)\n* Monitary - This represents the total purchase amount that a specified customer has made. (Sum of purchase_amt)\n* Time - This represents the age of the customer. Time span between a customer’s first and last transaction."},{"metadata":{"trusted":true,"_uuid":"c78a0e3777d2752e7e3fb40ad28e8d5c023d8560"},"cell_type":"code","source":"hist.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6727031e3adacf58f9ee84efb1b2e4051897bc62"},"cell_type":"code","source":"## Time\nfrom datetime import datetime\n\nz = hist.groupby('card_id')['purchase_date'].max().reset_index()\nq = hist.groupby('card_id')['purchase_date'].min().reset_index()\n\nz.columns = ['card_id', 'Max']\nq.columns = ['card_id', 'Min']\n\n## Extracting current timestamp\nnow = datetime.now()\ncurr_date = now.strftime(\"%m-%d-%Y, %H:%M:%S\")\ncurr_date = pd.to_datetime(curr_date)\n\nrec = pd.merge(z,q,how = 'left',on = 'card_id')\nrec['Min'] = pd.to_datetime(rec['Min'])\nrec['Max'] = pd.to_datetime(rec['Max'])\n\n## Time value \nrec['Recency'] = (curr_date - rec['Max']).astype('timedelta64[D]') ## current date - most recent date\n\n## Recency value\nrec['Time'] = (rec['Max'] - rec['Min']).astype('timedelta64[D]') ## Age of customer, MAX - MIN\n\nrec = rec[['card_id','Time','Recency']]\nrec.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8865d31a2b3be48afbfa8bbee02f29351f29fde5"},"cell_type":"code","source":"## Frequency\nfreq = hist.groupby('card_id').size().reset_index()\nfreq.columns = ['card_id', 'Frequency']\nfreq.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d0dad0fbaaed3e157453305f6d116c7966aaa3a1"},"cell_type":"code","source":"## Monitary\nmon = hist.groupby('card_id')['purchase_amount'].sum().reset_index()\nmon.columns = ['card_id', 'Monitary']\nmon.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5ce82bc17b37aaf21d4034a06cb56796669ffa51"},"cell_type":"code","source":"final = pd.merge(freq,mon,how = 'left', on = 'card_id')\nfinal = pd.merge(final,rec,how = 'left', on = 'card_id')\n\nfinal['historic_CLV'] = final['Frequency'] * final['Monitary'] \nfinal['AOV'] = final['Monitary']/final['Frequency'] ## AOV - Average order value (i.e) total_purchase_amt/total_trans\nfinal['Predictive_CLV'] = final['Time']*final['AOV']*final['Monitary']*final['Recency'] \n\nfinal.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0696e07f8d884c803c362e1c500bcf67eb407566"},"cell_type":"markdown","source":"### Hope these features boost your model performance. HAPPY KAGGLING! "}],"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}