{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":84896,"databundleVersionId":10305135,"sourceType":"competition"},{"sourceId":9178166,"sourceType":"datasetVersion","datasetId":5547076}],"dockerImageVersionId":30805,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Loading Data ","metadata":{}},{"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","trusted":true,"execution":{"iopub.status.busy":"2024-12-17T01:10:34.295458Z","iopub.execute_input":"2024-12-17T01:10:34.295915Z","iopub.status.idle":"2024-12-17T01:10:34.770409Z","shell.execute_reply.started":"2024-12-17T01:10:34.295877Z","shell.execute_reply":"2024-12-17T01:10:34.768945Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train=pd.read_csv('/kaggle/input/playground-series-s4e12/train.csv').drop(columns='id')\ntest=pd.read_csv('/kaggle/input/playground-series-s4e12/test.csv').drop(columns='id')\ntest_id=pd.read_csv('/kaggle/input/playground-series-s4e12/test.csv')['id']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T01:10:34.773188Z","iopub.execute_input":"2024-12-17T01:10:34.77376Z","iopub.status.idle":"2024-12-17T01:10:50.652073Z","shell.execute_reply.started":"2024-12-17T01:10:34.773723Z","shell.execute_reply":"2024-12-17T01:10:50.650759Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Optional : combining train and original data ","metadata":{}},{"cell_type":"markdown","source":"# Installing Libraries ","metadata":{}},{"cell_type":"code","source":"!pip install autogluon.tabular --no-cache-dir -q","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T01:10:50.653672Z","iopub.execute_input":"2024-12-17T01:10:50.653991Z","iopub.status.idle":"2024-12-17T01:11:16.725094Z","shell.execute_reply.started":"2024-12-17T01:10:50.65396Z","shell.execute_reply":"2024-12-17T01:11:16.723623Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install ray==2.10.0","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T01:11:16.727043Z","iopub.execute_input":"2024-12-17T01:11:16.727453Z","iopub.status.idle":"2024-12-17T01:11:39.260534Z","shell.execute_reply.started":"2024-12-17T01:11:16.727414Z","shell.execute_reply":"2024-12-17T01:11:39.259034Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Training","metadata":{}},{"cell_type":"code","source":"from autogluon.tabular import TabularPredictor","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T01:11:39.26503Z","iopub.execute_input":"2024-12-17T01:11:39.265468Z","iopub.status.idle":"2024-12-17T01:11:40.472805Z","shell.execute_reply.started":"2024-12-17T01:11:39.265429Z","shell.execute_reply":"2024-12-17T01:11:40.47157Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":" %%time\npredictor = TabularPredictor(problem_type = 'regression', \n                             eval_metric = 'root_mean_squared_error',\n                             label='Premium Amount')\n\n    \npredictor.fit(train_data= train, \n                       # dynamic_stacking=False, num_stack_levels=1,\n                       presets='medium_quality',\n# best_quality,  medium_quality                         \n                       time_limit = 33000, \n                      # num_gpus=1, \n                       num_bag_folds = 7, \n                       num_stack_levels = 4, \n                       auto_stack = True, \n                       dynamic_stacking=True   \n                       )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T01:17:44.40052Z","iopub.execute_input":"2024-12-17T01:17:44.401371Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%%time\npredictor.leaderboard()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T01:11:40.760965Z","iopub.execute_input":"2024-12-17T01:11:40.761461Z","iopub.status.idle":"2024-12-17T01:11:41.234915Z","shell.execute_reply.started":"2024-12-17T01:11:40.761408Z","shell.execute_reply":"2024-12-17T01:11:41.233662Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%%time\nprint ('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')\npredictor.fit_summary ()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T01:11:41.236488Z","iopub.execute_input":"2024-12-17T01:11:41.236944Z","iopub.status.idle":"2024-12-17T01:11:41.33127Z","shell.execute_reply.started":"2024-12-17T01:11:41.236887Z","shell.execute_reply":"2024-12-17T01:11:41.330265Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Fetching Results ","metadata":{}},{"cell_type":"code","source":"# this cell allows to reload the trained model from disk\npredictor = TabularPredictor.load(\"/kaggle/working/Autogluon\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T01:11:41.332605Z","iopub.execute_input":"2024-12-17T01:11:41.332926Z","iopub.status.idle":"2024-12-17T01:11:41.630419Z","shell.execute_reply.started":"2024-12-17T01:11:41.332897Z","shell.execute_reply":"2024-12-17T01:11:41.628827Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions_log = pl.Series (predictor.predict(test_clean_df.to_pandas () ))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T01:11:41.631677Z","iopub.status.idle":"2024-12-17T01:11:41.632268Z","shell.execute_reply.started":"2024-12-17T01:11:41.631981Z","shell.execute_reply":"2024-12-17T01:11:41.632008Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# trainsforming the results back from the log scake \npredictions = predictions_log.exp()\npredictions","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T01:11:41.633443Z","iopub.status.idle":"2024-12-17T01:11:41.633977Z","shell.execute_reply.started":"2024-12-17T01:11:41.633701Z","shell.execute_reply":"2024-12-17T01:11:41.633728Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = sample_df.with_columns (predictions.alias('Premium Amount'))\nsubmission","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T01:11:41.636112Z","iopub.status.idle":"2024-12-17T01:11:41.636549Z","shell.execute_reply.started":"2024-12-17T01:11:41.636372Z","shell.execute_reply":"2024-12-17T01:11:41.636391Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.write_csv('submission.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T01:11:41.637812Z","iopub.status.idle":"2024-12-17T01:11:41.63835Z","shell.execute_reply.started":"2024-12-17T01:11:41.638065Z","shell.execute_reply":"2024-12-17T01:11:41.638092Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%%time \nimport zipfile\n\ndef zip_files_in_directory(directory, zip_name):\n    num_files_deleted = 0 \n    with zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED) as zipf:\n        for root, dirs, files in os.walk(directory):\n            for file in files:\n                file_path = os.path.join(root, file)\n                zipf.write(file_path, os.path.relpath(file_path, directory))\n                os.remove(file_path)\n                num_files_deleted += 1\n    return num_files_deleted  \n# Example usage\ndirectory = '/kaggle/working/Autogluon'\nzip_name = 'Autogluon.zip'\nn = zip_files_in_directory(directory, zip_name)\n\nprint(f\"deleted {n} files in {directory}\")","metadata":{"trusted":true,"scrolled":true,"execution":{"iopub.status.busy":"2024-12-17T01:11:41.640151Z","iopub.status.idle":"2024-12-17T01:11:41.64055Z","shell.execute_reply.started":"2024-12-17T01:11:41.640379Z","shell.execute_reply":"2024-12-17T01:11:41.640397Z"}},"outputs":[],"execution_count":null}]}