{"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)\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.metrics import mean_squared_error\nimport lightgbm as lgb\n\nimport plotly.express as px\nimport plotly.graph_objects as go\n\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":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-08-04T14:12:46.926725Z","iopub.execute_input":"2023-08-04T14:12:46.927405Z","iopub.status.idle":"2023-08-04T14:12:50.086774Z","shell.execute_reply.started":"2023-08-04T14:12:46.927353Z","shell.execute_reply":"2023-08-04T14:12:50.085029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# define functions\n\n# Function to check if a brand should be replaced with \"Other\"\ndef replace_with_other(value, values_to_keep):\n    return value if value in values_to_keep else \"Other\"\n\n# Function to extract OS name\ndef extract_os_name(os_string):\n    if 'Android' in os_string:\n        return 'Android'\n    elif 'iOS' in os_string:\n        return 'iOS'\n    else:\n        return 'Other'\n    \n# Function to extract weekday and convert to desired format\ndef get_weekday(date_object):\n    # Get the weekday (0 for Monday, 1 for Tuesday, etc.)\n    weekday = date_object.weekday()\n    return weekday","metadata":{"execution":{"iopub.status.busy":"2023-08-04T14:12:50.088621Z","iopub.execute_input":"2023-08-04T14:12:50.089154Z","iopub.status.idle":"2023-08-04T14:12:50.098736Z","shell.execute_reply.started":"2023-08-04T14:12:50.089026Z","shell.execute_reply":"2023-08-04T14:12:50.097042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv('/kaggle/input/digital-turbine-auction-bid-price-prediction/train_data.csv', nrows=300000)\ndf_test = pd.read_csv('/kaggle/input/digital-turbine-auction-bid-price-prediction/test_data.csv', nrows=10000)\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-04T14:12:50.101266Z","iopub.execute_input":"2023-08-04T14:12:50.101923Z","iopub.status.idle":"2023-08-04T14:12:52.233921Z","shell.execute_reply.started":"2023-08-04T14:12:50.101874Z","shell.execute_reply":"2023-08-04T14:12:52.232234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA","metadata":{}},{"cell_type":"code","source":"# check for missing values\ndf_train.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2023-08-04T14:12:52.237366Z","iopub.execute_input":"2023-08-04T14:12:52.238005Z","iopub.status.idle":"2023-08-04T14:12:53.524771Z","shell.execute_reply.started":"2023-08-04T14:12:52.237969Z","shell.execute_reply":"2023-08-04T14:12:53.523568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"won_counts = df_train['has_won'].value_counts()\ntotal = len(df_train)\nwon_pct = (won_counts / total * 100)\nwon_pct = won_pct.round(2)\n\nf\"There are {won_counts[0]} ({won_pct[0]}%) lost bids, and {won_counts[1]} ({won_pct[1]}%) won bids out of {total}\"","metadata":{"execution":{"iopub.status.busy":"2023-08-04T14:12:53.526541Z","iopub.execute_input":"2023-08-04T14:12:53.527365Z","iopub.status.idle":"2023-08-04T14:12:53.545524Z","shell.execute_reply.started":"2023-08-04T14:12:53.527323Z","shell.execute_reply":"2023-08-04T14:12:53.543950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# both won and lost by DT\ndf_train['winBid'].describe().round(2)","metadata":{"execution":{"iopub.status.busy":"2023-08-04T14:12:53.547680Z","iopub.execute_input":"2023-08-04T14:12:53.548041Z","iopub.status.idle":"2023-08-04T14:12:53.583624Z","shell.execute_reply.started":"2023-08-04T14:12:53.548011Z","shell.execute_reply":"2023-08-04T14:12:53.582121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# split into won and lost by DT\nwin_bid_won = df_train[df_train['has_won'] == 1]['winBid']\nwin_bid_lost = df_train[df_train['has_won'] == 0]['winBid']\n\nwin_bid_won.describe().round(2)","metadata":{"execution":{"iopub.status.busy":"2023-08-04T14:12:53.585409Z","iopub.execute_input":"2023-08-04T14:12:53.585899Z","iopub.status.idle":"2023-08-04T14:12:53.675197Z","shell.execute_reply.started":"2023-08-04T14:12:53.585855Z","shell.execute_reply":"2023-08-04T14:12:53.674032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"win_bid_lost.describe().round(2)","metadata":{"execution":{"iopub.status.busy":"2023-08-04T14:12:53.676456Z","iopub.execute_input":"2023-08-04T14:12:53.676781Z","iopub.status.idle":"2023-08-04T14:12:53.702688Z","shell.execute_reply.started":"2023-08-04T14:12:53.676752Z","shell.execute_reply":"2023-08-04T14:12:53.701246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['bidFloorPrice'].describe().round(2)","metadata":{"execution":{"iopub.status.busy":"2023-08-04T14:12:53.704226Z","iopub.execute_input":"2023-08-04T14:12:53.705019Z","iopub.status.idle":"2023-08-04T14:12:53.727712Z","shell.execute_reply.started":"2023-08-04T14:12:53.704974Z","shell.execute_reply":"2023-08-04T14:12:53.726488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['brandName'].value_counts().head(25)","metadata":{"execution":{"iopub.status.busy":"2023-08-04T14:12:53.729384Z","iopub.execute_input":"2023-08-04T14:12:53.729754Z","iopub.status.idle":"2023-08-04T14:12:53.793400Z","shell.execute_reply.started":"2023-08-04T14:12:53.729722Z","shell.execute_reply":"2023-08-04T14:12:53.791664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['countryCode'].value_counts().head(25)","metadata":{"execution":{"iopub.status.busy":"2023-08-04T14:12:53.795594Z","iopub.execute_input":"2023-08-04T14:12:53.796013Z","iopub.status.idle":"2023-08-04T14:12:53.861149Z","shell.execute_reply.started":"2023-08-04T14:12:53.795981Z","shell.execute_reply":"2023-08-04T14:12:53.859541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['unitDisplayType'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-08-04T14:12:53.863553Z","iopub.execute_input":"2023-08-04T14:12:53.864168Z","iopub.status.idle":"2023-08-04T14:12:53.926910Z","shell.execute_reply.started":"2023-08-04T14:12:53.864119Z","shell.execute_reply":"2023-08-04T14:12:53.925377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.histogram(df_train, x=\"winBid\",log_y=True)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-04T14:12:53.933092Z","iopub.execute_input":"2023-08-04T14:12:53.933517Z","iopub.status.idle":"2023-08-04T14:12:56.465677Z","shell.execute_reply.started":"2023-08-04T14:12:53.933483Z","shell.execute_reply":"2023-08-04T14:12:56.464352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.scatter(df_train, x='winBid', y='sentPrice', color='has_won')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-04T14:12:56.467368Z","iopub.execute_input":"2023-08-04T14:12:56.467734Z","iopub.status.idle":"2023-08-04T14:12:56.763664Z","shell.execute_reply.started":"2023-08-04T14:12:56.467702Z","shell.execute_reply":"2023-08-04T14:12:56.761716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# explore median win bid by country\ndf_train.groupby('countryCode')['winBid'].median().sort_values(ascending=False).head(25)","metadata":{"execution":{"iopub.status.busy":"2023-08-04T14:12:56.765693Z","iopub.execute_input":"2023-08-04T14:12:56.766122Z","iopub.status.idle":"2023-08-04T14:12:56.858760Z","shell.execute_reply.started":"2023-08-04T14:12:56.766087Z","shell.execute_reply":"2023-08-04T14:12:56.857379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# explore median sent bid by country\ndf_train.groupby('countryCode')['sentPrice'].median().sort_values(ascending=False).head(25)","metadata":{"execution":{"iopub.status.busy":"2023-08-04T14:12:56.861100Z","iopub.execute_input":"2023-08-04T14:12:56.861941Z","iopub.status.idle":"2023-08-04T14:12:56.950973Z","shell.execute_reply.started":"2023-08-04T14:12:56.861881Z","shell.execute_reply":"2023-08-04T14:12:56.949463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# explore median win bid by unitDisplayType\ndf_train.groupby('unitDisplayType')['winBid'].median().sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2023-08-04T14:12:56.953566Z","iopub.execute_input":"2023-08-04T14:12:56.954058Z","iopub.status.idle":"2023-08-04T14:12:57.004635Z","shell.execute_reply.started":"2023-08-04T14:12:56.954011Z","shell.execute_reply":"2023-08-04T14:12:57.003238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# explore median win bid by ad size\ndf_train.groupby('size')['winBid'].median().sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2023-08-04T14:12:57.006732Z","iopub.execute_input":"2023-08-04T14:12:57.007326Z","iopub.status.idle":"2023-08-04T14:12:57.058736Z","shell.execute_reply.started":"2023-08-04T14:12:57.007255Z","shell.execute_reply":"2023-08-04T14:12:57.056920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create column with difference between sent price and win bid\ndf_train['bid_diff'] = df_train['sentPrice'] - df_train['winBid']","metadata":{"execution":{"iopub.status.busy":"2023-08-04T14:12:57.060240Z","iopub.execute_input":"2023-08-04T14:12:57.060593Z","iopub.status.idle":"2023-08-04T14:12:57.069721Z","shell.execute_reply.started":"2023-08-04T14:12:57.060563Z","shell.execute_reply":"2023-08-04T14:12:57.068094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lost_df = df_train[df_train['has_won']==0]\nlost_mean_diff = lost_df['bid_diff'].mean().round(2)\nlost_median_diff = lost_df['bid_diff'].median().round(2)\nf\"The difference between won and lost bids where DT didn't win, mean diff: {lost_mean_diff}, median diff: {lost_median_diff}\"","metadata":{"execution":{"iopub.status.busy":"2023-08-04T14:12:57.071512Z","iopub.execute_input":"2023-08-04T14:12:57.071869Z","iopub.status.idle":"2023-08-04T14:12:57.165912Z","shell.execute_reply.started":"2023-08-04T14:12:57.071837Z","shell.execute_reply":"2023-08-04T14:12:57.164445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Feature Engineering","metadata":{}},{"cell_type":"markdown","source":"We will create a new field from eventTimestamp","metadata":{}},{"cell_type":"code","source":"# create date and timestamp fields\ndf_train['datetime'] = pd.to_datetime(df_train['eventTimestamp'], unit='ms')\ndf_train['date'] = df_train['datetime'].dt.date\n\ndf_test['datetime'] = pd.to_datetime(df_test['eventTimestamp'], unit='ms')\ndf_test['date'] = df_test['datetime'].dt.date","metadata":{"execution":{"iopub.status.busy":"2023-08-04T14:12:57.167827Z","iopub.execute_input":"2023-08-04T14:12:57.168210Z","iopub.status.idle":"2023-08-04T14:12:57.370998Z","shell.execute_reply.started":"2023-08-04T14:12:57.168180Z","shell.execute_reply":"2023-08-04T14:12:57.369406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create features for month and weekday\ndf_train['month'] = df_train['datetime'].dt.month\ndf_train[\"weekday\"] = df_train[\"datetime\"].apply(get_weekday)\n\ndf_test['month'] = df_test['datetime'].dt.month\ndf_test[\"weekday\"] = df_test[\"datetime\"].apply(get_weekday)","metadata":{"execution":{"iopub.status.busy":"2023-08-04T14:12:57.372447Z","iopub.execute_input":"2023-08-04T14:12:57.372801Z","iopub.status.idle":"2023-08-04T14:12:58.683822Z","shell.execute_reply.started":"2023-08-04T14:12:57.372772Z","shell.execute_reply":"2023-08-04T14:12:58.682141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"min_date = df_train['date'].min()\nmax_date = df_train['date'].max()\n\nf\"Start date: {min_date}, End date: {max_date}\"","metadata":{"execution":{"iopub.status.busy":"2023-08-04T14:12:58.685221Z","iopub.execute_input":"2023-08-04T14:12:58.685728Z","iopub.status.idle":"2023-08-04T14:12:58.791834Z","shell.execute_reply.started":"2023-08-04T14:12:58.685690Z","shell.execute_reply":"2023-08-04T14:12:58.790356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create the scatter plot\nfig = go.Figure()\n\n# Add the 'WinPrice' dots\nfig.add_trace(go.Scatter(x=df_train['date'], y=df_train['winBid'], mode='markers'))\n\n# Update the layout of the graph\nfig.update_layout(title='Win Bids Over Time',\n                  xaxis_title='Date',\n                  yaxis_title='Win Bid')\n\n# Show the graph\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-04T14:12:58.793437Z","iopub.execute_input":"2023-08-04T14:12:58.793817Z","iopub.status.idle":"2023-08-04T14:13:05.416251Z","shell.execute_reply.started":"2023-08-04T14:12:58.793788Z","shell.execute_reply":"2023-08-04T14:13:05.413853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create the scatter plot\nfig = go.Figure()\n\n# Add the 'WinPrice' dots\nfig.add_trace(go.Scatter(x=df_train['date'], y=df_train['sentPrice'], mode='markers'))\n\n# Update the layout of the graph\nfig.update_layout(title='Sent Bids Over Time',\n                  xaxis_title='Date',\n                  yaxis_title='Sent Bid')\n\n# Show the graph\n\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-04T14:13:05.418979Z","iopub.execute_input":"2023-08-04T14:13:05.419463Z","iopub.status.idle":"2023-08-04T14:13:11.951926Z","shell.execute_reply.started":"2023-08-04T14:13:05.419424Z","shell.execute_reply":"2023-08-04T14:13:11.950626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# group brand names to keep only top 25\nbrands_to_keep = ['Generic', 'Samsung', 'Apple', 'Motorola', 'Huawei', 'Xiaomi', 'OPPO', 'LG', 'OnePlus', 'Vivo', 'Sony', 'Sharp', \n                  'Realme', 'Google', 'T-Mobile', 'TCL', 'Lenovo', 'Alcatel', 'Nokia']\n\ndf_train['brandNameGrouped'] = df_train['brandName'].apply(replace_with_other, values_to_keep=brands_to_keep)\ndf_test['brandNameGrouped'] = df_test['brandName'].apply(replace_with_other, values_to_keep=brands_to_keep)","metadata":{"execution":{"iopub.status.busy":"2023-08-04T14:13:11.953745Z","iopub.execute_input":"2023-08-04T14:13:11.954153Z","iopub.status.idle":"2023-08-04T14:13:12.256046Z","shell.execute_reply.started":"2023-08-04T14:13:11.954120Z","shell.execute_reply":"2023-08-04T14:13:12.254366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# group countries to keep only top 25\ncountries_to_keep = ['Generic', 'Samsung', 'Apple', 'Motorola', 'Huawei', 'Xiaomi', 'OPPO', 'LG', 'OnePlus', 'Vivo', 'Sony', \n                     'Sharp', 'Realme', 'Google', 'T-Mobile', 'TCL', 'Lenovo', 'Alcatel', 'Nokia', 'Kyocera', 'ONN', 'Infinix', 'ZTE', 'AT&T', 'Asus']\n\n\ndf_train['countryGrouped'] = df_train['countryCode'].apply(replace_with_other, values_to_keep=countries_to_keep)\ndf_test['countryGrouped'] = df_test['countryCode'].apply(replace_with_other, values_to_keep=countries_to_keep)","metadata":{"execution":{"iopub.status.busy":"2023-08-04T14:13:12.257791Z","iopub.execute_input":"2023-08-04T14:13:12.258178Z","iopub.status.idle":"2023-08-04T14:13:12.652660Z","shell.execute_reply.started":"2023-08-04T14:13:12.258147Z","shell.execute_reply":"2023-08-04T14:13:12.651340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# group OS versions\ndf_train['osGrouped'] = df_train['osAndVersion'].apply(extract_os_name)\ndf_test['osGrouped'] = df_test['osAndVersion'].apply(extract_os_name)","metadata":{"execution":{"iopub.status.busy":"2023-08-04T14:13:12.655114Z","iopub.execute_input":"2023-08-04T14:13:12.655569Z","iopub.status.idle":"2023-08-04T14:13:12.768584Z","shell.execute_reply.started":"2023-08-04T14:13:12.655534Z","shell.execute_reply":"2023-08-04T14:13:12.767106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# drop columns that won't be used in the model\ndf_train.drop('date', axis=1, inplace=True)\ndf_train.drop('datetime', axis=1, inplace=True)\ndf_train.drop('mediationProviderVersion', axis=1, inplace=True)\ndf_train.drop('osAndVersion', axis=1, inplace=True)\ndf_train.drop('deviceId', axis=1, inplace=True)\ndf_train.drop('bundleId', axis=1, inplace=True)\ndf_train.drop('appVersion', axis=1, inplace=True)\ndf_train.drop('brandName', axis=1, inplace=True)\ndf_train.drop('countryCode', axis=1, inplace=True)\ndf_train.drop('correctModelName', axis=1, inplace=True)\ndf_train.drop('bid_diff', axis=1, inplace=True)\ndf_train.drop('c1', axis=1, inplace=True)\n\n# drop rows with missing values\ndf_train = df_train.dropna()\n\n# drop columns that won't be used in the model\ndf_test.drop('date', axis=1, inplace=True)\ndf_test.drop('datetime', axis=1, inplace=True)\ndf_test.drop('mediationProviderVersion', axis=1, inplace=True)\ndf_test.drop('osAndVersion', axis=1, inplace=True)\ndf_test.drop('deviceId', axis=1, inplace=True)\ndf_test.drop('bundleId', axis=1, inplace=True)\ndf_test.drop('appVersion', axis=1, inplace=True)\ndf_test.drop('brandName', axis=1, inplace=True)\ndf_test.drop('countryCode', axis=1, inplace=True)\ndf_test.drop('correctModelName', axis=1, inplace=True)\ndf_test.drop('c1', axis=1, inplace=True)\n\n# drop rows with missing values\ndf_test = df_test.dropna()","metadata":{"execution":{"iopub.status.busy":"2023-08-04T14:13:12.770182Z","iopub.execute_input":"2023-08-04T14:13:12.770623Z","iopub.status.idle":"2023-08-04T14:13:14.318876Z","shell.execute_reply.started":"2023-08-04T14:13:12.770590Z","shell.execute_reply":"2023-08-04T14:13:14.317872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Linear Regression Model","metadata":{}},{"cell_type":"code","source":"# one-hot encoding of categorical data\ndf_train_converted = pd.get_dummies(df_train)\ndf_train_converted","metadata":{"execution":{"iopub.status.busy":"2023-08-04T14:13:14.320433Z","iopub.execute_input":"2023-08-04T14:13:14.321105Z","iopub.status.idle":"2023-08-04T14:13:14.706263Z","shell.execute_reply.started":"2023-08-04T14:13:14.321058Z","shell.execute_reply":"2023-08-04T14:13:14.704847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df_train_converted.drop(['has_won', 'winBid'], axis=1)\ny = df_train_converted['winBid']  \n\nscaler = StandardScaler(with_mean=False)\nX = scaler.fit_transform(X)","metadata":{"execution":{"iopub.status.busy":"2023-08-04T14:13:14.707716Z","iopub.execute_input":"2023-08-04T14:13:14.708069Z","iopub.status.idle":"2023-08-04T14:13:15.008838Z","shell.execute_reply.started":"2023-08-04T14:13:14.708039Z","shell.execute_reply":"2023-08-04T14:13:15.006969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split the data into training and validation\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-08-04T14:13:15.011133Z","iopub.execute_input":"2023-08-04T14:13:15.011524Z","iopub.status.idle":"2023-08-04T14:13:15.320947Z","shell.execute_reply.started":"2023-08-04T14:13:15.011495Z","shell.execute_reply":"2023-08-04T14:13:15.319524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train the model\nmodel = LinearRegression()\nmodel.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2023-08-04T14:13:15.322615Z","iopub.execute_input":"2023-08-04T14:13:15.323051Z","iopub.status.idle":"2023-08-04T14:13:16.360429Z","shell.execute_reply.started":"2023-08-04T14:13:15.323016Z","shell.execute_reply":"2023-08-04T14:13:16.359211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Validate the model\ny_pred = model.predict(X_val)\n\n# Calculate and print the RMSE on the validation data\nrmse = mean_squared_error(y_val, y_pred, squared=False)\nprint(f'Validation RMSE: {rmse}')","metadata":{"execution":{"iopub.status.busy":"2023-08-04T14:13:16.362034Z","iopub.execute_input":"2023-08-04T14:13:16.363056Z","iopub.status.idle":"2023-08-04T14:13:16.395548Z","shell.execute_reply.started":"2023-08-04T14:13:16.363009Z","shell.execute_reply":"2023-08-04T14:13:16.393101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# one-hot encoding of categorical data\ndf_test_converted = pd.get_dummies(df_test)\n\n# ensure the test data has the same dummy columns as the training data\ndf_train_converted.drop(['has_won', 'winBid'], axis=1, inplace=True)\ndf_test_converted = df_test_converted.reindex(columns = df_train_converted.columns, fill_value=0)\n\n# predict on test data\nX_test = df_test_converted\nX_test = scaler.transform(X_test)\ny_test_pred = model.predict(X_test)\n\n# add column with predictions to the test df\ndf_test['predicted_winBid (LR)'] = y_test_pred","metadata":{"execution":{"iopub.status.busy":"2023-08-04T14:13:16.398315Z","iopub.execute_input":"2023-08-04T14:13:16.401068Z","iopub.status.idle":"2023-08-04T14:13:16.504615Z","shell.execute_reply.started":"2023-08-04T14:13:16.401000Z","shell.execute_reply":"2023-08-04T14:13:16.502826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test[['eventTimestamp','predicted_winBid (LR)']]","metadata":{"execution":{"iopub.status.busy":"2023-08-04T14:13:16.507713Z","iopub.execute_input":"2023-08-04T14:13:16.509255Z","iopub.status.idle":"2023-08-04T14:13:16.538719Z","shell.execute_reply.started":"2023-08-04T14:13:16.509174Z","shell.execute_reply":"2023-08-04T14:13:16.537130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# LightGBM Model","metadata":{}},{"cell_type":"code","source":"# Create a LightGBM dataset for training\ntrain_data = lgb.Dataset(X_train, label=y_train)\n\n# Define parameters\nparams = {\n    'objective': 'regression',\n    'metric': 'rmse',\n    'num_leaves': 31,\n    'learning_rate': 0.05,\n    'feature_fraction': 0.9\n}\n\n# Train the model\nmodel = lgb.train(params, train_data, num_boost_round=100)\n\n# Predict on the validation data\ny_pred = model.predict(X_val)\n\n# Calculate and print the RMSE on the validation data\nrmse = mean_squared_error(y_val, y_pred, squared=False)\nprint(f'Validation RMSE: {rmse}')","metadata":{"execution":{"iopub.status.busy":"2023-08-04T14:13:16.541988Z","iopub.execute_input":"2023-08-04T14:13:16.543268Z","iopub.status.idle":"2023-08-04T14:13:18.850200Z","shell.execute_reply.started":"2023-08-04T14:13:16.543195Z","shell.execute_reply":"2023-08-04T14:13:18.848540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predict on the test data\ny_test_pred = model.predict(X_test)\n# add column with predictions to the test df\ndf_test['predicted_winBid (LightGBM)'] = y_test_pred\n\ndf_test[['eventTimestamp','predicted_winBid (LightGBM)']]","metadata":{"execution":{"iopub.status.busy":"2023-08-04T14:13:18.852237Z","iopub.execute_input":"2023-08-04T14:13:18.852716Z","iopub.status.idle":"2023-08-04T14:13:18.892258Z","shell.execute_reply.started":"2023-08-04T14:13:18.852678Z","shell.execute_reply":"2023-08-04T14:13:18.890713Z"},"trusted":true},"execution_count":null,"outputs":[]}]}