{"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":"code","source":"import gc\nimport shap\nimport random\nimport numpy as np\nimport pandas as pd\nimport lightgbm as lgb\nfrom sklearn.model_selection import train_test_split\nfrom lightgbm import LGBMClassifier, early_stopping, log_evaluation\nfrom pandas.api.types import is_numeric_dtype\n\nSEED = 42\nrandom.seed(SEED)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-06-27T19:32:24.420186Z","iopub.execute_input":"2022-06-27T19:32:24.420680Z","iopub.status.idle":"2022-06-27T19:32:24.428294Z","shell.execute_reply.started":"2022-06-27T19:32:24.420632Z","shell.execute_reply":"2022-06-27T19:32:24.426940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ntrain = pd.read_feather('../input/amexfeather/train_data.ftr')","metadata":{"execution":{"iopub.status.busy":"2022-06-27T19:32:24.660063Z","iopub.execute_input":"2022-06-27T19:32:24.660952Z","iopub.status.idle":"2022-06-27T19:32:34.274770Z","shell.execute_reply.started":"2022-06-27T19:32:24.660898Z","shell.execute_reply":"2022-06-27T19:32:34.273538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Generating features","metadata":{}},{"cell_type":"code","source":"agg_train = pd.DataFrame()\ntrain['S_2'] = pd.to_datetime(train['S_2'])\nagg_train['S_2_min'] = train[['S_2','customer_ID']].groupby('customer_ID')['S_2'].first()\nagg_train['S_2_max'] = train[['S_2','customer_ID']].groupby('customer_ID')['S_2'].last()\nagg_train['S_2_period'] = (agg_train['S_2_max'] - agg_train['S_2_min']).dt.days\nagg_train['S_2_start_month'] = agg_train['S_2_min'].apply(lambda x: x.strftime('%m'))","metadata":{"execution":{"iopub.status.busy":"2022-06-27T19:32:34.277522Z","iopub.execute_input":"2022-06-27T19:32:34.278019Z","iopub.status.idle":"2022-06-27T19:32:41.122355Z","shell.execute_reply.started":"2022-06-27T19:32:34.277973Z","shell.execute_reply":"2022-06-27T19:32:41.120222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def agg_feat_eng(df):\n    num_features = [col for col in df.columns if is_numeric_dtype(df[col]) and col != 'target']\n    agg_feature_names = [f'{feat}_mean' for feat in num_features] + [f'{feat}_std' for feat in num_features]\n    num_feats_agg = df.groupby('customer_ID')[num_features].agg(['mean','std'])\n    num_feats_agg.columns = agg_feature_names\n    return num_feats_agg","metadata":{"execution":{"iopub.status.busy":"2022-06-27T19:32:41.124730Z","iopub.execute_input":"2022-06-27T19:32:41.125601Z","iopub.status.idle":"2022-06-27T19:32:41.133353Z","shell.execute_reply.started":"2022-06-27T19:32:41.125550Z","shell.execute_reply":"2022-06-27T19:32:41.132263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_agg = agg_feat_eng(train)\ntrain_df = train.groupby('customer_ID').tail(1).set_index('customer_ID')\ntrain_df = pd.concat([train_df,train_agg], axis=1)\n\ndel train\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-06-27T19:32:41.135876Z","iopub.execute_input":"2022-06-27T19:32:41.136233Z","iopub.status.idle":"2022-06-27T19:33:24.254129Z","shell.execute_reply.started":"2022-06-27T19:32:41.136197Z","shell.execute_reply":"2022-06-27T19:33:24.253109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.concat([train_df, agg_train[['S_2_period','S_2_start_month']]], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-06-27T19:33:24.255337Z","iopub.execute_input":"2022-06-27T19:33:24.255860Z","iopub.status.idle":"2022-06-27T19:33:24.835119Z","shell.execute_reply.started":"2022-06-27T19:33:24.255806Z","shell.execute_reply":"2022-06-27T19:33:24.834092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Pre-processing","metadata":{}},{"cell_type":"code","source":"object_cols = [col for col in train_df.columns if str(train_df[col].dtype) == 'object']\ntrain_df[object_cols] = train_df[object_cols].astype('category')\n\ntrain_cols = [col for col in train_df.columns if col not in ['customer_ID','target','S_2']]\nX_train, X_val, y_train, y_val = train_test_split(train_df[train_cols], train_df['target'], test_size=0.2, random_state=SEED)\n\ndel train_df\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-06-27T19:33:26.565757Z","iopub.execute_input":"2022-06-27T19:33:26.566260Z","iopub.status.idle":"2022-06-27T19:33:30.754577Z","shell.execute_reply.started":"2022-06-27T19:33:26.566210Z","shell.execute_reply":"2022-06-27T19:33:30.753134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Training LightGBM Model","metadata":{}},{"cell_type":"code","source":"dtrain = lgb.Dataset(X_train, y_train)\ndeval = lgb.Dataset(X_val, y_val)\n\nparams = {'objective':'binary',\n          'learning_rate':0.05,\n          'metric':['auc','binary_logloss'],\n          'max_depth':7,\n          'num_leaves':70,\n          'verbose':-1\n         }\n\ndel X_train\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-06-27T19:33:30.756107Z","iopub.execute_input":"2022-06-27T19:33:30.756573Z","iopub.status.idle":"2022-06-27T19:33:31.279637Z","shell.execute_reply.started":"2022-06-27T19:33:30.756536Z","shell.execute_reply":"2022-06-27T19:33:31.278407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nmodel = lgb.train(params, dtrain, num_boost_round=1000, valid_sets=[dtrain, deval], callbacks=[early_stopping(50), log_evaluation(500)])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Feature Ranking based on lgb importances","metadata":{}},{"cell_type":"code","source":"feature_importance_df = pd.DataFrame({'feature_name': model.feature_name(),'tree_split': model.feature_importance(importance_type='split')})\nfeature_importance_df = feature_importance_df.sort_values(by='tree_split', ascending=False)\nfeature_importance_df['lgb_rank'] = feature_importance_df['tree_split'].rank(method='first',ascending=False).astype(int)\nlgb.plot_importance(model, max_num_features = 20,figsize=(10, 9))","metadata":{"execution":{"iopub.status.busy":"2022-06-27T19:38:40.186197Z","iopub.execute_input":"2022-06-27T19:38:40.187357Z","iopub.status.idle":"2022-06-27T19:38:40.542808Z","shell.execute_reply.started":"2022-06-27T19:38:40.187300Z","shell.execute_reply":"2022-06-27T19:38:40.541309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Feature Ranking based on shap importances","metadata":{}},{"cell_type":"code","source":"%%time\nexplainer = shap.TreeExplainer(model)\nshap_values = explainer.shap_values(X_val)\nshap.summary_plot(shap_values, features=X_val, feature_names=X_val.columns)","metadata":{"execution":{"iopub.status.busy":"2022-06-27T19:38:40.544216Z","iopub.execute_input":"2022-06-27T19:38:40.544583Z","iopub.status.idle":"2022-06-27T19:38:41.643813Z","shell.execute_reply.started":"2022-06-27T19:38:40.544551Z","shell.execute_reply":"2022-06-27T19:38:41.642605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"means = [np.abs(shap_values[class_]).mean(axis=0) for class_ in range(len(shap_values))]\nshap_means = np.sum(np.column_stack(means), 1)\nimportance_df = pd.DataFrame({'feature_name': X_val.columns, 'mean_shap_value': shap_means}).sort_values(by='mean_shap_value', ascending=False).reset_index(drop=True)\nimportance_df['shap_rank'] = importance_df['mean_shap_value'].rank(method='first',ascending=False).astype(int)","metadata":{"execution":{"iopub.status.busy":"2022-06-27T19:38:41.647050Z","iopub.execute_input":"2022-06-27T19:38:41.647493Z","iopub.status.idle":"2022-06-27T19:38:42.020541Z","shell.execute_reply.started":"2022-06-27T19:38:41.647454Z","shell.execute_reply":"2022-06-27T19:38:42.019289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_importance_df = feature_importance_df.merge(importance_df,on='feature_name')","metadata":{"execution":{"iopub.status.busy":"2022-06-27T19:38:42.022419Z","iopub.execute_input":"2022-06-27T19:38:42.022792Z","iopub.status.idle":"2022-06-27T19:38:42.034334Z","shell.execute_reply.started":"2022-06-27T19:38:42.022759Z","shell.execute_reply":"2022-06-27T19:38:42.032929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_importance_df","metadata":{"execution":{"iopub.status.busy":"2022-06-27T19:38:42.035967Z","iopub.execute_input":"2022-06-27T19:38:42.036459Z","iopub.status.idle":"2022-06-27T19:38:42.053472Z","shell.execute_reply.started":"2022-06-27T19:38:42.036409Z","shell.execute_reply":"2022-06-27T19:38:42.052643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}