{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"---\n<a id='section1'></a>\n# Introduction\nAs part of the Elo Merchant Category Recommendation Kaggle competition, this kernel aims to predict a merchant loyalty score for a certain credict card holder.","metadata":{"_uuid":"bc4c357422d83e54ff6a3a5bd365a0ea83293597"}},{"cell_type":"markdown","source":"<a id='section2'></a>\n# Import libraries","metadata":{"_uuid":"fce441d178384af65e37edccd98eff7a5eace1ee"}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport math\nimport datetime\n\nimport lightgbm as lgb\nimport xgboost as xgb\nfrom sklearn.model_selection import KFold\nfrom sklearn.ensemble import RandomForestRegressor\nfrom sklearn.neighbors import KNeighborsRegressor\nfrom sklearn.tree import DecisionTreeRegressor\nfrom sklearn.linear_model import Ridge, Lasso\nfrom lightgbm.sklearn import LGBMRegressor\nfrom xgboost.sklearn import XGBRegressor\nfrom catboost import CatBoostRegressor\nfrom sklearn.base import BaseEstimator, TransformerMixin, RegressorMixin\nfrom mlxtend.regressor import StackingCVRegressor\n\nfrom sklearn.metrics import mean_squared_error\nfrom sklearn.model_selection import cross_val_predict\nfrom sklearn.model_selection import cross_val_score\nfrom sklearn.metrics import make_scorer\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom IPython.display import Image\nimport warnings\n\n%matplotlib inline\nwarnings.filterwarnings('ignore')\nsns.set(style='white', context='notebook', palette='deep')","metadata":{"_uuid":"7fd9dbaacddeea6781a4dba565f1b8f9a6809855","execution":{"iopub.status.busy":"2022-03-10T07:40:00.717501Z","iopub.execute_input":"2022-03-10T07:40:00.718032Z","iopub.status.idle":"2022-03-10T07:40:02.47037Z","shell.execute_reply.started":"2022-03-10T07:40:00.717907Z","shell.execute_reply":"2022-03-10T07:40:02.46952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Image(url= \"https://upload.wikimedia.org/wikipedia/commons/1/10/Logo-ELO-NEG-Black.png\")","metadata":{"_uuid":"8c4735eef6c9fa72c60731b87a6c842096c87e5d","_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-03-10T07:40:02.471702Z","iopub.execute_input":"2022-03-10T07:40:02.471972Z","iopub.status.idle":"2022-03-10T07:40:02.481453Z","shell.execute_reply.started":"2022-03-10T07:40:02.471923Z","shell.execute_reply":"2022-03-10T07:40:02.480448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n<a id='section4'></a>\n# EDA ","metadata":{"_uuid":"9f7a441d365fddc8b65fe7dc16873e20b4966c60"}},{"cell_type":"markdown","source":"<a id='section5'></a>\n## Read in the data","metadata":{"_uuid":"da1a7ff853f16bae6e9b64fd95e549c839bfe03d"}},{"cell_type":"code","source":"# Historical and new transactions data\nhist_trans = pd.read_csv('../input/historical_transactions.csv')\nnew_trans = pd.read_csv('../input/new_merchant_transactions.csv')\n\n# Train and Test data\ntrain = pd.read_csv('../input/train.csv', parse_dates=['first_active_month'])\ntest = pd.read_csv('../input/test.csv', parse_dates=['first_active_month'])\n\ntrain_idx = train.shape[0]\ntest_idx = test.shape[0]\n\nprint(\"--------------------------\")\nprint(\"Train shape: \", train.shape)\nprint(\"Test shape: \", test.shape)\nprint(\"--------------------------\")\nprint(\"Historical transactions shape: \", hist_trans.shape)\nprint(\"New transactions shape: \", new_trans.shape)","metadata":{"scrolled":true,"_uuid":"3823241830309287b76ca513ed0b60540924c256","execution":{"iopub.status.busy":"2022-03-10T07:40:02.482978Z","iopub.execute_input":"2022-03-10T07:40:02.48333Z","iopub.status.idle":"2022-03-10T07:42:01.341478Z","shell.execute_reply.started":"2022-03-10T07:40:02.483265Z","shell.execute_reply":"2022-03-10T07:42:01.340536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='section6'></a>\n### Description","metadata":{"_uuid":"e5636c2dd01f8ad96e9fe1ca33023dcf2f21c3ca"}},{"cell_type":"code","source":"print(\"----------------------------------------------------------------\")\nprint(\"Train\")\nprint(\"----------------------------------------------------------------\")\nprint(train.info())\nprint(\"\\n----------------------------------------------------------------\")\nprint(\"Test\")\nprint(\"----------------------------------------------------------------\")\nprint(train.info())\nprint(\"\\n----------------------------------------------------------------\")\nprint(\"Historical transactions\")\nprint(\"----------------------------------------------------------------\")\nprint(hist_trans.info())\nprint(\"\\n----------------------------------------------------------------\")\nprint(\"New transactions\")\nprint(\"----------------------------------------------------------------\")\nprint(new_trans.info())","metadata":{"_uuid":"5b789a7fc633ce9f60a2cf162bd40c0740b6c433","_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-03-10T07:42:01.342781Z","iopub.execute_input":"2022-03-10T07:42:01.343004Z","iopub.status.idle":"2022-03-10T07:42:01.458954Z","shell.execute_reply.started":"2022-03-10T07:42:01.342965Z","shell.execute_reply":"2022-03-10T07:42:01.458121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"_uuid":"8989d7be6fd5381bc1a32cb02692ed49538ef599","execution":{"iopub.status.busy":"2022-03-10T07:42:01.46029Z","iopub.execute_input":"2022-03-10T07:42:01.460538Z","iopub.status.idle":"2022-03-10T07:42:01.49017Z","shell.execute_reply.started":"2022-03-10T07:42:01.460497Z","shell.execute_reply":"2022-03-10T07:42:01.489305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"_uuid":"26402c76a8abcc55275ee1556bdb6eb218b3f24c","execution":{"iopub.status.busy":"2022-03-10T07:42:01.491313Z","iopub.execute_input":"2022-03-10T07:42:01.491556Z","iopub.status.idle":"2022-03-10T07:42:01.511762Z","shell.execute_reply.started":"2022-03-10T07:42:01.491513Z","shell.execute_reply":"2022-03-10T07:42:01.510813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist_trans.head()","metadata":{"scrolled":true,"_uuid":"a4cd541e6087eb643a88b3c6ee2c3202e35270bc","execution":{"iopub.status.busy":"2022-03-10T07:42:01.51326Z","iopub.execute_input":"2022-03-10T07:42:01.513603Z","iopub.status.idle":"2022-03-10T07:42:01.553425Z","shell.execute_reply.started":"2022-03-10T07:42:01.513546Z","shell.execute_reply":"2022-03-10T07:42:01.552399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_trans.head()","metadata":{"_uuid":"5ba2ec54556b5bb3af862f82ddc6f7bee13a9860","execution":{"iopub.status.busy":"2022-03-10T07:42:01.554643Z","iopub.execute_input":"2022-03-10T07:42:01.554863Z","iopub.status.idle":"2022-03-10T07:42:01.590198Z","shell.execute_reply.started":"2022-03-10T07:42:01.554823Z","shell.execute_reply":"2022-03-10T07:42:01.589183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='section7'></a>\n### Target column","metadata":{"_uuid":"9ee70e910d3d114edc24608d8f2abc0136fc41df"}},{"cell_type":"code","source":"print(\"Target description:\\n\\n\", train['target'].describe())\nprint(\"\\n--------------------------------------------------------------------------------------------\")\nprint(\"\\nTarget values:\\n\\n\", train['target'].value_counts())","metadata":{"scrolled":true,"_uuid":"1d4a0dc19490e62d7633ddf53eb63a534c636a78","execution":{"iopub.status.busy":"2022-03-10T07:42:01.591722Z","iopub.execute_input":"2022-03-10T07:42:01.592123Z","iopub.status.idle":"2022-03-10T07:42:01.636509Z","shell.execute_reply.started":"2022-03-10T07:42:01.591931Z","shell.execute_reply":"2022-03-10T07:42:01.6358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(20,6))\nax1, ax2 = axes.flatten()\n\n# Distribution\nsns.distplot(train['target'], ax=ax1, color='Green')\n\n# Sorted correlations with target\nsorted_corrs = train.corr()['target'].sort_values(ascending=False)\nsns.heatmap(train[sorted_corrs.index].corr(), ax=ax2)\n\nax1.set_title('Target Distribution')\nax2.set_title('Correlations')\nplt.show()\ndel sorted_corrs","metadata":{"_uuid":"2a9e9200759c54ff3bb5f93695bb66e5e6582563","execution":{"iopub.status.busy":"2022-03-10T07:42:01.637697Z","iopub.execute_input":"2022-03-10T07:42:01.637945Z","iopub.status.idle":"2022-03-10T07:42:02.551622Z","shell.execute_reply.started":"2022-03-10T07:42:01.637895Z","shell.execute_reply":"2022-03-10T07:42:02.550771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"There seem to be 2207 values around -33 for the target column, which follows a normal distribution. Let's take that into account. Also, 'feature_3' correlates better than 'feature_2' and 'feature_1' with 'target'.\n\nLet's confirm the number of values under -30.","metadata":{"_uuid":"e840b09a6664d64ecd5d4b0d8ec0a1587546d348"}},{"cell_type":"code","source":"under_30 = train.loc[train['target'] < -30, 'target'].count()\nprint(\"Under -30:\", under_30, \"values.\")","metadata":{"_uuid":"797fad254aef2122a61e3becb1150bcc39d337cf","execution":{"iopub.status.busy":"2022-03-10T07:42:02.552827Z","iopub.execute_input":"2022-03-10T07:42:02.553103Z","iopub.status.idle":"2022-03-10T07:42:02.56286Z","shell.execute_reply.started":"2022-03-10T07:42:02.553053Z","shell.execute_reply":"2022-03-10T07:42:02.561947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='section8'></a>\n# Cleaning\n<a id='section9'></a>\n## Missing","metadata":{"_uuid":"bf72d2b62c1957a9123400c4c536999e71d975eb"}},{"cell_type":"code","source":"print(\"MISSING VALUES BEFORE CLEANING\\n\")\nprint(\"--------------------------------------------------\\nTrain:\\n--------------------------------------------------\\n\", train.isnull().sum())\nprint(\"\\n--------------------------------------------------\\nTest:\\n--------------------------------------------------\\n\", test.isnull().sum())\nprint(\"\\n--------------------------------------------------\\nHistorical transactions:\\n--------------------------------------------------\\n\", hist_trans.isnull().sum())\nprint(\"\\n--------------------------------------------------\\nNew transactions:\\n--------------------------------------------------\\n\", new_trans.isnull().sum())","metadata":{"_uuid":"f4dde287b21391b070cc054974b080289679a478","_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-03-10T07:42:02.564787Z","iopub.execute_input":"2022-03-10T07:42:02.565098Z","iopub.status.idle":"2022-03-10T07:42:26.61Z","shell.execute_reply.started":"2022-03-10T07:42:02.565043Z","shell.execute_reply":"2022-03-10T07:42:26.609124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='section10'></a>\n### Train/Test\nThere's no null values for train. However, there seem to be one observation with a missing 'first_active_month' in test.","metadata":{"_uuid":"c1b5b610a4bdd5eeadd0166e02a87e07df2da11d"}},{"cell_type":"code","source":"test_missing = test[test.isnull()['first_active_month']]\nidx_test_missing = test_missing.index\ntest_missing","metadata":{"scrolled":true,"_uuid":"d7bc10e6a05c27a9711e8f5742d0370dd70228e6","execution":{"iopub.status.busy":"2022-03-10T07:42:26.611186Z","iopub.execute_input":"2022-03-10T07:42:26.611473Z","iopub.status.idle":"2022-03-10T07:42:26.642378Z","shell.execute_reply.started":"2022-03-10T07:42:26.611428Z","shell.execute_reply":"2022-03-10T07:42:26.641404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We can look for all the observations that match the same 'feature_1', 'feature_2' and 'feature_3' values as that, and replace it with the 'first_active_month' that corresponds to their mode.","metadata":{"_uuid":"59c9245da5c9dfd2126564d248c17c78412c8157"}},{"cell_type":"code","source":"same_category = test[(test['feature_1'] == 5) & (test['feature_2'] == 2) & (test['feature_3'] == 1)]\ntest.loc[idx_test_missing, 'first_active_month'] = same_category['first_active_month'].mode()[0]\n\ndel same_category\ntest.iloc[11578]","metadata":{"_uuid":"c2043796522cec743879176f48e783a87b25f280","execution":{"iopub.status.busy":"2022-03-10T07:42:26.643784Z","iopub.execute_input":"2022-03-10T07:42:26.644119Z","iopub.status.idle":"2022-03-10T07:42:26.666958Z","shell.execute_reply.started":"2022-03-10T07:42:26.644055Z","shell.execute_reply":"2022-03-10T07:42:26.665967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='section12'></a>\n### Historical transactions","metadata":{"_uuid":"4c9106aa1013b24d882d5eaafdb4a9ec521aaeaa"}},{"cell_type":"markdown","source":"Similarly, in this case let's drop the missing rows for 'category_3' and 'merchant_id' (less than 1% of total).","metadata":{"_uuid":"73a733b89c4fe8d932988790d9f20d8edbf57bfd"}},{"cell_type":"code","source":"hist_trans.dropna(subset=['category_3', 'merchant_id'], inplace=True)","metadata":{"_uuid":"0b856b50ebee191791a9d7209850c30a9fb26b1b","execution":{"iopub.status.busy":"2022-03-10T07:42:26.667826Z","iopub.execute_input":"2022-03-10T07:42:26.668063Z","iopub.status.idle":"2022-03-10T07:42:37.152464Z","shell.execute_reply.started":"2022-03-10T07:42:26.668018Z","shell.execute_reply":"2022-03-10T07:42:37.15162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"For 'category_2', since it has about 10% of missing values, let's replace them with the rounded average value (since values for this column include [1.0, 2.0, 3.0, 4.0, 5.0]), as seen below.","metadata":{"_uuid":"0b9bf60a72fe44a06efee1c4f508794d03bfdcdc"}},{"cell_type":"code","source":"hist_trans['category_2'].describe()","metadata":{"_uuid":"5b28cc963cb4cb3c9beb77cc290369a842cb2956","execution":{"iopub.status.busy":"2022-03-10T07:42:37.153811Z","iopub.execute_input":"2022-03-10T07:42:37.15407Z","iopub.status.idle":"2022-03-10T07:42:38.636521Z","shell.execute_reply.started":"2022-03-10T07:42:37.154022Z","shell.execute_reply":"2022-03-10T07:42:38.635923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist_trans['category_2'].fillna((math.floor(hist_trans['category_2'].mean())), inplace=True)","metadata":{"_uuid":"05298d903433f659549f04357e3a53e44e2232ee","execution":{"iopub.status.busy":"2022-03-10T07:42:38.637553Z","iopub.execute_input":"2022-03-10T07:42:38.637928Z","iopub.status.idle":"2022-03-10T07:42:38.76524Z","shell.execute_reply.started":"2022-03-10T07:42:38.637891Z","shell.execute_reply":"2022-03-10T07:42:38.764213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='section13'></a>\n### New transactions\nOnce more, since there are missing values in 'category_3', 'merchant_id', 'category_2' and they add up to no more than about 5%, let's  drop the corresponding rows.","metadata":{"_uuid":"cfa81652acc852727daf924d9e773ac6c8a7ac20"}},{"cell_type":"code","source":"new_trans.dropna(inplace=True)","metadata":{"_uuid":"e0a68cdb0c324ed0c7c38c884a197c7dd2d4fd67","execution":{"iopub.status.busy":"2022-03-10T07:42:38.766836Z","iopub.execute_input":"2022-03-10T07:42:38.767351Z","iopub.status.idle":"2022-03-10T07:42:40.378675Z","shell.execute_reply.started":"2022-03-10T07:42:38.767249Z","shell.execute_reply":"2022-03-10T07:42:40.377833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Lastly, let's confirm no null values are present after cleaning.","metadata":{"_uuid":"b088973b84f6188fa80c3bff937da6ad7a8a980f"}},{"cell_type":"code","source":"print(\"MISSING VALUES AFTER CLEANING\\n\")\nprint(\"--------------------------------------------------\\nTrain:\\n--------------------------------------------------\\n\", train.isnull().sum())\nprint(\"\\n--------------------------------------------------\\nTest:\\n--------------------------------------------------\\n\", test.isnull().sum())\n#print(\"\\n--------------------------------------------------\\nMerchant:\\n--------------------------------------------------\\n\", merchants.isnull().sum())\nprint(\"\\n--------------------------------------------------\\nHistorical transactions:\\n--------------------------------------------------\\n\", hist_trans.isnull().sum())\nprint(\"\\n--------------------------------------------------\\nNew transactions:\\n--------------------------------------------------\\n\", new_trans.isnull().sum())","metadata":{"_uuid":"5e8e90e505d9f7bb5bde5892d7beae0b32d1d2fc","_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-03-10T07:42:40.379816Z","iopub.execute_input":"2022-03-10T07:42:40.380057Z","iopub.status.idle":"2022-03-10T07:43:04.205655Z","shell.execute_reply.started":"2022-03-10T07:42:40.380015Z","shell.execute_reply":"2022-03-10T07:43:04.204491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='section14'></a>\n# Feature engineering","metadata":{"_uuid":"508ee325bf8080cafeda0aaa84be12312d868cdd"}},{"cell_type":"code","source":"# Merge train and test for data processing\ndata = pd.concat([train, test], ignore_index=True)\n\n# Check shapes match\nprint(\"Train ({}) + Test ({}) observations: {}\".format(train.shape[0], test.shape[0], train.shape[0] + test.shape[0]))\nprint(\"Merged shape:\", data.shape)\n\ndel train\ndel test","metadata":{"_uuid":"1f65b7975b3560416afea49f9e9dc8027a928425","execution":{"iopub.status.busy":"2022-03-10T07:43:04.208027Z","iopub.execute_input":"2022-03-10T07:43:04.208277Z","iopub.status.idle":"2022-03-10T07:43:04.222989Z","shell.execute_reply.started":"2022-03-10T07:43:04.208234Z","shell.execute_reply":"2022-03-10T07:43:04.222195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='section15'></a>\n## Train/Test","metadata":{"_uuid":"88819bc2106733914565af8f2dbd34b92e4836d5"}},{"cell_type":"code","source":"# Year and month, separately\ndata['year'] = data['first_active_month'].dt.year\ndata['month'] = data['first_active_month'].dt.month\n\n# Elapsed time, until the latest date on the dataset\ndata['elapsed_time'] = (datetime.date(2018, 2, 1) - data['first_active_month'].dt.date).dt.days\n\n# Categorical features: 'feature_1', 'feature_2' and 'feature_3'\ncont = 1\nfor col in ['feature_1', 'feature_2', 'feature_3']:\n    dummy_col = pd.get_dummies(data[col], prefix='f{}'.format(cont))\n    data = pd.concat([data, dummy_col], axis=1)\n    data.drop(col, axis=1, inplace=True)\n    cont += 1\n    \ndata.head()","metadata":{"_uuid":"5203ec3e8ddff1cc528a3daf4904790049acc45e","execution":{"iopub.status.busy":"2022-03-10T07:43:04.224243Z","iopub.execute_input":"2022-03-10T07:43:04.224505Z","iopub.status.idle":"2022-03-10T07:43:06.549256Z","shell.execute_reply.started":"2022-03-10T07:43:04.22446Z","shell.execute_reply":"2022-03-10T07:43:06.548495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='section18'></a>\n### Historical + New transactions\nLet's create a column called 'new' on 'hist_trans' and 'new_trans' such that, before concatening them, they have the age reference:\n\n- 1: New\n- 0: Historical","metadata":{"_uuid":"bd7839d5879c7b295ce9cc5adb5d629582bb96b1"}},{"cell_type":"code","source":"new_trans['new'] = 1\nhist_trans['new'] = 0\n\n# Concatenate new_trans and hist_trans\ntrans_data = pd.concat([new_trans, hist_trans])\n\ndel new_trans\ndel hist_trans","metadata":{"_uuid":"a96aad5d6ee63045ed506278cf813fc04f132ba0","execution":{"iopub.status.busy":"2022-03-10T07:43:06.550509Z","iopub.execute_input":"2022-03-10T07:43:06.550727Z","iopub.status.idle":"2022-03-10T07:43:18.577829Z","shell.execute_reply.started":"2022-03-10T07:43:06.550689Z","shell.execute_reply":"2022-03-10T07:43:18.577043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"More preprocessing: 'category_1', 'category_2' and 'category_3'.","metadata":{"_uuid":"25ffde1a3370c7b22206f99105753fd78ceed7d6"}},{"cell_type":"code","source":"# Change Yes/No for 0/1 in 'authorized_flag' and 'category_1'\nyes_no_dict = {'Y':1, 'N':0}\ntrans_data['authorized_flag'] = trans_data['authorized_flag'].map(yes_no_dict)\ntrans_data['category_1'] = trans_data['category_1'].map(yes_no_dict)\n\n# Create five different cols for 'category_2'\ndummy_col = pd.get_dummies(trans_data['category_2'], prefix='category_2')\ntrans_data = pd.concat([trans_data, dummy_col], axis=1)\ntrans_data.drop('category_2', axis=1, inplace=True)\n    \n# Create three different cols for categorical A/B/C in 'category_3'\ndummy_col = pd.get_dummies(trans_data['category_3'], prefix='cat3')\ntrans_data = pd.concat([trans_data, dummy_col], axis=1)\ntrans_data.drop('category_3', axis=1, inplace=True)\n\ntrans_data.head()","metadata":{"_uuid":"d8f3d085b5b71f796caf03ea2f1d3b7664437870","execution":{"iopub.status.busy":"2022-03-10T07:43:18.579189Z","iopub.execute_input":"2022-03-10T07:43:18.579439Z","iopub.status.idle":"2022-03-10T07:44:08.209357Z","shell.execute_reply.started":"2022-03-10T07:43:18.579397Z","shell.execute_reply":"2022-03-10T07:44:08.208685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n<a id='section40'></a>\n### Aggregate function\nAggregate function, grouped by 'card_id': min, max, mean, median, std, sum, nunique, range. \n\nAdded:\n- Count on 'installments' and 'purchase_amount'.\n- Mode() on 'new' column (previously created).\n- Mean on new trans_data's category_2 dummy columns.\n- Mean on trans_data's category_4.\n- Mean on 'cat3_A', 'cat3_B' and 'cat3_C' (old 'category_3').\n- Mean on merchants' new dummy columns.","metadata":{"_uuid":"ff3c2a444f70196b03a528454155084eac64bc2d"}},{"cell_type":"code","source":"def aggregate_historical_transactions(trans_data):\n    \n    trans_data.loc[:, 'purchase_date'] = pd.DatetimeIndex(trans_data['purchase_date']).astype(np.int64)*1e-9\n    \n    agg_func = {\n        'authorized_flag': ['sum', 'mean'],\n        'category_1': ['mean'],\n        'category_2_1.0': ['mean'],\n        'category_2_2.0': ['mean'],\n        'category_2_3.0': ['mean'],\n        'category_2_4.0': ['mean'],\n        'category_2_5.0': ['mean'],\n        'cat3_A': ['mean'],\n        'cat3_B': ['mean'],\n        'cat3_C': ['mean'],\n        'merchant_id': ['nunique'],\n        'merchant_category_id': ['nunique'],\n        'state_id': ['nunique'],\n        'city_id': ['nunique'],\n        'subsector_id': ['nunique'],\n        'purchase_amount': ['count', 'sum', 'median', 'max', 'min', 'std'],\n        'installments': ['count', 'sum', 'median', 'max', 'min', 'std'],\n        'purchase_date': [np.ptp],\n        'month_lag': ['min', 'max'],\n        'new':[lambda x:x.value_counts().index[0]] # Mode\n        }\n    \n    agg_history = trans_data.groupby(['card_id']).agg(agg_func)\n    agg_history.columns = ['_'.join(col).strip() for col in agg_history.columns.values]\n    agg_history.reset_index(inplace=True)\n\n    df = (trans_data.groupby('card_id').size().reset_index(name='transactions_count'))\n    \n    agg_history = pd.merge(df, agg_history, on='card_id', how='left')\n    \n    return agg_history\n\ntrans_data = aggregate_historical_transactions(trans_data)\ntrans_data.head()","metadata":{"_uuid":"3d8b855c737d0f49354bbb1c903b0c280aed37b7","execution":{"iopub.status.busy":"2022-03-10T07:44:08.21032Z","iopub.execute_input":"2022-03-10T07:44:08.210661Z","iopub.status.idle":"2022-03-10T07:52:00.260106Z","shell.execute_reply.started":"2022-03-10T07:44:08.210624Z","shell.execute_reply":"2022-03-10T07:52:00.259278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n<a id='section20'></a>\n# Data preparation\n<a id='section56'></a>\n## Merge","metadata":{"_uuid":"66e57d7dc83fbfdfd4916957d82f8f7e7ab635d7"}},{"cell_type":"code","source":"# Merch data (train + test) with trans_data (historical + new transactions)\nprocessed_data = pd.merge(data, trans_data, on='card_id', how='left')\ndel data\ndel trans_data\nprint(processed_data.shape)\nprocessed_data.head()","metadata":{"scrolled":true,"_uuid":"53d7e3aee837b0b4224d2550ace7e663c37c74c1","execution":{"iopub.status.busy":"2022-03-10T07:52:00.261275Z","iopub.execute_input":"2022-03-10T07:52:00.261687Z","iopub.status.idle":"2022-03-10T07:52:01.093001Z","shell.execute_reply.started":"2022-03-10T07:52:00.261645Z","shell.execute_reply":"2022-03-10T07:52:01.092149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='section57'></a>\n## Final Train/Test ","metadata":{"_uuid":"076ddd56d65cb691157172cf6f7cdf7fb64ffac2"}},{"cell_type":"code","source":"# Train and Test\ntrain = processed_data[:train_idx]\ntest = processed_data[train_idx:]\n\ndel processed_data\n\n# There are some nan values after feature eng in 'purchase_amount_std' and 'installments_std'\ncols = ['purchase_amount_std', 'installments_std']\n\nfor col in cols:\n    train[col].fillna((train[col].value_counts().index[0]), inplace=True)\n    test[col].fillna((test[col].value_counts().index[0]), inplace=True)\n\ntarget = train['target']\n\ncols_2_remove = ['target', 'card_id', 'first_active_month']\nfor col in cols_2_remove:  \n    del train[col]\n    del test[col] \n\n# Check on shapes\nprint(\"--------------------------\")\nprint(\"Train shape: \", train.shape)\nprint(\"Test shape: \", test.shape)\nprint(\"--------------------------\")","metadata":{"scrolled":true,"_uuid":"893ad6cdc077fa30b5c2a9899c38b52ca03e2dd0","execution":{"iopub.status.busy":"2022-03-10T07:52:01.094487Z","iopub.execute_input":"2022-03-10T07:52:01.094739Z","iopub.status.idle":"2022-03-10T07:52:01.578675Z","shell.execute_reply.started":"2022-03-10T07:52:01.094688Z","shell.execute_reply":"2022-03-10T07:52:01.577874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n<a id='section21'></a>\n# Modeling and testing","metadata":{"_uuid":"b4dbc2b41f56ccded5728760c3ec76ecdb208765"}},{"cell_type":"markdown","source":"<a id='section22'></a>\n## LightGBM","metadata":{"_uuid":"8c1ffc386ef21ced8f296b15389464965c9bf633"}},{"cell_type":"code","source":"lgb_params = {'num_leaves': 50,\n         'min_data_in_leaf': 30, \n         'objective':'regression',\n         'max_depth': -1,\n         'learning_rate': 0.005,\n         \"boosting\": \"gbdt\",\n         \"feature_fraction\": 0.9,\n         \"bagging_freq\": 1,\n         \"bagging_fraction\": 0.9,\n         \"bagging_seed\": 11,\n         \"metric\": 'rmse',\n         \"lambda_l1\": 0.1,\n         \"verbosity\": -1}\n\nFOLDs = KFold(n_splits=5, shuffle=True, random_state=1989)\n\noof_lgb = np.zeros(len(train))\npredictions_lgb = np.zeros(len(test))\n\nfeatures_lgb = list(train.columns)\nfeature_importance_df_lgb = pd.DataFrame()\n\nfor fold_, (trn_idx, val_idx) in enumerate(FOLDs.split(train)):\n    trn_data = lgb.Dataset(train.iloc[trn_idx], label=target.iloc[trn_idx])\n    val_data = lgb.Dataset(train.iloc[val_idx], label=target.iloc[val_idx])\n\n    print(\"LGB \" + str(fold_) + \"-\" * 50)\n    num_round = 2000\n    clf = lgb.train(lgb_params, trn_data, num_round, valid_sets = [trn_data, val_data], verbose_eval=200, early_stopping_rounds = 2000)\n    oof_lgb[val_idx] = clf.predict(train.iloc[val_idx], num_iteration=clf.best_iteration)\n\n    fold_importance_df_lgb = pd.DataFrame()\n    fold_importance_df_lgb[\"feature\"] = features_lgb\n    fold_importance_df_lgb[\"importance\"] = clf.feature_importance()\n    fold_importance_df_lgb[\"fold\"] = fold_ + 1\n    feature_importance_df_lgb = pd.concat([feature_importance_df_lgb, fold_importance_df_lgb], axis=0)\n    predictions_lgb += clf.predict(test, num_iteration=clf.best_iteration) / FOLDs.n_splits\n    \n\ndel fold_importance_df_lgb\ndel trn_data\ndel val_data\n\nprint(np.sqrt(mean_squared_error(oof_lgb, target)))","metadata":{"_uuid":"89fae7f72e712b834a128c86e9cb504078b78c54","execution":{"iopub.status.busy":"2022-03-10T07:52:01.580232Z","iopub.execute_input":"2022-03-10T07:52:01.580509Z","iopub.status.idle":"2022-03-10T07:56:03.75443Z","shell.execute_reply.started":"2022-03-10T07:52:01.580463Z","shell.execute_reply":"2022-03-10T07:56:03.753236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions_lgb","metadata":{"execution":{"iopub.status.busy":"2022-03-10T08:44:39.037898Z","iopub.execute_input":"2022-03-10T08:44:39.038737Z","iopub.status.idle":"2022-03-10T08:44:39.04863Z","shell.execute_reply.started":"2022-03-10T08:44:39.038661Z","shell.execute_reply":"2022-03-10T08:44:39.04698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='section23'></a>\n## Xgboost","metadata":{"_uuid":"6d4703c8666ebcca9eccd0c42e11f3a01a03e98d"}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"train.rename(index=str, columns={\"new_<lambda>\": \"new_mode\"}, inplace=True)\ntest.rename(index=str, columns={\"new_<lambda>\": \"new_mode\"}, inplace=True)\n\nxgb_params = {'eta': 0.001, 'max_depth': 7, 'subsample': 0.8, 'colsample_bytree': 0.8, \n          'objective': 'reg:linear', 'eval_metric': 'rmse', 'silent': True}\n\nFOLDs = KFold(n_splits=5, shuffle=True, random_state=1989)\n\noof_xgb = np.zeros(len(train))\npredictions_xgb = np.zeros(len(test))\n\nfor fold_, (trn_idx, val_idx) in enumerate(FOLDs.split(train)):\n    trn_data = xgb.DMatrix(data=train.iloc[trn_idx], label=target.iloc[trn_idx])\n    val_data = xgb.DMatrix(data=train.iloc[val_idx], label=target.iloc[val_idx])\n    watchlist = [(trn_data, 'train'), (val_data, 'valid')]\n    print(\"xgb \" + str(fold_) + \"-\" * 50)\n    num_round = 2000\n    xgb_model = xgb.train(xgb_params, trn_data, num_round, watchlist, early_stopping_rounds=100, verbose_eval=200)\n    oof_xgb[val_idx] = xgb_model.predict(xgb.DMatrix(train.iloc[val_idx]), ntree_limit=xgb_model.best_ntree_limit+50)\n\n    predictions_xgb += xgb_model.predict(xgb.DMatrix(test), ntree_limit=xgb_model.best_ntree_limit+50) / FOLDs.n_splits\n\ndel trn_data\ndel val_data\ndel watchlist\n\nnp.sqrt(mean_squared_error(oof_xgb, target))","metadata":{"_uuid":"7163cd80fe006b4b8c2611197953eeddf99c2844","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='section24'></a>\n## Summary of results","metadata":{"_uuid":"a2397101139ce7f24ef63e902b2673e487e65d8a"}},{"cell_type":"code","source":"print(\"-----------------\\nScores on train\\n-----------------\")\nprint('lgb:', np.sqrt(mean_squared_error(oof_lgb, target)))\nprint('xgb:', np.sqrt(mean_squared_error(oof_xgb, target)))\n\ntotal_sum = 0.5*oof_lgb + 0.5*oof_xgb\n\nprint(\"CV score: {:<8.5f}\".format(mean_squared_error(total_sum, target)**0.5))","metadata":{"_uuid":"05575aedc8bb430e1cd5ad74535faf9e0bbd6edd","execution":{"iopub.status.busy":"2022-03-10T08:40:43.788557Z","iopub.status.idle":"2022-03-10T08:40:43.789328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='section25'></a>\n## Feature importance","metadata":{"_uuid":"e5648b79387996410a4f131f581f3cef38771f03"}},{"cell_type":"code","source":"cols = (feature_importance_df_lgb[[\"feature\", \"importance\"]]\n        .groupby(\"feature\")\n        .mean()\n        .sort_values(by=\"importance\", ascending=False)[:1000].index)\n\nbest_features = feature_importance_df_lgb.loc[feature_importance_df_lgb.feature.isin(cols)]\n\nplt.figure(figsize=(14,14))\nsns.barplot(x=\"importance\",\n            y=\"feature\",\n            data=best_features.sort_values(by=\"importance\",\n                                           ascending=False))\nplt.title('LightGBM Features (avg over folds)')\nplt.tight_layout()\nplt.savefig('lgbm_importances.png')\ndel feature_importance_df_lgb","metadata":{"_uuid":"0ad08e5c46bdc980e34d0cb828e36bb7f27a487c","execution":{"iopub.status.busy":"2022-03-10T08:40:43.790615Z","iopub.status.idle":"2022-03-10T08:40:43.791128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n<a id='section26'></a>\n# Ensembled model: averaged and stacked\nThe follwing tests lgbm, xgb, catboost, random forest, decision tree, knn, ridge and lasso models individual performance, and compared for averaged and stacked models.\n<a id='section27'></a>\n## Model definition","metadata":{"_uuid":"97dde43a8c3a54264477e1594f81285b408440a0"}},{"cell_type":"code","source":"# Model definition\ntrain_y = target\n\n# Same lgbm and xgb models as before\nlgbm_model = LGBMRegressor(\n                objective=\"regression\", metric=\"rmse\", \n                max_depth=7, min_child_samples=20, \n                reg_alpha= 1, reg_lambda=1,\n                num_leaves=64, learning_rate=0.001, \n                subsample=0.8, colsample_bytree=0.8, \n                verbosity=-1\n)\n\nxgb_model = XGBRegressor(\n                eta=0.001, max_depth=7, \n                subsample=0.8, colsample_bytree=0.8, \n                objective='reg:linear', eval_metric='rmse', \n                silent=True\n)\n\n\n# Test catboost, random forest, decision tree, knn, ridge and lasso models individual performance, for averaged and stacked model\ncatboost_model = CatBoostRegressor(iterations=150)\nrf_model = RandomForestRegressor(n_estimators=25, min_samples_leaf=25, min_samples_split=25)\ntree_model = DecisionTreeRegressor(min_samples_leaf=25, min_samples_split=25)\nknn_model = KNeighborsRegressor(n_neighbors=25, weights='distance')\nridge_model = Ridge(alpha=75.0)\nlasso_model = Lasso(alpha=0.75)\n\n# ------------------------------------------------------------------------------------------------\n# Average regressor\nclass AveragingRegressor(BaseEstimator, RegressorMixin, TransformerMixin):\n    def __init__(self, regressors):\n        self.regressors = regressors\n        self.predictions = None\n\n    def fit(self, X, y):\n        for regr in self.regressors:\n            regr.fit(X, y)\n        return self\n\n    def predict(self, X):\n        self.predictions = np.column_stack([regr.predict(X) for regr in self.regressors])\n        return np.mean(self.predictions, axis=1)\n    \n# Averaged & stacked models \naveraged_model = AveragingRegressor([catboost_model, xgb_model, rf_model, lgbm_model])\n\n\nstacked_model = StackingCVRegressor(\n    regressors=[catboost_model, xgb_model, rf_model, lgbm_model],\n    meta_regressor=Ridge()\n)\n\n# Test performance\ndef rmse_fun(predicted, actual):\n    return np.sqrt(np.mean(np.square(predicted - actual)))\n\nrmse = make_scorer(rmse_fun, greater_is_better=False)\n\nmodels = [\n     ('CatBoost', catboost_model),\n     ('XGBoost', xgb_model),\n     ('LightGBM', lgbm_model),\n     ('DecisionTree', tree_model),\n     ('RandomForest', rf_model),\n     ('Ridge', ridge_model),\n     ('Lasso', lasso_model),\n     ('KNN', knn_model),\n     ('Averaged', averaged_model),\n     ('Stacked', stacked_model),\n]\n\n\nscores = [\n    -1.0 * cross_val_score(model, train.values, train_y.values, scoring=rmse).mean()\n    for _,model in models\n]","metadata":{"_uuid":"ca94b185e51e01335ba971aca895ffb3894000ce","execution":{"iopub.status.busy":"2022-03-10T08:40:43.792589Z","iopub.status.idle":"2022-03-10T08:40:43.793262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataz = pd.DataFrame({ 'Model': [name for name, _ in models], 'Error (RMSE)': scores })\ndataz.plot(x='Model', kind='bar')\nplt.savefig('stacked_scores.png')","metadata":{"_uuid":"fa4f39a32a51f607880d23cc2d22e5776ecdb7c5","execution":{"iopub.status.busy":"2022-03-10T08:40:43.794134Z","iopub.status.idle":"2022-03-10T08:40:43.794762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='section28'></a>\n## Results","metadata":{"_uuid":"c6587ebbe1aebf1419a981dde40a4633eb44c8fe"}},{"cell_type":"code","source":"dataz","metadata":{"_uuid":"126eafdbd0e620dd2e472ef161944af6f5c178b1","execution":{"iopub.status.busy":"2022-03-10T08:40:43.795676Z","iopub.status.idle":"2022-03-10T08:40:43.796321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='section29'></a>\n## Predictions","metadata":{"_uuid":"dc288a32957c411e8b1efa3a437b3651050bd7ca"}},{"cell_type":"code","source":"# Stacked model predictions (best score)\nstacked_model.fit(train.values, target.values)    \npredictions_stacked = stacked_model.predict(test.values)","metadata":{"scrolled":true,"_uuid":"8f2cae45ef645912354cb61c99d7cc1eea8e2931","execution":{"iopub.status.busy":"2022-03-10T08:40:43.797275Z","iopub.status.idle":"2022-03-10T08:40:43.798045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-10T08:41:45.84529Z","iopub.execute_input":"2022-03-10T08:41:45.845996Z","iopub.status.idle":"2022-03-10T08:41:45.853516Z","shell.execute_reply.started":"2022-03-10T08:41:45.845942Z","shell.execute_reply":"2022-03-10T08:41:45.852573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions_lgb","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions_lgb()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf.predict(test.values)","metadata":{"execution":{"iopub.status.busy":"2022-03-10T08:49:01.601473Z","iopub.execute_input":"2022-03-10T08:49:01.601841Z","iopub.status.idle":"2022-03-10T08:49:09.660614Z","shell.execute_reply.started":"2022-03-10T08:49:01.6018Z","shell.execute_reply":"2022-03-10T08:49:09.659837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.values","metadata":{"execution":{"iopub.status.busy":"2022-03-10T08:49:13.167119Z","iopub.execute_input":"2022-03-10T08:49:13.16784Z","iopub.status.idle":"2022-03-10T08:49:13.184576Z","shell.execute_reply.started":"2022-03-10T08:49:13.167774Z","shell.execute_reply":"2022-03-10T08:49:13.183852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle\n\nfilename = 'clf.sav'\npickle.dump(clf,open(filename, 'wb'))\n","metadata":{"execution":{"iopub.status.busy":"2022-03-10T08:50:08.874582Z","iopub.execute_input":"2022-03-10T08:50:08.874978Z","iopub.status.idle":"2022-03-10T08:50:09.228245Z","shell.execute_reply.started":"2022-03-10T08:50:08.874938Z","shell.execute_reply":"2022-03-10T08:50:09.227366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf_reload = pickle.load(open(filename, 'rb'))1\npred_clf=clf_reload.predict(test.values)\npred_clf","metadata":{"execution":{"iopub.status.busy":"2022-03-10T08:51:27.250191Z","iopub.execute_input":"2022-03-10T08:51:27.250685Z","iopub.status.idle":"2022-03-10T08:51:35.300628Z","shell.execute_reply.started":"2022-03-10T08:51:27.250609Z","shell.execute_reply":"2022-03-10T08:51:35.299726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_clf","metadata":{"execution":{"iopub.status.busy":"2022-03-10T08:51:46.813921Z","iopub.execute_input":"2022-03-10T08:51:46.814593Z","iopub.status.idle":"2022-03-10T08:51:46.820043Z","shell.execute_reply.started":"2022-03-10T08:51:46.814536Z","shell.execute_reply":"2022-03-10T08:51:46.819445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Final_Predicted_CLV = pd.DataFrame(pred_clf)\nFinal_Predicted_CLV.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-10T08:53:56.77165Z","iopub.execute_input":"2022-03-10T08:53:56.771965Z","iopub.status.idle":"2022-03-10T08:53:56.786152Z","shell.execute_reply.started":"2022-03-10T08:53:56.771924Z","shell.execute_reply":"2022-03-10T08:53:56.785276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-03-10T08:55:01.266516Z","iopub.execute_input":"2022-03-10T08:55:01.267168Z","iopub.status.idle":"2022-03-10T08:55:01.367799Z","shell.execute_reply.started":"2022-03-10T08:55:01.267098Z","shell.execute_reply":"2022-03-10T08:55:01.366853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('../input/test.csv', parse_dates=['first_active_month'])\ntest2 = test.copy()\ntest2.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-03-10T08:56:29.899325Z","iopub.execute_input":"2022-03-10T08:56:29.900034Z","iopub.status.idle":"2022-03-10T08:56:30.138151Z","shell.execute_reply.started":"2022-03-10T08:56:29.899958Z","shell.execute_reply":"2022-03-10T08:56:30.137224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Card = pd.DataFrame(test2['card_id'])\nCard.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-10T08:56:37.061156Z","iopub.execute_input":"2022-03-10T08:56:37.061542Z","iopub.status.idle":"2022-03-10T08:56:37.082623Z","shell.execute_reply.started":"2022-03-10T08:56:37.061475Z","shell.execute_reply":"2022-03-10T08:56:37.080686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Final_Predicted_CLV.columns= ['Predicted_CLV']","metadata":{"execution":{"iopub.status.busy":"2022-03-10T08:57:06.458797Z","iopub.execute_input":"2022-03-10T08:57:06.459268Z","iopub.status.idle":"2022-03-10T08:57:06.463955Z","shell.execute_reply.started":"2022-03-10T08:57:06.459223Z","shell.execute_reply":"2022-03-10T08:57:06.46318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Final_Predicted_CLV.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-10T08:57:44.298901Z","iopub.execute_input":"2022-03-10T08:57:44.299526Z","iopub.status.idle":"2022-03-10T08:57:44.312614Z","shell.execute_reply.started":"2022-03-10T08:57:44.29947Z","shell.execute_reply":"2022-03-10T08:57:44.311516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-03-10T08:40:43.815727Z","iopub.status.idle":"2022-03-10T08:40:43.816395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Card=Card.reset_index(drop = True)","metadata":{"execution":{"iopub.status.busy":"2022-03-10T08:57:56.944852Z","iopub.execute_input":"2022-03-10T08:57:56.945353Z","iopub.status.idle":"2022-03-10T08:57:56.951703Z","shell.execute_reply.started":"2022-03-10T08:57:56.945272Z","shell.execute_reply":"2022-03-10T08:57:56.951036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Card.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-10T08:58:07.588018Z","iopub.execute_input":"2022-03-10T08:58:07.58868Z","iopub.status.idle":"2022-03-10T08:58:07.600418Z","shell.execute_reply.started":"2022-03-10T08:58:07.588619Z","shell.execute_reply":"2022-03-10T08:58:07.599658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Final_Predicted_CLV=Final_Predicted_CLV.reset_index(drop = True)","metadata":{"execution":{"iopub.status.busy":"2022-03-10T08:40:43.819804Z","iopub.status.idle":"2022-03-10T08:40:43.820467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Final_Predicted_CLV","metadata":{"execution":{"iopub.status.busy":"2022-03-10T08:40:43.821175Z","iopub.status.idle":"2022-03-10T08:40:43.821722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test2 = pd.read_csv('/content/drive/MyDrive/elo-merchant-category-recommendation/test.csv')\n\nsub_df = pd.DataFrame({\"card_id\":Card[\"card_id\"].values,\"Predicted_CLV\":Final_Predicted_CLV[\"Predicted_CLV\"].values})","metadata":{"execution":{"iopub.status.busy":"2022-03-10T09:11:10.987799Z","iopub.execute_input":"2022-03-10T09:11:10.98813Z","iopub.status.idle":"2022-03-10T09:11:10.994946Z","shell.execute_reply.started":"2022-03-10T09:11:10.988067Z","shell.execute_reply":"2022-03-10T09:11:10.994161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df.head(10)         #Final Predicted CLV","metadata":{"execution":{"iopub.status.busy":"2022-03-10T09:14:39.369976Z","iopub.execute_input":"2022-03-10T09:14:39.370544Z","iopub.status.idle":"2022-03-10T09:14:39.385981Z","shell.execute_reply.started":"2022-03-10T09:14:39.370263Z","shell.execute_reply":"2022-03-10T09:14:39.384976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='section30'></a>\n# Submission","metadata":{"_uuid":"56e8b0f399c3853bfb591e253b86dd111c5a3365"}},{"cell_type":"code","source":"# LightGBM/Xgboost\nsub_df = pd.read_csv('../input/sample_submission.csv')\nsub_df[\"target\"] = 0.5 * predictions_lgb + 0.5 * predictions_xgb\nsub_df.to_csv(\"submission_lgbxgboost.csv\", index=False)\n\n# Stacked\nsub_df = pd.read_csv('../input/sample_submission.csv')\nsub_df[\"target\"] = predictions_stacked\nsub_df.to_csv(\"submission_stacked.csv\", index=False)","metadata":{"_uuid":"344cfaf4f3a8281e53801e59e77be27608fff179","execution":{"iopub.status.busy":"2022-03-10T08:40:43.859817Z","iopub.status.idle":"2022-03-10T08:40:43.860382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"_uuid":"e5498c6e3bbdf5b5f4adf4923b887d48b61ee577","trusted":true},"execution_count":null,"outputs":[]}]}