{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"3e7cdf09-cf39-46e4-a82d-568aaadb3f80","_cell_guid":"715e7a01-a42b-4c2a-ae20-0dfbf2cb4914","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\n# import lightgbm as lgb\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import mean_squared_error\nfrom sklearn import preprocessing\n# from sklearn.preprocessing import StandardScaler\nfrom sklearn.model_selection import cross_val_score, cross_val_predict\n\nfrom sklearn.linear_model import RidgeCV, LassoCV\nfrom sklearn.neighbors import KNeighborsRegressor\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.ensemble import VotingRegressor","metadata":{"_uuid":"fbc3f974-03e8-4357-af09-c64fdd0cb359","_cell_guid":"118c6e93-7940-4807-b4c5-ee122c7395c4","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/digital-turbine-auction-bid-price-prediction/train_data.csv')\ntest = pd.read_csv('/kaggle/input/digital-turbine-auction-bid-price-prediction/test_data.csv')\nsample_sub = pd.read_csv('../input/digital-turbine-auction-bid-price-prediction/sample_submission.csv')","metadata":{"_uuid":"42c5f87c-2021-4852-8063-9fed9a8411d8","_cell_guid":"a3dfc450-a6ca-4e2a-a959-4e6d0d154c0f","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('\\n Train shape', train.shape)\nprint('\\n Test shape', test.shape)\nprint('\\n Sample sub shape', sample_sub.shape)","metadata":{"_uuid":"a8aca37d-13a5-4162-9ff7-592675f2de91","_cell_guid":"bd06e986-d226-48f2-a751-837cfce37ed3","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"_uuid":"58b2e07b-8026-4aa1-9c0e-2164083712ed","_cell_guid":"f0aa4e15-feb1-4fb5-bf0c-f75c826aa446","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"_uuid":"e7937964-16d9-48c5-884b-434eaf94ef21","_cell_guid":"4c9f52c0-b948-401c-a476-0f9c593573e5","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features = train.columns.to_list()\nprint(features)\n# drop_feat = ['eventTimestamp', 'deviceId']\n# num_features = ['bidFloorPrice','sentPrice']","metadata":{"_uuid":"91ca8618-15e0-4b5b-81c5-ed17f32cbafa","_cell_guid":"6495e166-b0b7-4f34-aefb-6d2bff356040","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Feature Engineering","metadata":{"_uuid":"50f21479-8865-4f82-9bfd-3f9cb72ec237","_cell_guid":"2b2fbad7-a4d1-456e-86b7-f9fbf04a2c43","trusted":true}},{"cell_type":"code","source":"def feat_eng(df):\n    df['dif_sent_floor'] = df['sentPrice'] - df['bidFloorPrice']\n    df['div_sent_floor'] = df['sentPrice']/df['bidFloorPrice']\n    #\n    df['sum_c2_c4'] = df['c2'] + df['c4']\n    df['dif_c2_c4'] = df['c2'] - df['c4']\n    df['mult_c2_c4'] = df['c2'] * df['c4']\n    df['div_c2_c4'] = df['c2'] / df['c4']\n    #\n    df['c1_c3'] = df['c1']+'_'+df['c3']\n    return df\n\ntrain = feat_eng(train)\ntest = feat_eng(test)","metadata":{"_uuid":"e5fda16f-2e25-4524-a083-c24640e09f05","_cell_guid":"b6f21401-e094-486e-a095-673406c9026a","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Some stats","metadata":{"_uuid":"ce161ed0-3b51-4fe4-981b-cc631007e30e","_cell_guid":"29a32a31-be2a-4abb-91e3-9b3b0d1efa1c","trusted":true}},{"cell_type":"code","source":"train.describe()","metadata":{"_uuid":"40a0e485-b20f-4a36-8bc7-374e498cceba","_cell_guid":"c6bcb493-a231-4b73-9635-2af2c1648b49","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.describe()","metadata":{"_uuid":"ec8ed416-da32-4b0f-853c-f0494641e875","_cell_guid":"3b467d3b-0481-4e89-8ed7-3524504fa20f","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('\\nTrain shape', train.shape)\nprint('\\nTest shape', test.shape)","metadata":{"_uuid":"c7c85dde-e5dc-4f29-8813-a353cacd7a54","_cell_guid":"5d5c308d-53fb-4159-bbf6-86ac43ffe87b","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# dataframe info stats (https://www.kaggle.com/code/itkin16/catboost-on-gpu-baseline)\n\ndef stats(data):\n    \n    maxx = []\n    minn = []\n    for i in data.columns:\n        maxx.append(data[i].value_counts().max())\n        minn.append(data[i].value_counts().min())\n\n    return pd.DataFrame(\n        {'nunique': data.nunique(),\n         'len': len(data),\n        # 'nunique/len': data.nunique()/len(data),\n         'types':data.dtypes,\n         'Nulls' : data.isna().sum(),\n        # 'Nullpercent' : data.isna().sum()/len(data),\n         \"Value counts Max\": maxx,\n         'Value counts Min':minn \n        },\n        columns = ['nunique', 'len','types','Nulls'#,'Nullpercent', 'nunique/len'\n                   ,\"Value counts Max\",'Value counts Min']).\\\n        sort_values(by ='nunique',ascending = False)","metadata":{"_uuid":"705e6d71-29d7-4c0d-bd11-83f5bb135eb1","_cell_guid":"857e54cf-fa5c-44f1-adf8-a8732a166223","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"\\nStats train\")\nstats(train)","metadata":{"_uuid":"20384f0f-a738-41ed-9c9a-26aeec626650","_cell_guid":"6e7a21d9-6457-4bf3-9950-9dcffbc4fdf4","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"\\nStats test\")\nstats(test)","metadata":{"_uuid":"cb60f5fd-b3cf-4ec6-b1be-c7ad067899e5","_cell_guid":"550668c1-607f-4cdd-a049-1dc91d0fad08","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Target endoding - Thanks to","metadata":{"_uuid":"34a0887c-6ca0-4cca-81c3-452d5884d6d9","_cell_guid":"c7d40cb0-bc91-4c4f-aecd-2f360038ae8b","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Target encoding\n\nUpvote Lucas Morin\n\nhttps://www.kaggle.com/code/lucasmorin/eda-item-price","metadata":{"_uuid":"0f428b77-f810-4162-8121-a9edfb966e02","_cell_guid":"72eceed5-5983-4a34-af9a-ce4e9c5b59d7","trusted":true}},{"cell_type":"code","source":"def agg_features(df):\n    feature_groups = ['has_won', 'sentPrice', 'winBid']\n    # Group by device id\n    df_agg = df.groupby(['deviceId'])[feature_groups].agg(['mean', 'median', 'std', 'max', 'min', 'sum',]).reset_index()\n    # Rename columns joining suffix\n    df_agg.columns = ['_'.join(col) for col in df_agg.columns]\n    return df_agg\n\ntrain_agg = agg_features(train)\ntrain =  train.merge(train_agg, left_on = ['deviceId'], right_on = ['deviceId_'], how='left')\ntest =  test.merge(train_agg, left_on = ['deviceId'], right_on = ['deviceId_'], how='left')","metadata":{"_uuid":"29fde943-d448-4b66-b976-e6342deb55b6","_cell_guid":"13e8b99a-5551-46a6-afde-03474268bd07","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"_uuid":"2137eb47-ef7a-41d6-b03d-4c89ab3f1b6e","_cell_guid":"67bb7662-4de4-4b7e-818b-d17da2d27150","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"_uuid":"05b52b4d-d318-498b-ae21-3e02ab1375b3","_cell_guid":"2a92862e-228c-48b6-b1ca-8bab6b6fe921","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model","metadata":{"_uuid":"d638043d-94a7-4e6c-bb6f-03cd2fac0121","_cell_guid":"b292a264-1d6c-4909-a5a2-d0475fc69a4b","trusted":true}},{"cell_type":"code","source":"# Categorical features\ncat_features = [\"countryCode\", \n    \"brandName\", \n    \"appVersion\", \n    \"osAndVersion\", \n    \"c1\", \n    \"mediationProviderVersion\", \n    \"bundleId\",\n    \"size\", \n    \"c3\", \n    \"unitDisplayType\",\n    \"connectionType\",\n    \"c1_c3\",]\n\n# Encode Categorical Columns\ntrain[cat_features] = train[cat_features].astype(\"category\")\ntest[cat_features] = test[cat_features].astype(\"category\")","metadata":{"_uuid":"bf78a849-ba19-44b8-bbd8-f0339140891f","_cell_guid":"aca25bc0-7e4a-4c27-8a6e-0d0b50f8ed18","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Using categorical data for groupby operations\n# mean_cc = train.groupby('countryCode').winBid.mean()\n# train = train.merge(mean_cc, on='countryCode', how='left')\n\n# Drop ALL categorical Features!!!\ntrain = train.drop(columns=cat_features).reset_index(drop = True) \n\n# from sklearn import preprocessing\n# le = preprocessing.LabelEncoder()\n# train[cat_features] = train[cat_features].apply(le.fit_transform)\n# test[cat_features] = test[cat_features].apply(le.transform)\n\n# Target\ntrain_y = train.winBid\nmax_winBid = np.max(train['winBid'])\nmin_winBid = np.min(train['winBid'])\n# Drop columns\ntrain = train.drop(columns=[\"eventTimestamp\",\n                            \"deviceId\", \"deviceId_\",\n                            \"correctModelName\",\n                            \"winBid\",\n                            \"has_won\"]).reset_index(drop = True)\n\ntrain.fillna(0, inplace= True)","metadata":{"_uuid":"32139be3-1c8a-4fca-95e8-a2f8dd522dbe","_cell_guid":"529c058f-13d1-4c1e-a999-6ed5a8bb9746","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"_uuid":"595fcd1a-ed04-4490-a7e6-02c5209816c0","_cell_guid":"3c9a058e-e2eb-41bf-8acc-3737cb395b49","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"_uuid":"aaf43d82-843f-4db8-9696-cad7b25cba35","_cell_guid":"24fb4fbc-fb26-4a3f-8ffc-2ab5ce25bc82","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_test, y_train, y_test = train_test_split(train, train_y,test_size = .3, random_state=42)","metadata":{"_uuid":"add06428-44d4-42ce-8966-52ad583669b6","_cell_guid":"afbb4a32-d717-4bab-9382-caf9961f8758","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Ensemble\n\n**Averages** the individual predictions to form a final prediction.","metadata":{"_uuid":"35547d3e-a32c-42a8-9b76-728a18d361ec","_cell_guid":"ddfbbf57-0368-41ef-92e3-3288fb1a56c2","trusted":true}},{"cell_type":"code","source":"# Training classifiers\nreg1 = RidgeCV()\nreg2 = LassoCV()\nreg3 = KNeighborsRegressor(n_neighbors=5)\nreg4 = LinearRegression()\n#\nreg1.fit(X_train, y_train)\ny_pred1 = reg1.predict(X_test)\nprint('The rmse of prediction is:','Ridge' ,np.sqrt(mean_squared_error(y_pred1, y_test)))\n\nreg2.fit(X_train, y_train)\ny_pred2 = reg2.predict(X_test)\nprint('The rmse of prediction is:','Lasso' ,np.sqrt(mean_squared_error(y_pred2, y_test)))\n\n# reg3.fit(X_train, y_train)\n# y_pred3 = reg3.predict(X_test)\n# print('The rmse of prediction is:','KNN' ,np.sqrt(mean_squared_error(y_pred3, y_test)))\n\nreg4.fit(X_train, y_train)\ny_pred4 = reg4.predict(X_test)\nprint('The rmse of prediction is:','LR', np.sqrt(mean_squared_error(y_pred4, y_test)))\n#","metadata":{"_uuid":"22694237-da03-44a2-8673-5062bf54cc70","_cell_guid":"505b61fd-f26b-4d0c-800e-7d75d81277f9","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Number of models\nM = 3\ny_pred = (y_pred1 + y_pred2 + y_pred4)/M # y_pred3\nprint('The rmse of prediction is:','ALL', np.sqrt(mean_squared_error(y_pred, y_test)))","metadata":{"_uuid":"aaa848f5-174a-4b1b-9be7-c5469af0a839","_cell_guid":"cda26a21-c0ab-4947-8b85-4da79935f09e","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Submission","metadata":{"_uuid":"10e3c798-b714-41a8-830c-e9c3052e85e8","_cell_guid":"7d64c993-12fb-4ae3-9b44-4d0eb58aafc4","trusted":true}},{"cell_type":"code","source":"sub_test = pd.DataFrame({'deviceId':test['deviceId'], 'winBid':0})\n\n# Drop ALL categorical Features!!!\ntest = test.drop(columns=cat_features).reset_index(drop = True) \n                         \ntest = test.drop(columns=[\"eventTimestamp\",\n                        \"deviceId\",\n                        \"deviceId_\",\n                        \"correctModelName\"]).reset_index(drop = True)\ntest.fillna(0, inplace= True)","metadata":{"_uuid":"759c8b43-d1b8-4cf4-a65d-2edb095caf57","_cell_guid":"1adcebae-175b-45e9-85d6-57ea5099e0c0","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predict\ny_pred = reg1.predict(test)\ny_pred += reg2.predict(test)\n# y_pred += reg3.predict(test)\ny_pred += reg4.predict(test)\ny_pred = y_pred/M\n#\nsub_test['winBid'] = y_pred\nsub_test.head()","metadata":{"_uuid":"4523adba-180a-4187-b4a7-b34cb505c9c3","_cell_guid":"c060548b-f44c-4f1c-9156-e6ad2b1b9811","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_test.to_csv('submission.csv', index = False)","metadata":{"_uuid":"745a220f-bdcd-4e27-9847-dabc28f5840e","_cell_guid":"a98a73da-6199-47d3-9eec-d6be89b6da35","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_test['winBid'] = np.clip(y_pred, min_winBid, max_winBid)\nsub_test.head()\nsub_test.to_csv('submission_clip.csv', index = False)","metadata":{"_uuid":"da914911-a853-4382-8e1d-6fc340b1f3a5","_cell_guid":"4a5ceaad-41f5-4b22-98fd-4b403306e828","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]}]}