{"cells":[{"metadata":{"_uuid":"85d7c93830c7ca26b87038be6026269617684096"},"cell_type":"markdown","source":"These features are inspired from raddar findings and suggestions: https://www.kaggle.com/raddar/target-true-meaning-revealed\n\nIt helped me improve my CV and LB by 0.002; hope it helps you too.\nI have taken two months gap since it is given in data dictionary that target is \"Loyalty numerical score calculated 2 months after historical and evaluation period\""},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport datetime\n\nimport gc\nimport sys\nimport os","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"### \n# train = pd.read_csv('../input/train.csv')\nhist = pd.read_csv('../input/historical_transactions.csv')\n# new = pd.read_csv('../input/new_merchant_transactions.csv')\n# merchant = pd.read_csv('../input/merchants.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2e9884bb05b1e89fb21c1c3d2350361db2531e78"},"cell_type":"code","source":"agg_func = {\n        'purchase_amount': ['sum'],\n        'installments':['sum']\n        }\n    \nagg_history = hist.groupby(['card_id','month_lag']).agg(agg_func)\nagg_history.columns = ['_'.join(col).strip() for col in agg_history.columns.values]\nagg_history.reset_index(inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c2def6b025ac5d13535e9cfad785f03da1a957b0"},"cell_type":"code","source":"tmp = agg_history.loc[agg_history['month_lag'] == 0]\ntmp = tmp[['card_id']].reset_index(drop = True)\n\nfor i in range(-13,-1):\n    LAG = agg_history.loc[agg_history['month_lag'] == i ]\n    lag = agg_history.loc[agg_history['month_lag'] == i+2]\n    \n    LAG = LAG.merge(lag,how = 'left',on = 'card_id')\n    \n    LAG['ratio_month_lag_purchase_amount'+'_'+str(i)+'_'+str(i+2)] = LAG['purchase_amount_sum_y']/LAG['purchase_amount_sum_x']\n#     LAG['ratio_month_lag_installments'+'_'+str(i)+'_'+str(i+2)] = LAG['installments_sum_y']/LAG['installments_sum_x']\n    \n    LAG = LAG[['card_id','ratio_month_lag_purchase_amount'+'_'+str(i)+'_'+str(i+2)]]\n    \n    tmp = tmp.merge(LAG,on = \"card_id\",how = \"left\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"817ca125e804e058a348d64af2f0177fd41efba9"},"cell_type":"code","source":"tmp['average_increase_rate_by_2months'] = tmp.iloc[:,1:].sum(axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7cd2c014aef0cb8dbc9a27453249a8e7ee3699ff"},"cell_type":"code","source":"tmp.to_csv(\"monthly_rate.csv\",index=False)","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}