{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":82253,"databundleVersionId":8965849,"sourceType":"competition"}],"dockerImageVersionId":30732,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-06-29T05:38:09.258982Z","iopub.execute_input":"2024-06-29T05:38:09.259677Z","iopub.status.idle":"2024-06-29T05:38:10.308702Z","shell.execute_reply.started":"2024-06-29T05:38:09.259644Z","shell.execute_reply":"2024-06-29T05:38:10.307615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\n# Replace 'filename.csv' with the actual CSV file name in the dataset\ntrain = '/kaggle/input/f1nalyze-datathon-ieeecsmuj/train.csv'\ndf = pd.read_csv(train)\n\n# Display the first few rows of the DataFrame\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2024-06-29T05:38:13.221313Z","iopub.execute_input":"2024-06-29T05:38:13.221813Z","iopub.status.idle":"2024-06-29T05:38:45.885094Z","shell.execute_reply.started":"2024-06-29T05:38:13.221780Z","shell.execute_reply":"2024-06-29T05:38:45.884243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testdir = '/kaggle/input/f1nalyze-datathon-ieeecsmuj/test.csv'\ntest = pd.read_csv(testdir)\n\n# Display the first few rows of the DataFrame\ntest.head()\n","metadata":{"execution":{"iopub.status.busy":"2024-06-29T05:38:45.887248Z","iopub.execute_input":"2024-06-29T05:38:45.887621Z","iopub.status.idle":"2024-06-29T05:38:49.950583Z","shell.execute_reply.started":"2024-06-29T05:38:45.887588Z","shell.execute_reply":"2024-06-29T05:38:49.949683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation = '/kaggle/input/f1nalyze-datathon-ieeecsmuj/validation.csv'\nval = pd.read_csv(validation)\n\n# Display the first few rows of the DataFrame\nval.head()","metadata":{"execution":{"iopub.status.busy":"2024-06-29T05:38:49.952019Z","iopub.execute_input":"2024-06-29T05:38:49.952659Z","iopub.status.idle":"2024-06-29T05:38:54.042702Z","shell.execute_reply.started":"2024-06-29T05:38:49.952626Z","shell.execute_reply":"2024-06-29T05:38:54.041771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"diff = [item for item in list(df.columns) if item not in list(test.columns)]\ndiff","metadata":{"execution":{"iopub.status.busy":"2024-06-28T09:55:16.792448Z","iopub.execute_input":"2024-06-28T09:55:16.792837Z","iopub.status.idle":"2024-06-28T09:55:16.800271Z","shell.execute_reply.started":"2024-06-28T09:55:16.792806Z","shell.execute_reply":"2024-06-28T09:55:16.799221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['positionText_y']","metadata":{"execution":{"iopub.status.busy":"2024-06-28T09:57:41.049480Z","iopub.execute_input":"2024-06-28T09:57:41.049929Z","iopub.status.idle":"2024-06-28T09:57:41.059802Z","shell.execute_reply.started":"2024-06-28T09:57:41.049902Z","shell.execute_reply":"2024-06-28T09:57:41.058594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[df['positionText_y'] == 'B']","metadata":{"execution":{"iopub.status.busy":"2024-06-28T10:00:53.017591Z","iopub.execute_input":"2024-06-28T10:00:53.018005Z","iopub.status.idle":"2024-06-28T10:00:53.538263Z","shell.execute_reply.started":"2024-06-28T10:00:53.017973Z","shell.execute_reply":"2024-06-28T10:00:53.536866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2024-06-28T10:02:46.356571Z","iopub.execute_input":"2024-06-28T10:02:46.356978Z","iopub.status.idle":"2024-06-28T10:02:57.269868Z","shell.execute_reply.started":"2024-06-28T10:02:46.356942Z","shell.execute_reply":"2024-06-28T10:02:57.268857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['positionText_y']=df['positionText_y'].astype(str)\n\ndf[df['positionText_y'] != df['position'].astype(str)].positionText_y.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-06-28T10:06:41.574866Z","iopub.execute_input":"2024-06-28T10:06:41.575289Z","iopub.status.idle":"2024-06-28T10:06:43.811900Z","shell.execute_reply.started":"2024-06-28T10:06:41.575256Z","shell.execute_reply":"2024-06-28T10:06:43.810780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def clean(df):\n    if 'positionText_y' in df.columns:\n        df.drop('positionText_y', axis=1, inplace=True)\n    for col in idcols:\n        if col in df.columns:\n            df.drop(col, axis=1, inplace=True)\n    if 'time_x' in df.columns:\n        df.drop('time_x', axis=1, inplace=True)\n        \n    for col in df.columns:\n        df[col] = df[col].apply(lambda x: x if str(x).strip() != '\\\\N' else 0)\n        \n        \n        \n    return df.copy()","metadata":{"execution":{"iopub.status.busy":"2024-06-28T10:44:50.533057Z","iopub.execute_input":"2024-06-28T10:44:50.533437Z","iopub.status.idle":"2024-06-28T10:44:50.541910Z","shell.execute_reply.started":"2024-06-28T10:44:50.533409Z","shell.execute_reply":"2024-06-28T10:44:50.540453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2024-06-28T10:48:03.079512Z","iopub.execute_input":"2024-06-28T10:48:03.079811Z","iopub.status.idle":"2024-06-28T10:48:03.134221Z","shell.execute_reply.started":"2024-06-28T10:48:03.079785Z","shell.execute_reply":"2024-06-28T10:48:03.132986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2 = clean(df.copy())\ndf2","metadata":{"execution":{"iopub.status.busy":"2024-06-28T10:48:12.973974Z","iopub.execute_input":"2024-06-28T10:48:12.974411Z","iopub.status.idle":"2024-06-28T10:50:02.080056Z","shell.execute_reply.started":"2024-06-28T10:48:12.974380Z","shell.execute_reply":"2024-06-28T10:50:02.078828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df3.head(2)","metadata":{"execution":{"iopub.status.busy":"2024-06-28T10:39:15.488145Z","iopub.execute_input":"2024-06-28T10:39:15.488505Z","iopub.status.idle":"2024-06-28T10:39:15.526522Z","shell.execute_reply.started":"2024-06-28T10:39:15.488479Z","shell.execute_reply":"2024-06-28T10:39:15.525287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idcols = []\n\nfor col in df.columns:\n    if 'Id' in col:\n        idcols.append(col)\n\nidcols","metadata":{"execution":{"iopub.status.busy":"2024-06-28T13:29:27.763830Z","iopub.execute_input":"2024-06-28T13:29:27.764339Z","iopub.status.idle":"2024-06-28T13:29:27.774434Z","shell.execute_reply.started":"2024-06-28T13:29:27.764301Z","shell.execute_reply":"2024-06-28T13:29:27.772662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2 = clean(df)\npd.set_option('display.max_columns', None)\ndf2.head(2)","metadata":{"execution":{"iopub.status.busy":"2024-06-28T10:12:19.571941Z","iopub.execute_input":"2024-06-28T10:12:19.572732Z","iopub.status.idle":"2024-06-28T10:12:19.607881Z","shell.execute_reply.started":"2024-06-28T10:12:19.572694Z","shell.execute_reply":"2024-06-28T10:12:19.606847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.fastestLap = df2.fastestLap.apply(lambda x: int(x) if x != '\\\\N' else 0)\ndf2.fastestLap.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-06-28T10:14:25.194702Z","iopub.execute_input":"2024-06-28T10:14:25.195192Z","iopub.status.idle":"2024-06-28T10:14:27.421442Z","shell.execute_reply.started":"2024-06-28T10:14:25.195157Z","shell.execute_reply":"2024-06-28T10:14:27.419982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in df2.columns:\n    df2[col] = df2[col].apply(lambda x: x if x != '\\\\N' else 0)\ndf2.head(2)","metadata":{"execution":{"iopub.status.busy":"2024-06-28T10:32:40.501445Z","iopub.execute_input":"2024-06-28T10:32:40.501918Z","iopub.status.idle":"2024-06-28T10:33:48.907943Z","shell.execute_reply.started":"2024-06-28T10:32:40.501882Z","shell.execute_reply":"2024-06-28T10:33:48.906649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2 = clean(df)\ndf2.head(2)","metadata":{"execution":{"iopub.status.busy":"2024-06-28T10:39:28.069277Z","iopub.execute_input":"2024-06-28T10:39:28.070227Z","iopub.status.idle":"2024-06-28T10:39:28.107721Z","shell.execute_reply.started":"2024-06-28T10:39:28.070189Z","shell.execute_reply":"2024-06-28T10:39:28.106306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2024-06-28T10:39:40.625328Z","iopub.execute_input":"2024-06-28T10:39:40.626584Z","iopub.status.idle":"2024-06-28T10:39:40.680594Z","shell.execute_reply.started":"2024-06-28T10:39:40.626545Z","shell.execute_reply":"2024-06-28T10:39:40.679166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2","metadata":{"execution":{"iopub.status.busy":"2024-06-28T10:34:53.828589Z","iopub.execute_input":"2024-06-28T10:34:53.829057Z","iopub.status.idle":"2024-06-28T10:34:53.880394Z","shell.execute_reply.started":"2024-06-28T10:34:53.829021Z","shell.execute_reply":"2024-06-28T10:34:53.879145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head(2)","metadata":{"execution":{"iopub.status.busy":"2024-06-28T10:30:22.369285Z","iopub.execute_input":"2024-06-28T10:30:22.369665Z","iopub.status.idle":"2024-06-28T10:30:22.404139Z","shell.execute_reply.started":"2024-06-28T10:30:22.369636Z","shell.execute_reply":"2024-06-28T10:30:22.402710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2['time_y'] = df2['time_y'].replace(0, '00:00:00')","metadata":{"execution":{"iopub.status.busy":"2024-06-28T10:35:22.359800Z","iopub.execute_input":"2024-06-28T10:35:22.360288Z","iopub.status.idle":"2024-06-28T10:35:22.894782Z","shell.execute_reply.started":"2024-06-28T10:35:22.360255Z","shell.execute_reply":"2024-06-28T10:35:22.893659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2","metadata":{"execution":{"iopub.status.busy":"2024-06-28T13:27:34.745665Z","iopub.execute_input":"2024-06-28T13:27:34.746105Z","iopub.status.idle":"2024-06-28T13:27:34.775597Z","shell.execute_reply.started":"2024-06-28T13:27:34.746070Z","shell.execute_reply":"2024-06-28T13:27:34.773806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2['time_y'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-06-28T10:35:25.315603Z","iopub.execute_input":"2024-06-28T10:35:25.316506Z","iopub.status.idle":"2024-06-28T10:35:25.737455Z","shell.execute_reply.started":"2024-06-28T10:35:25.316466Z","shell.execute_reply":"2024-06-28T10:35:25.736406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2['time_y'] = pd.to_timedelta(df2['time_y'])\ndf2.time_y = df2.time_y.dt.total_seconds()","metadata":{"execution":{"iopub.status.busy":"2024-06-28T10:51:17.533933Z","iopub.execute_input":"2024-06-28T10:51:17.534443Z","iopub.status.idle":"2024-06-28T10:51:28.312316Z","shell.execute_reply.started":"2024-06-28T10:51:17.534405Z","shell.execute_reply":"2024-06-28T10:51:28.310926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.time_y","metadata":{"execution":{"iopub.status.busy":"2024-06-28T10:51:28.314237Z","iopub.execute_input":"2024-06-28T10:51:28.315175Z","iopub.status.idle":"2024-06-28T10:51:28.325410Z","shell.execute_reply.started":"2024-06-28T10:51:28.315142Z","shell.execute_reply":"2024-06-28T10:51:28.324157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.sample(2)","metadata":{"execution":{"iopub.status.busy":"2024-06-28T10:53:40.893045Z","iopub.execute_input":"2024-06-28T10:53:40.894086Z","iopub.status.idle":"2024-06-28T10:53:41.030844Z","shell.execute_reply.started":"2024-06-28T10:53:40.894052Z","shell.execute_reply":"2024-06-28T10:53:41.029535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.quali_time.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-06-28T10:54:22.228430Z","iopub.execute_input":"2024-06-28T10:54:22.228942Z","iopub.status.idle":"2024-06-28T10:54:22.265333Z","shell.execute_reply.started":"2024-06-28T10:54:22.228903Z","shell.execute_reply":"2024-06-28T10:54:22.263950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"colz=[]\nfor col in df2.columns:\n    if df2[col].nunique() == 1:\n        colz.append(col)\n        \ncolz","metadata":{"execution":{"iopub.status.busy":"2024-06-28T10:56:22.197051Z","iopub.execute_input":"2024-06-28T10:56:22.198101Z","iopub.status.idle":"2024-06-28T10:56:27.709063Z","shell.execute_reply.started":"2024-06-28T10:56:22.198039Z","shell.execute_reply":"2024-06-28T10:56:27.707765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.sample(2)","metadata":{"execution":{"iopub.status.busy":"2024-06-28T10:57:05.331300Z","iopub.execute_input":"2024-06-28T10:57:05.331775Z","iopub.status.idle":"2024-06-28T10:57:05.525569Z","shell.execute_reply.started":"2024-06-28T10:57:05.331739Z","shell.execute_reply":"2024-06-28T10:57:05.524269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.status.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-06-28T11:01:47.457486Z","iopub.execute_input":"2024-06-28T11:01:47.458343Z","iopub.status.idle":"2024-06-28T11:01:47.977344Z","shell.execute_reply.started":"2024-06-28T11:01:47.458309Z","shell.execute_reply":"2024-06-28T11:01:47.975877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"colstr2 = ['grand_prix', 'date', 'url_x', 'fp1_date', 'fp1_time', 'fp2_date', 'fp2_time', 'fp3_date', 'fp3_time', 'quali_date', 'quali_time', 'sprint_date', 'sprint_time', 'forename', 'surname', 'dob', 'url_y', 'company', 'url', 'result_driver_standing']\ndf2.drop(colstr2, axis=1, inplace=True)\ndf2","metadata":{"execution":{"iopub.status.busy":"2024-06-28T11:03:04.278047Z","iopub.execute_input":"2024-06-28T11:03:04.279004Z","iopub.status.idle":"2024-06-28T11:03:04.887089Z","shell.execute_reply.started":"2024-06-28T11:03:04.278963Z","shell.execute_reply":"2024-06-28T11:03:04.885964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.drop('driver_code', axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-06-28T11:04:28.546675Z","iopub.execute_input":"2024-06-28T11:04:28.547045Z","iopub.status.idle":"2024-06-28T11:04:29.226448Z","shell.execute_reply.started":"2024-06-28T11:04:28.547018Z","shell.execute_reply":"2024-06-28T11:04:29.225295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def clean(df):\n    if 'positionText_y' in df.columns:\n        df.drop('positionText_y', axis=1, inplace=True)\n        \n    idcols = ['resultId',\n             'racerId',\n             'driverId',\n             'constructorId',\n             'statusId',\n             'circuitId',\n             'driverStandingsId',\n             'raceId_y']\n    for col in idcols:\n        if col in df.columns:\n            df.drop(col, axis=1, inplace=True)\n    if 'time_x' in df.columns:\n        df.drop('time_x', axis=1, inplace=True)\n        \n    for col in df.columns:\n        df[col] = df[col].apply(lambda x: x if str(x).strip() != '\\\\N' else 0)\n    \n    df['time_y'] = df['time_y'].replace(0, '00:00:00')\n    \n    df['time_y'] = pd.to_timedelta(df['time_y'])\n    df.time_y = df.time_y.dt.total_seconds()\n    \n    colstr2 = ['driver_code', 'grand_prix', 'date', 'url_x', 'fp1_date', 'fp1_time', 'fp2_date', 'fp2_time', 'fp3_date', 'fp3_time', 'quali_date', 'quali_time', 'sprint_date', 'sprint_time', 'forename', 'surname', 'dob', 'url_y', 'company', 'url', 'result_driver_standing']\n    for col in colstr2:\n        if col in df.columns:\n            df.drop(col, axis=1, inplace=True)\n    \n    df.driver_num = df.driver_num.astype(int)\n    df.max_speed = df.max_speed.astype(float)\n    def time_to_seconds(time_str):\n        if '.' in time_str:\n            minutes, seconds = time_str.split(':')\n            seconds, milliseconds = seconds.split('.')\n            total_seconds = int(minutes) * 60 + int(seconds) + float('0.' + milliseconds)\n        else:\n            total_seconds = 0\n        return total_seconds\n    df.fastestLapTime = df.fastestLapTime.replace(0, '00:00.000')\n    df.fastestLapTime = df.fastestLapTime.apply(time_to_seconds)\n    \n    df['rank'] = df['rank'].astype(int)\n    \n    df.fastestLap = df.fastestLap.astype(int)\n    \n    df.timetaken_in_millisec = df.timetaken_in_millisec.astype(int)\n    \n    df.position_x = df.position_x.astype(int)\n    \n    df.number = df.number.astype(int)\n    \n    df.drop('positionText_x', axis=1, inplace=True)\n    df.drop(['position_x', 'nationality_y', 'driverRef'], axis=1, inplace=True)\n        \n    return df.copy()","metadata":{"execution":{"iopub.status.busy":"2024-06-29T05:39:39.508074Z","iopub.execute_input":"2024-06-29T05:39:39.508698Z","iopub.status.idle":"2024-06-29T05:39:39.523158Z","shell.execute_reply.started":"2024-06-29T05:39:39.508663Z","shell.execute_reply":"2024-06-29T05:39:39.522250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2","metadata":{"execution":{"iopub.status.busy":"2024-06-28T11:04:32.592382Z","iopub.execute_input":"2024-06-28T11:04:32.592783Z","iopub.status.idle":"2024-06-28T11:04:32.624484Z","shell.execute_reply.started":"2024-06-28T11:04:32.592753Z","shell.execute_reply":"2024-06-28T11:04:32.623089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in df2.columns:\n    if df2.dtypes[col] == 'object':\n        print(col)","metadata":{"execution":{"iopub.status.busy":"2024-06-28T11:47:09.444971Z","iopub.execute_input":"2024-06-28T11:47:09.445413Z","iopub.status.idle":"2024-06-28T11:47:09.455477Z","shell.execute_reply.started":"2024-06-28T11:47:09.445382Z","shell.execute_reply":"2024-06-28T11:47:09.454114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.driver_num = df2.driver_num.astype(int)","metadata":{"execution":{"iopub.status.busy":"2024-06-28T11:47:03.609840Z","iopub.execute_input":"2024-06-28T11:47:03.610788Z","iopub.status.idle":"2024-06-28T11:47:03.841869Z","shell.execute_reply.started":"2024-06-28T11:47:03.610753Z","shell.execute_reply":"2024-06-28T11:47:03.840783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.max_speed = df2.max_speed.astype(float)","metadata":{"execution":{"iopub.status.busy":"2024-06-28T11:39:46.178714Z","iopub.execute_input":"2024-06-28T11:39:46.179128Z","iopub.status.idle":"2024-06-28T11:39:46.558440Z","shell.execute_reply.started":"2024-06-28T11:39:46.179068Z","shell.execute_reply":"2024-06-28T11:39:46.557217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def time_to_seconds(time_str):\n    if '.' in time_str:\n        minutes, seconds = time_str.split(':')\n        seconds, milliseconds = seconds.split('.')\n        total_seconds = int(minutes) * 60 + int(seconds) + float('0.' + milliseconds)\n    else:\n        total_seconds = 0\n    return total_seconds\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df2['fastestLapTime'] = pd.to_timedelta(df2['fastestLapTime'])\ndf2.fastestLapTime = df2.fastestLapTime.apply(time_to_seconds)","metadata":{"execution":{"iopub.status.busy":"2024-06-28T11:39:01.515030Z","iopub.execute_input":"2024-06-28T11:39:01.516343Z","iopub.status.idle":"2024-06-28T11:39:06.353273Z","shell.execute_reply.started":"2024-06-28T11:39:01.516291Z","shell.execute_reply":"2024-06-28T11:39:06.352155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2['rank'] = df2['rank'].astype(int)","metadata":{"execution":{"iopub.status.busy":"2024-06-28T11:30:54.674986Z","iopub.execute_input":"2024-06-28T11:30:54.675445Z","iopub.status.idle":"2024-06-28T11:30:54.910009Z","shell.execute_reply.started":"2024-06-28T11:30:54.675413Z","shell.execute_reply":"2024-06-28T11:30:54.908946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.fastestLap = df2.fastestLap.astype(int)","metadata":{"execution":{"iopub.status.busy":"2024-06-28T11:29:51.556563Z","iopub.execute_input":"2024-06-28T11:29:51.557691Z","iopub.status.idle":"2024-06-28T11:29:51.800258Z","shell.execute_reply.started":"2024-06-28T11:29:51.557650Z","shell.execute_reply":"2024-06-28T11:29:51.798978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.timetaken_in_millisec = df2.timetaken_in_millisec.astype(int)","metadata":{"execution":{"iopub.status.busy":"2024-06-28T11:29:13.070631Z","iopub.execute_input":"2024-06-28T11:29:13.071037Z","iopub.status.idle":"2024-06-28T11:29:13.344030Z","shell.execute_reply.started":"2024-06-28T11:29:13.071007Z","shell.execute_reply":"2024-06-28T11:29:13.341948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.position_x = df2.position_x.astype(int)","metadata":{"execution":{"iopub.status.busy":"2024-06-28T11:27:46.888838Z","iopub.execute_input":"2024-06-28T11:27:46.889271Z","iopub.status.idle":"2024-06-28T11:27:47.072605Z","shell.execute_reply.started":"2024-06-28T11:27:46.889241Z","shell.execute_reply":"2024-06-28T11:27:47.071171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.number = df2.number.astype(int)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.drop('positionText_x', axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-06-28T11:27:01.638156Z","iopub.execute_input":"2024-06-28T11:27:01.638616Z","iopub.status.idle":"2024-06-28T11:27:02.202152Z","shell.execute_reply.started":"2024-06-28T11:27:01.638583Z","shell.execute_reply":"2024-06-28T11:27:02.200821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfx = clean(df.copy())\nvaldf = clean(val.copy())\ntestdf = clean(test.copy())\n\ndfx.columns, valdf.columns","metadata":{"execution":{"iopub.status.busy":"2024-06-29T06:04:18.561226Z","iopub.execute_input":"2024-06-29T06:04:18.561595Z","iopub.status.idle":"2024-06-29T06:06:53.562798Z","shell.execute_reply.started":"2024-06-29T06:04:18.561563Z","shell.execute_reply":"2024-06-29T06:06:53.561852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# valdf.driverRef = valdf.driverRef.replace('kevin_magnussen', 'others')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nle = LabelEncoder()\n\nfulldf = pd.concat([dfx, valdf], ignore_index=True)\nfulldf = fulldf.drop(['status', 'nationality', 'constructorRef'], axis=1)\ntestdf = testdf.drop(['status', 'nationality', 'constructorRef'], axis=1)\n\nfor col in fulldf.columns:\n    if fulldf.dtypes[col] == 'object':\n        le.fit(fulldf[col])\n        fulldf[col] = le.transform(fulldf[col])\n        testdf[col] = le.transform(testdf[col])","metadata":{"execution":{"iopub.status.busy":"2024-06-29T06:06:53.564325Z","iopub.execute_input":"2024-06-29T06:06:53.564636Z","iopub.status.idle":"2024-06-29T06:06:54.042905Z","shell.execute_reply.started":"2024-06-29T06:06:53.564612Z","shell.execute_reply":"2024-06-29T06:06:54.042130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testdf = clean(test.copy())","metadata":{"execution":{"iopub.status.busy":"2024-06-29T06:27:06.579620Z","iopub.execute_input":"2024-06-29T06:27:06.580239Z","iopub.status.idle":"2024-06-29T06:27:20.487507Z","shell.execute_reply.started":"2024-06-29T06:27:06.580208Z","shell.execute_reply":"2024-06-29T06:27:20.486694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nfrom sklearn.pipeline import Pipeline\nle = LabelEncoder()\n\n\n\ndef safe_transform(le, x):\n    try:\n        return le.transform(x)\n    except ValueError:\n        print(\"hehe\")\n        return -1\n\n\nfor col in dfx.columns:\n    if dfx.dtypes[col] == 'object':\n        le.fit(dfx[col])\n        dfx[col] = safe_transform(le, dfx[col])\n        valdf[col] = safe_transform(le, valdf[col])\n        testdf[col] = safe_transform(le, testdf[col])\ndfx","metadata":{"execution":{"iopub.status.busy":"2024-06-29T06:27:25.213653Z","iopub.execute_input":"2024-06-29T06:27:25.214130Z","iopub.status.idle":"2024-06-29T06:27:27.433151Z","shell.execute_reply.started":"2024-06-29T06:27:25.214091Z","shell.execute_reply":"2024-06-29T06:27:27.432164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import MinMaxScaler, StandardScaler\nfrom sklearn.model_selection import train_test_split\n\nss = StandardScaler()\n\nX = dfx.drop(['position', 'status', 'constructorRef', 'nationality', 'round', 'laps'], axis=1)\ny = dfx['position']\nX = ss.fit_transform(X)\n\nvalX = valdf.drop(['position', 'status', 'constructorRef', 'nationality', 'round', 'laps'], axis=1)\nvaly = valdf['position']\nvalX = ss.transform(valX)","metadata":{"execution":{"iopub.status.busy":"2024-06-29T06:29:25.957862Z","iopub.execute_input":"2024-06-29T06:29:25.958864Z","iopub.status.idle":"2024-06-29T06:29:26.786724Z","shell.execute_reply.started":"2024-06-29T06:29:25.958825Z","shell.execute_reply":"2024-06-29T06:29:26.785802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valdf","metadata":{"execution":{"iopub.status.busy":"2024-06-29T06:29:37.385180Z","iopub.execute_input":"2024-06-29T06:29:37.385659Z","iopub.status.idle":"2024-06-29T06:29:37.410541Z","shell.execute_reply.started":"2024-06-29T06:29:37.385626Z","shell.execute_reply":"2024-06-29T06:29:37.409666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = fulldf.drop(['position', 'round', 'laps'], axis=1)\ny = fulldf['position']\nX = ss.fit_transform(X)\nX.shape, y.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-29T06:31:18.541489Z","iopub.execute_input":"2024-06-29T06:31:18.542153Z","iopub.status.idle":"2024-06-29T06:31:19.476328Z","shell.execute_reply.started":"2024-06-29T06:31:18.542120Z","shell.execute_reply":"2024-06-29T06:31:19.475413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fulldf","metadata":{"execution":{"iopub.status.busy":"2024-06-29T06:30:54.740483Z","iopub.execute_input":"2024-06-29T06:30:54.741287Z","iopub.status.idle":"2024-06-29T06:30:54.765597Z","shell.execute_reply.started":"2024-06-29T06:30:54.741253Z","shell.execute_reply":"2024-06-29T06:30:54.764670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valX = valdf.drop(['position', 'status', 'constructorRef', 'nationality'], axis=1)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor, AdaBoostRegressor, HistGradientBoostingRegressor\nfrom sklearn.metrics import mean_squared_error\nimport numpy as np\n\nhgb = HistGradientBoostingRegressor()\nhgb.fit(X, y)\ny_pred = hgb.predict(valX)\nnp.sqrt(mean_squared_error(valy, y_pred))","metadata":{"execution":{"iopub.status.busy":"2024-06-29T06:30:14.104976Z","iopub.execute_input":"2024-06-29T06:30:14.105365Z","iopub.status.idle":"2024-06-29T06:30:28.402523Z","shell.execute_reply.started":"2024-06-29T06:30:14.105333Z","shell.execute_reply":"2024-06-29T06:30:28.401690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valX.shape, valy.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-29T05:17:02.571440Z","iopub.execute_input":"2024-06-29T05:17:02.571798Z","iopub.status.idle":"2024-06-29T05:17:02.579195Z","shell.execute_reply.started":"2024-06-29T05:17:02.571771Z","shell.execute_reply":"2024-06-29T05:17:02.578015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from lightgbm import LGBMRegressor\n\n\nlgbm = LGBMRegressor()\nlgbm.fit(X, y)\ny_pred = lgbm.predict(valX)\nnp.sqrt(mean_squared_error(valy, y_pred))","metadata":{"execution":{"iopub.status.busy":"2024-06-29T06:30:36.866563Z","iopub.execute_input":"2024-06-29T06:30:36.867329Z","iopub.status.idle":"2024-06-29T06:30:53.890066Z","shell.execute_reply.started":"2024-06-29T06:30:36.867298Z","shell.execute_reply":"2024-06-29T06:30:53.889077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from xgboost import XGBRegressor\nxgb = XGBRegressor(n_estimators=1000)\nxgb.fit(X, y)\ny_pred = xgb.predict(valX)\nnp.sqrt(mean_squared_error(valy, y_pred))","metadata":{"execution":{"iopub.status.busy":"2024-06-28T12:32:52.630650Z","iopub.execute_input":"2024-06-28T12:32:52.631561Z","iopub.status.idle":"2024-06-28T12:41:32.435936Z","shell.execute_reply.started":"2024-06-28T12:32:52.631515Z","shell.execute_reply":"2024-06-28T12:41:32.434535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import VotingRegressor\n\nhgb2 = HistGradientBoostingRegressor()\nlgbm2 = LGBMRegressor()\n\nvoting = VotingRegressor([('hgb', hgb2), ('lgbm', lgbm2)])\nvoting.fit(X, y)\ny_pred = voting.predict(valX)\nnp.sqrt(mean_squared_error(valy, y_pred))","metadata":{"execution":{"iopub.status.busy":"2024-06-29T06:31:30.828921Z","iopub.execute_input":"2024-06-29T06:31:30.829279Z","iopub.status.idle":"2024-06-29T06:32:07.258634Z","shell.execute_reply.started":"2024-06-29T06:31:30.829251Z","shell.execute_reply":"2024-06-29T06:32:07.257759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testdf = clean(test.copy())","metadata":{"execution":{"iopub.status.busy":"2024-06-29T05:06:44.619747Z","iopub.execute_input":"2024-06-29T05:06:44.620140Z","iopub.status.idle":"2024-06-29T05:06:56.733551Z","shell.execute_reply.started":"2024-06-29T05:06:44.620103Z","shell.execute_reply":"2024-06-29T05:06:56.732304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testdf","metadata":{"execution":{"iopub.status.busy":"2024-06-29T06:24:02.345027Z","iopub.execute_input":"2024-06-29T06:24:02.345396Z","iopub.status.idle":"2024-06-29T06:24:02.371040Z","shell.execute_reply.started":"2024-06-29T06:24:02.345365Z","shell.execute_reply":"2024-06-29T06:24:02.369997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in testdf.columns:\n    if testdf.dtypes[col] == 'object':\n        le.fit(testdf[col])\n        testdf[col] = safe_transform(le, testdf[col])","metadata":{"execution":{"iopub.status.busy":"2024-06-29T05:07:55.362103Z","iopub.execute_input":"2024-06-29T05:07:55.362523Z","iopub.status.idle":"2024-06-29T05:07:55.584691Z","shell.execute_reply.started":"2024-06-29T05:07:55.362490Z","shell.execute_reply":"2024-06-29T05:07:55.583326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testdf","metadata":{"execution":{"iopub.status.busy":"2024-06-29T06:33:29.888066Z","iopub.execute_input":"2024-06-29T06:33:29.888479Z","iopub.status.idle":"2024-06-29T06:33:29.915275Z","shell.execute_reply.started":"2024-06-29T06:33:29.888421Z","shell.execute_reply":"2024-06-29T06:33:29.914167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testdf = testdf.drop(['status', 'constructorRef', 'nationality', 'round', 'laps'], axis=1)\ntestdf = ss.transform(testdf)","metadata":{"execution":{"iopub.status.busy":"2024-06-29T06:33:51.441302Z","iopub.execute_input":"2024-06-29T06:33:51.441993Z","iopub.status.idle":"2024-06-29T06:33:51.496018Z","shell.execute_reply.started":"2024-06-29T06:33:51.441960Z","shell.execute_reply":"2024-06-29T06:33:51.495197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = voting.predict(testdf)\ny_pred","metadata":{"execution":{"iopub.status.busy":"2024-06-29T06:33:57.817756Z","iopub.execute_input":"2024-06-29T06:33:57.818405Z","iopub.status.idle":"2024-06-29T06:33:59.231278Z","shell.execute_reply.started":"2024-06-29T06:33:57.818373Z","shell.execute_reply":"2024-06-29T06:33:59.230294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = [y_pred[i][0] for i in range(len(y_pred))]\ny_pred","metadata":{"execution":{"iopub.status.busy":"2024-06-29T06:24:49.173337Z","iopub.execute_input":"2024-06-29T06:24:49.173978Z","iopub.status.idle":"2024-06-29T06:24:49.342741Z","shell.execute_reply.started":"2024-06-29T06:24:49.173943Z","shell.execute_reply":"2024-06-29T06:24:49.341805Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testid = test['result_driver_standing']\ntestid","metadata":{"execution":{"iopub.status.busy":"2024-06-29T06:34:05.951264Z","iopub.execute_input":"2024-06-29T06:34:05.951998Z","iopub.status.idle":"2024-06-29T06:34:05.959587Z","shell.execute_reply.started":"2024-06-29T06:34:05.951965Z","shell.execute_reply":"2024-06-29T06:34:05.958708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.DataFrame({'position': y_pred, 'result_driver_standing': testid})\nsub.to_csv('sub10.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-06-29T06:34:51.841930Z","iopub.execute_input":"2024-06-29T06:34:51.842850Z","iopub.status.idle":"2024-06-29T06:34:52.870896Z","shell.execute_reply.started":"2024-06-29T06:34:51.842818Z","shell.execute_reply":"2024-06-29T06:34:52.869870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub","metadata":{"execution":{"iopub.status.busy":"2024-06-29T06:35:45.380884Z","iopub.execute_input":"2024-06-29T06:35:45.381244Z","iopub.status.idle":"2024-06-29T06:35:45.392714Z","shell.execute_reply.started":"2024-06-29T06:35:45.381215Z","shell.execute_reply":"2024-06-29T06:35:45.391698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\n\n# Generate sample data (replace with your actual DataFrame dfx)\nimport numpy as np\n\n# Plotting the heatmap\nplt.figure(figsize=(20, 20))  # Set the figure size\n\n# Adjusting font size for annotations\nsns.heatmap(fulldf.corr(), \n            annot=True, \n            cmap='RdBu', \n            fmt='.2f', \n            square=True,\n            annot_kws={\"size\": 10},  # Adjust font size here\n            vmin=-1, vmax=1)  # Set vmin and vmax to control the range\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-29T05:22:25.349625Z","iopub.execute_input":"2024-06-29T05:22:25.351249Z","iopub.status.idle":"2024-06-29T05:22:29.981208Z","shell.execute_reply.started":"2024-06-29T05:22:25.351190Z","shell.execute_reply":"2024-06-29T05:22:29.980047Z"},"trusted":true},"execution_count":null,"outputs":[]}]}