{"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 pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\npd.set_option('display.max_columns', None)","metadata":{"id":"G0o2xaLYK831","execution":{"iopub.status.busy":"2022-11-02T16:28:42.345225Z","iopub.execute_input":"2022-11-02T16:28:42.346246Z","iopub.status.idle":"2022-11-02T16:28:43.085917Z","shell.execute_reply.started":"2022-11-02T16:28:42.346199Z","shell.execute_reply":"2022-11-02T16:28:43.084881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_df = pd.read_csv('train_data.csv')\n# test_df = pd.read_csv('test_data.csv')\n# actuals = pd.read_csv('test_results.csv')\n\ntrain_df = pd.read_csv('../input/digital-turbine-auction-bid-price-prediction/train_data.csv')\ntest_df = pd.read_csv('../input/digital-turbine-auction-bid-price-prediction/test_data.csv')","metadata":{"id":"kWxQHcLyLXWT","execution":{"iopub.status.busy":"2022-11-02T16:28:48.897522Z","iopub.execute_input":"2022-11-02T16:28:48.898041Z","iopub.status.idle":"2022-11-02T16:29:29.429747Z","shell.execute_reply.started":"2022-11-02T16:28:48.898004Z","shell.execute_reply":"2022-11-02T16:29:29.428697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_df.shape[0])\nprint(test_df.shape[0])","metadata":{"execution":{"iopub.status.busy":"2022-11-02T16:29:29.431769Z","iopub.execute_input":"2022-11-02T16:29:29.432210Z","iopub.status.idle":"2022-11-02T16:29:29.439335Z","shell.execute_reply.started":"2022-11-02T16:29:29.432170Z","shell.execute_reply":"2022-11-02T16:29:29.437951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = train_df.dropna(axis=0, subset=['deviceId'])\ntrain_df.reset_index(inplace=True)\n\n#check missing data\ntrain_df.isnull().any()","metadata":{"id":"Yl1oI0orxCVY","execution":{"iopub.status.busy":"2022-11-02T16:29:38.978465Z","iopub.execute_input":"2022-11-02T16:29:38.978969Z","iopub.status.idle":"2022-11-02T16:29:45.748276Z","shell.execute_reply.started":"2022-11-02T16:29:38.978916Z","shell.execute_reply":"2022-11-02T16:29:45.747012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.sort_values(by = 'eventTimestamp', ascending = False)","metadata":{"id":"xEPxW11q_-NK"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"I tried to look if there is some time pattern for each device on the win bin but at quick site I didn't find.","metadata":{"id":"DnxUhyE_3Hdu"}},{"cell_type":"code","source":"train_df[train_df.deviceId.eq('6a0019a885a')].sort_values(by = 'eventTimestamp', ascending = False)","metadata":{"id":"l3zu5jTU2cjI"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"deviceId is not unique, there are devices with many auctions","metadata":{"id":"_ZJsbc_c0s-U"}},{"cell_type":"code","source":"train_df.groupby('deviceId')['winBid'].count()","metadata":{"id":"4hrmpCpZAJjn","outputId":"49ad035e-c93b-403b-a5c0-a7d9f41d7d30","execution":{"iopub.status.busy":"2022-11-02T16:32:00.994084Z","iopub.execute_input":"2022-11-02T16:32:00.994579Z","iopub.status.idle":"2022-11-02T16:32:02.117568Z","shell.execute_reply.started":"2022-11-02T16:32:00.994538Z","shell.execute_reply":"2022-11-02T16:32:02.116297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Number of deviceId in test that are not in train","metadata":{"id":"5FnxfITB1Un6"}},{"cell_type":"code","source":"test_df['deviceId'].value_counts()","metadata":{"id":"vfgD5HXr1fMg","outputId":"71bf6c5d-10e3-4733-d934-1096d96f69df","execution":{"iopub.status.busy":"2022-11-02T16:30:32.815687Z","iopub.execute_input":"2022-11-02T16:30:32.816794Z","iopub.status.idle":"2022-11-02T16:30:32.871904Z","shell.execute_reply.started":"2022-11-02T16:30:32.816723Z","shell.execute_reply":"2022-11-02T16:30:32.870990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df[test_df.deviceId.isin(train_df.deviceId)==False].shape[0]","metadata":{"id":"LsQDyUCR0z--","outputId":"02c9e5d4-62f7-4eba-8421-1343156c4e35","execution":{"iopub.status.busy":"2022-11-02T16:32:07.981008Z","iopub.execute_input":"2022-11-02T16:32:07.981835Z","iopub.status.idle":"2022-11-02T16:32:08.809737Z","shell.execute_reply.started":"2022-11-02T16:32:07.981777Z","shell.execute_reply":"2022-11-02T16:32:08.808658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"I dont have time to investigate C2 and C4 but the lowest and highest values (1 and 9) are times 2 less occuring then all the others. ","metadata":{"id":"-ixM0MEV5Q-2"}},{"cell_type":"code","source":"train_df['c4'].value_counts()","metadata":{"id":"8D_-jzZ44l5w","outputId":"df626282-c6af-4ef0-af67-1bfd3e139a39","execution":{"iopub.status.busy":"2022-11-02T16:32:32.308391Z","iopub.execute_input":"2022-11-02T16:32:32.308855Z","iopub.status.idle":"2022-11-02T16:32:32.439392Z","shell.execute_reply.started":"2022-11-02T16:32:32.308822Z","shell.execute_reply":"2022-11-02T16:32:32.437814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['c4'].value_counts()","metadata":{"id":"8U51uHhf4S-L","outputId":"3f12196b-3308-4e26-e790-baea40d5d030","execution":{"iopub.status.busy":"2022-11-02T16:32:32.850312Z","iopub.execute_input":"2022-11-02T16:32:32.850826Z","iopub.status.idle":"2022-11-02T16:32:32.862519Z","shell.execute_reply.started":"2022-11-02T16:32:32.850787Z","shell.execute_reply":"2022-11-02T16:32:32.861309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.groupby('c4')['winBid'].agg(['count', 'mean'])","metadata":{"id":"4IMMfU693jCU","outputId":"2339963a-d811-4d03-b4b1-1cafbb140166"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"C3 is correlated with os_type","metadata":{"id":"5NqtLep70moS"}},{"cell_type":"code","source":"train_set.groupby('c3')['os_type'].value_counts()","metadata":{"id":"15rMdkwLtcrX","outputId":"86596354-72e7-4db4-a914-eeaec6398c62","execution":{"iopub.status.busy":"2022-11-02T16:37:51.470693Z","iopub.execute_input":"2022-11-02T16:37:51.472055Z","iopub.status.idle":"2022-11-02T16:37:52.694147Z","shell.execute_reply.started":"2022-11-02T16:37:51.471992Z","shell.execute_reply":"2022-11-02T16:37:52.693078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.groupby('mediationProviderVersion')['winBid'].agg(['count', 'mean'])","metadata":{"id":"QpJZJSpgcG2M"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Target Encoding**\n\nEncode high dimentional categorical features with agg functions","metadata":{"id":"peQfjJp7Vuux"}},{"cell_type":"code","source":"mean_device_winbid = train_df.groupby('deviceId')['winBid'].mean()\nmedian_device_winbid = train_df.groupby('deviceId')['winBid'].median()\nstd_device_winbid = train_df.groupby('deviceId')['winBid'].std()\nmax_device_winbid = train_df.groupby('deviceId')['winBid'].max()\nmin_device_winbid = train_df.groupby('deviceId')['winBid'].min()\n\nmean_deviceId_sentPrice = train_df.groupby('deviceId')['sentPrice'].mean()\nmedian_deviceId_sentPrice = train_df.groupby('deviceId')['sentPrice'].median()\nstd_deviceId_sentPrice = train_df.groupby('deviceId')['sentPrice'].std()\nmax_device_sentPrice = train_df.groupby('deviceId')['sentPrice'].max()\nmin_device_sentPrice = train_df.groupby('deviceId')['sentPrice'].min()\n\nmean_bundleId_winbid = train_df.groupby('bundleId')['winBid'].mean()\nmean_brandName_winbid = train_df.groupby('brandName')['winBid'].mean()\nmean_countryCode_winbid = train_df.groupby('countryCode')['winBid'].mean()\nmean_c1_winbid = train_df.groupby('c1')['winBid'].mean()","metadata":{"id":"W0V60xDDrVfh","execution":{"iopub.status.busy":"2022-11-02T16:32:58.371372Z","iopub.execute_input":"2022-11-02T16:32:58.371848Z","iopub.status.idle":"2022-11-02T16:33:12.352415Z","shell.execute_reply.started":"2022-11-02T16:32:58.371811Z","shell.execute_reply":"2022-11-02T16:33:12.350919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def pre_process(df): \n\n  # transform timestep\n\n  df['event_Date'] = pd.to_datetime(df['eventTimestamp'], unit='ms')\n  df['event_month'] = df['event_Date'].dt.strftime('%m')\n  df['event_weekday'] = df['event_Date'].dt.dayofweek+1\n  df['event_hour'] = df['event_Date'].dt.strftime('%H')\n  df['event_hour'] = df['event_hour'].astype(int)\n\n  # transform categorial feature\n\n  df['os_type'] = df['osAndVersion'].str.split('-').str[0]\n  df['os_type'] = df['os_type'].astype('category').cat.codes\n\n  df['size_pix'] = (df['size'].str.split('x').str[0].astype(int)) * (df['size'].str.split('x').str[1].astype(int)) * 0.001\n\n  df['connectionType_WIFI'] = np.where(df['connectionType'] == 'WIFI', 1, 0)\n  df['connectionType_3G'] = np.where(df['connectionType'] == '3G', 1, 0)\n\n  df['uDT_banner'] = np.where(df['unitDisplayType'] == 'banner', 1, 0)\n  df['uDT_interstitial'] = np.where(df['unitDisplayType'] == 'interstitial', 1, 0)\n  df['uDT_rewarded'] = np.where(df['unitDisplayType'] == 'rewarded', 1, 0)\n\n  # target encoding \n\n  df['mean_device_winbid'] = df['deviceId'].map(mean_device_winbid)\n  df['median_device_winbid'] = df['deviceId'].map(median_device_winbid)\n  df['std_device_winbid'] = df['deviceId'].map(std_device_winbid)\n  df['max_device_winbid'] = df['deviceId'].map(max_device_winbid)\n  df['min_device_winbid'] = df['deviceId'].map(min_device_winbid)\n\n  df['mean_deviceId_sentPrice'] = df['deviceId'].map(mean_deviceId_sentPrice)\n  df['median_deviceId_sentPrice'] = df['deviceId'].map(median_deviceId_sentPrice)\n  df['std_deviceId_sentPrice'] = df['deviceId'].map(std_deviceId_sentPrice)\n  df['max_device_sentPrice'] = df['deviceId'].map(max_device_sentPrice)\n  df['min_device_sentPrice'] = df['deviceId'].map(min_device_sentPrice)\n\n  df['mean_bundleId_winbid'] = df['bundleId'].map(mean_bundleId_winbid)\n  df['mean_brandName_winbid'] = df['brandName'].map(mean_brandName_winbid)\n  df['mean_countryCode_winbid'] = df['countryCode'].map(mean_countryCode_winbid)\n  df['mean_c1_winbid'] = df['c1'].map(mean_c1_winbid)\n\n  return df","metadata":{"id":"qVuMPHi47qSb","execution":{"iopub.status.busy":"2022-11-02T16:33:12.354832Z","iopub.execute_input":"2022-11-02T16:33:12.355359Z","iopub.status.idle":"2022-11-02T16:33:12.373296Z","shell.execute_reply.started":"2022-11-02T16:33:12.355310Z","shell.execute_reply":"2022-11-02T16:33:12.371913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set = pre_process(train_df)\ntest_set = pre_process(test_df)","metadata":{"id":"9w4sprpNEzX9","execution":{"iopub.status.busy":"2022-11-02T16:33:12.375541Z","iopub.execute_input":"2022-11-02T16:33:12.376184Z","iopub.status.idle":"2022-11-02T16:35:36.688877Z","shell.execute_reply.started":"2022-11-02T16:33:12.376112Z","shell.execute_reply":"2022-11-02T16:35:36.687625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features = ['os_type', 'size_pix', 'connectionType_WIFI', 'connectionType_3G','uDT_banner', 'uDT_interstitial', 'uDT_rewarded', 'mean_device_winbid', 'median_device_winbid',\n            'mean_deviceId_sentPrice', 'median_deviceId_sentPrice', 'mean_bundleId_winbid', 'mean_brandName_winbid',\n            'mean_countryCode_winbid', 'mean_c1_winbid', 'event_weekday', 'event_hour', 'c2', 'c4', 'sentPrice', 'max_device_winbid',\n            'min_device_winbid', 'max_device_sentPrice', 'min_device_sentPrice', 'std_device_winbid', 'std_deviceId_sentPrice']","metadata":{"id":"MbvZ1hefHull","execution":{"iopub.status.busy":"2022-11-02T16:35:36.691388Z","iopub.execute_input":"2022-11-02T16:35:36.691854Z","iopub.status.idle":"2022-11-02T16:35:36.698308Z","shell.execute_reply.started":"2022-11-02T16:35:36.691818Z","shell.execute_reply":"2022-11-02T16:35:36.696961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fill missing values in the test set\n\nfor i in test_set[features].columns[test_set[features].isnull().any(axis=0)]:\n  test_set[i].fillna(test_set[i].mean(),inplace=True)","metadata":{"id":"RAzF-TnjFL7a","execution":{"iopub.status.busy":"2022-11-02T16:38:06.955154Z","iopub.execute_input":"2022-11-02T16:38:06.955655Z","iopub.status.idle":"2022-11-02T16:38:06.973039Z","shell.execute_reply.started":"2022-11-02T16:38:06.955613Z","shell.execute_reply":"2022-11-02T16:38:06.971644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Train LightGBM**","metadata":{"id":"HjgEfjF7ufgC"}},{"cell_type":"markdown","source":"Hyperparameter tuning","metadata":{"id":"zThq-oUZviSG"}},{"cell_type":"code","source":"import optuna\nimport optuna.integration.lightgbm as olgb\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import mean_squared_error\n\nfrom lightgbm import early_stopping\n\nimport sklearn.datasets\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.model_selection import train_test_split\n\n\ntrain_x, val_x, train_y, val_y = train_test_split(train_set[features], train_set.winBid, test_size=0.25)\ndtrain = olgb.Dataset(train_x, label=train_y)\ndval = olgb.Dataset(val_x, label=val_y)\n\nparams = {\n    \"objective\": \"regression\",\n    \"metric\": \"rmse\",\n    \"verbosity\": 0,\n    \"boosting_type\": \"gbdt\",\n}\n\nmodel = olgb.train(\n    params,\n    dtrain,\n    valid_sets=[dtrain, dval],\n    callbacks=[early_stopping(100)],\n)\n\nprediction = model.predict(val_x, num_iteration=model.best_iteration)\nrmse = np.sqrt(mean_squared_error(val_y, prediction))\n\nbest_params = model.params\nprint(\"Best params:\", best_params)\nprint(\"  rmse = {}\".format(rmse))\nprint(\"  Params: \")\nfor key, value in best_params.items():\n    print(\"    {}: {}\".format(key, value))","metadata":{"id":"hUvfjkObZukF","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"LightGBM","metadata":{"id":"0X_Iikervqcw"}},{"cell_type":"code","source":"import lightgbm as lgb\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import mean_squared_error\n\n# build the lightgbm model\nhyper_params = {\n    'task': 'train',\n    'boosting_type': 'gbdt',\n    'objective': 'regression',\n    'metric': 'rmse',\n    'learning_rate': 0.1,\n    'lambda_l1': 7.27,\n    'lambda_l2': 0.0002,\n    # 'num_leaves': 138,\n    # 'feature_fraction': 0.7,\n    'bagging_fraction': 0.6,\n    # 'bagging_freq': 1,\n    'verbosity': 0,\n    \"max_depth\": 10,\n    # 'min_child_samples': 5,\n    \"num_iterations\": 2000\n}\n\nlgbreg = lgb.LGBMRegressor(**hyper_params)\n\ntrain_x, val_x, train_y, val_y = train_test_split(train_set[features], train_set.winBid, test_size=0.3)\n\nlgbreg.fit(train_x, train_y,\n        eval_set=[(val_x, val_y)],\n        eval_metric='rmse',\n        early_stopping_rounds=50,)","metadata":{"id":"r_cmnG-3ay53","execution":{"iopub.status.busy":"2022-11-02T16:53:04.989521Z","iopub.execute_input":"2022-11-02T16:53:04.990005Z","iopub.status.idle":"2022-11-02T16:59:52.568368Z","shell.execute_reply.started":"2022-11-02T16:53:04.989926Z","shell.execute_reply":"2022-11-02T16:59:52.567092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Predict and subbmit","metadata":{"id":"vJdh_ans2Cqb"}},{"cell_type":"code","source":"prediction_tuned = lgbreg.predict(test_set[features])\n\ntest_set['winBid'] = prediction_tuned\nfinal = test_set[['deviceId', 'winBid']]","metadata":{"id":"bsC3kiXvFEdW","execution":{"iopub.status.busy":"2022-11-02T17:23:49.110919Z","iopub.execute_input":"2022-11-02T17:23:49.114886Z","iopub.status.idle":"2022-11-02T17:23:50.420837Z","shell.execute_reply.started":"2022-11-02T17:23:49.114806Z","shell.execute_reply":"2022-11-02T17:23:50.419363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Using Feature Importance we can understand what feature are more influential ","metadata":{"id":"7cPoQRkRzV7F"}},{"cell_type":"code","source":"# Feature importance\nimport warnings\nwarnings.simplefilter(action='ignore', category=FutureWarning)\n\nimportance = pd.DataFrame(sorted(zip(lgbreg.feature_importances_,train_x.columns)), columns=['Value','Feature'])\n\nplt.figure(figsize=(20, 10))\nsns.barplot(x=\"Value\", y=\"Feature\", data=importance.sort_values(by=\"Value\", ascending=False))\nplt.title(' Feature Importance')\nplt.tight_layout()\nplt.show()","metadata":{"id":"8gnSzo8HMPrW","outputId":"3172c80d-9e2c-4a02-87fd-596906ac2b90","execution":{"iopub.status.busy":"2022-11-02T17:23:53.161491Z","iopub.execute_input":"2022-11-02T17:23:53.162021Z","iopub.status.idle":"2022-11-02T17:23:53.803061Z","shell.execute_reply.started":"2022-11-02T17:23:53.161975Z","shell.execute_reply":"2022-11-02T17:23:53.801315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Final Score","metadata":{"id":"4K1oLyCU0Sc1"}},{"cell_type":"markdown","source":"Compare my results to the top winners (took the median of the top 3 solutions as shared on the competition page)","metadata":{}},{"cell_type":"code","source":"actuals = pd.read_csv('../input/test-results/test_results.csv')\n\nfinal.sort_values(by=['deviceId'], inplace=True)\nactuals.sort_values(by=['deviceId'], inplace=True)\n\nscore = np.sqrt(mean_squared_error(actuals.winBid, final.winBid))\nprint(\"Final Score is: \", score)","metadata":{"id":"Y-OWTyjjx4qE","outputId":"044d9af6-783d-4505-e59a-1efd9548fb4b","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final.to_csv(\"Submission.csv\", index = None)\nfinal","metadata":{},"execution_count":null,"outputs":[]}]}