{"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":"I'd like to grasp the difference train-data and test-data with adversarial validation before conducting feature(variable) engineerings.\nEspecially, which variables make the difference.... I wanna understand that with Adversarial Validation concept as bellow.","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\nfrom sklearn.preprocessing import LabelEncoder\n\nfrom sklearn.metrics import roc_auc_score\nfrom sklearn.metrics import roc_curve, auc\nfrom sklearn.model_selection import train_test_split\n\nimport lightgbm as lgb\n\nimport matplotlib.pyplot as plt\nplt.style.use('ggplot')\n\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2022-10-02T03:20:37.695880Z","iopub.execute_input":"2022-10-02T03:20:37.696555Z","iopub.status.idle":"2022-10-02T03:20:40.057932Z","shell.execute_reply.started":"2022-10-02T03:20:37.696399Z","shell.execute_reply":"2022-10-02T03:20:40.056640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load train.csv\ntrain_df = pd.read_csv('../input/digital-turbine-auction-bid-price-prediction/train_data.csv')\n\nprint(f'train_df.shape = {train_df.shape}')\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-02T03:20:40.060250Z","iopub.execute_input":"2022-10-02T03:20:40.061214Z","iopub.status.idle":"2022-10-02T03:21:17.076155Z","shell.execute_reply.started":"2022-10-02T03:20:40.061163Z","shell.execute_reply":"2022-10-02T03:21:17.074893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load test.csv\ntest_df = pd.read_csv('../input/digital-turbine-auction-bid-price-prediction/test_data.csv')\n\nprint(f'test_df.shape = {test_df.shape}')\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-02T03:21:17.077535Z","iopub.execute_input":"2022-10-02T03:21:17.077859Z","iopub.status.idle":"2022-10-02T03:21:17.304185Z","shell.execute_reply.started":"2022-10-02T03:21:17.077829Z","shell.execute_reply":"2022-10-02T03:21:17.302961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Set \"flag\" on train-dataset and test-dataset, and then, we will check whether LightGBM model will be able to classify the data as train-data or test-data or not.","metadata":{}},{"cell_type":"code","source":"# Set \"flag\" on train-data and test-data\n# \ntrain_df['target'] = 0\ntest_df['target'] = 1\n\n\n# conbine train data and test data\nall_df = pd.concat([train_df, test_df], axis=0)\n\nall_df","metadata":{"execution":{"iopub.status.busy":"2022-10-02T03:21:17.307618Z","iopub.execute_input":"2022-10-02T03:21:17.308519Z","iopub.status.idle":"2022-10-02T03:21:27.709671Z","shell.execute_reply.started":"2022-10-02T03:21:17.308468Z","shell.execute_reply":"2022-10-02T03:21:27.708482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Label Encoding\n\n# obtain objective variables\ncategories = all_df.columns[all_df.dtypes=='object'].values.tolist()\n\n# Label Encoding\nfor cat in categories:\n    \n    # manage missing values\n    all_df[cat].fillna('unknown', inplace=True)\n    \n    # Label Encoding\n    le_train = LabelEncoder()\n    le_train = le_train.fit(all_df[cat])\n    all_df[cat] = le_train.transform(all_df[cat])\n    \n    # convert the data into Category\n    all_df[cat] = all_df[cat].astype('category')","metadata":{"execution":{"iopub.status.busy":"2022-10-02T03:21:27.710927Z","iopub.execute_input":"2022-10-02T03:21:27.711780Z","iopub.status.idle":"2022-10-02T03:22:05.031757Z","shell.execute_reply.started":"2022-10-02T03:21:27.711744Z","shell.execute_reply":"2022-10-02T03:22:05.030636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# set ovjective variable\ny_all = all_df['target']\n\n# set explanatory variable\nx_all = all_df.drop(['deviceId', 'has_won', 'winBid', 'target'], axis=1)\n\nx_all","metadata":{"execution":{"iopub.status.busy":"2022-10-02T03:22:05.033100Z","iopub.execute_input":"2022-10-02T03:22:05.033486Z","iopub.status.idle":"2022-10-02T03:22:05.517104Z","shell.execute_reply.started":"2022-10-02T03:22:05.033451Z","shell.execute_reply":"2022-10-02T03:22:05.516011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# split the data\nx_train_adv, x_valid_adv, y_train_adv, y_valid_adv = train_test_split(\n    x_all, y_all, test_size=0.33, random_state=42, shuffle=True)\n\n# set up LightGBM\nmodel = lgb.LGBMClassifier(\nn_estimators=200, \nrandom_state=42)\n\n# model fitting\nmodel.fit(\n    x_train_adv, y_train_adv, \n    eval_set = [(x_train_adv, y_train_adv), (x_valid_adv, y_valid_adv)], \n    eval_names = ['train', 'valid'], \n    eval_metric = 'auc', \n    verbose=20\n)","metadata":{"execution":{"iopub.status.busy":"2022-10-02T03:22:05.518649Z","iopub.execute_input":"2022-10-02T03:22:05.518962Z","iopub.status.idle":"2022-10-02T03:26:15.850817Z","shell.execute_reply.started":"2022-10-02T03:22:05.518933Z","shell.execute_reply":"2022-10-02T03:26:15.849749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# draw auc\nax = lgb.plot_metric(model.evals_result_, metric='auc',figsize=(8, 6))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-02T03:26:15.852382Z","iopub.execute_input":"2022-10-02T03:26:15.852852Z","iopub.status.idle":"2022-10-02T03:26:16.133447Z","shell.execute_reply.started":"2022-10-02T03:26:15.852805Z","shell.execute_reply":"2022-10-02T03:26:16.132276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"AUC score of the validation has been reached over 0.88.\nIt indicates that LightGBM model grasps the certain difference between train-dataset and test-dataset with some variables as follows.","metadata":{}},{"cell_type":"code","source":"# grasp feature importance\nlgb.plot_importance(model, importance_type='gain', max_num_features=15, figsize=(8, 6));","metadata":{"execution":{"iopub.status.busy":"2022-10-02T03:26:16.134825Z","iopub.execute_input":"2022-10-02T03:26:16.135639Z","iopub.status.idle":"2022-10-02T03:26:16.498705Z","shell.execute_reply.started":"2022-10-02T03:26:16.135589Z","shell.execute_reply":"2022-10-02T03:26:16.497465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"According to the above AUC and Feature importance with LightGBM, it seems that some variables such as C1 etc has the different data distributions between its train-dataset and its test-dataset.\nHence, it could be better to care about the difference when we carry out their feature engineerings.","metadata":{}}]}