{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","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"}],"dockerImageVersionId":30918,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"pip -q install autogluon","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-04-06T18:01:21.212089Z","iopub.execute_input":"2025-04-06T18:01:21.212603Z","iopub.status.idle":"2025-04-06T18:01:27.872928Z","shell.execute_reply.started":"2025-04-06T18:01:21.212566Z","shell.execute_reply":"2025-04-06T18:01:27.871545Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Import necessary libraries\nimport pandas as pd\nimport numpy as np\nimport os\nfrom autogluon.tabular import TabularPredictor","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-06T18:03:50.882691Z","iopub.execute_input":"2025-04-06T18:03:50.883087Z","iopub.status.idle":"2025-04-06T18:03:50.888165Z","shell.execute_reply.started":"2025-04-06T18:03:50.883056Z","shell.execute_reply":"2025-04-06T18:03:50.886961Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Set random seed for reproducibility\nRANDOM_SEED = 42\nnp.random.seed(RANDOM_SEED)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-06T18:04:00.93005Z","iopub.execute_input":"2025-04-06T18:04:00.930471Z","iopub.status.idle":"2025-04-06T18:04:00.935068Z","shell.execute_reply.started":"2025-04-06T18:04:00.930435Z","shell.execute_reply":"2025-04-06T18:04:00.93385Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load data\nprint(\"Loading data...\")\ntrain = pd.read_csv('/kaggle/input/playground-series-s4e12/train.csv', index_col=[0])\ntest = pd.read_csv('/kaggle/input/playground-series-s4e12/test.csv', index_col=[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-06T18:02:14.969542Z","iopub.execute_input":"2025-04-06T18:02:14.969904Z","iopub.status.idle":"2025-04-06T18:02:22.765699Z","shell.execute_reply.started":"2025-04-06T18:02:14.969875Z","shell.execute_reply":"2025-04-06T18:02:22.764566Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Identify target column\ntarget_column = (set(train.columns) - set(test.columns)).pop()\nprint(f\"Target column: {target_column}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-06T18:05:00.622062Z","iopub.execute_input":"2025-04-06T18:05:00.622484Z","iopub.status.idle":"2025-04-06T18:05:00.628403Z","shell.execute_reply.started":"2025-04-06T18:05:00.622452Z","shell.execute_reply":"2025-04-06T18:05:00.627293Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Quick feature engineering\nprint(\"Performing quick feature engineering...\")\nnumeric_cols = train.select_dtypes(include=['int64', 'float64']).columns.tolist()\nnumeric_cols = [col for col in numeric_cols if col != target_column]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-06T18:05:01.336133Z","iopub.execute_input":"2025-04-06T18:05:01.336532Z","iopub.status.idle":"2025-04-06T18:05:01.360614Z","shell.execute_reply.started":"2025-04-06T18:05:01.336498Z","shell.execute_reply":"2025-04-06T18:05:01.359463Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Create a few high-value interaction features (limited to save time)\nif len(numeric_cols) >= 2:\n    # Sort features by correlation with target to prioritize important ones\n    corr_with_target = train[numeric_cols].corrwith(train[target_column]).abs().sort_values(ascending=False)\n    top_features = corr_with_target.index[:5].tolist()  # Take only top 5 features\n    \n    for i, col1 in enumerate(top_features):\n        for col2 in top_features[i+1:]:\n            train[f'{col1}_mult_{col2}'] = train[col1] * train[col2]\n            test[f'{col1}_mult_{col2}'] = test[col1] * test[col2]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-06T18:05:02.023074Z","iopub.execute_input":"2025-04-06T18:05:02.023478Z","iopub.status.idle":"2025-04-06T18:05:02.352014Z","shell.execute_reply.started":"2025-04-06T18:05:02.023447Z","shell.execute_reply":"2025-04-06T18:05:02.350936Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Initialize and train predictor with time-optimized settings\nprint(\"Training AutoGluon model (10 minute time limit)...\")\npredictor = TabularPredictor(\n    label=target_column,\n    path='autogluon_output',\n    problem_type='regression',\n    eval_metric='root_mean_squared_error'\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-06T18:05:02.35345Z","iopub.execute_input":"2025-04-06T18:05:02.353814Z","iopub.status.idle":"2025-04-06T18:05:02.368121Z","shell.execute_reply.started":"2025-04-06T18:05:02.353787Z","shell.execute_reply":"2025-04-06T18:05:02.366708Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Configure training to fit within 10 minutes\npredictor.fit(\n    train_data=train,\n    time_limit=600,  # 10 minutes total\n    presets='medium_quality_faster_train',  # Faster training preset\n    hyperparameters={\n        'GBM': [  # LightGBM - fast and effective\n            {'num_boost_round': 100, 'num_leaves': 31}\n        ],\n        'XGB': [  # XGBoost with limited iterations\n            {'n_estimators': 100, 'max_depth': 6}\n        ],\n        'RF': [  # Random Forest with fewer trees\n            {'n_estimators': 100}\n        ]\n    },\n    # Disable slower models to save time\n    excluded_model_types=['NN_TORCH', 'CAT', 'KNN'],\n    num_bag_folds=3,  # Still use some bagging for robustness\n    num_bag_sets=1,\n    num_stack_levels=0,  # Disable stacking to save time\n    verbosity=2\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-06T18:05:02.420312Z","iopub.execute_input":"2025-04-06T18:05:02.420677Z","iopub.status.idle":"2025-04-06T18:13:38.18326Z","shell.execute_reply.started":"2025-04-06T18:05:02.420651Z","shell.execute_reply":"2025-04-06T18:13:38.1822Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Quick model evaluation\nprint(\"\\nModel performance:\")\nleaderboard = predictor.leaderboard(silent=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-06T18:13:38.184539Z","iopub.execute_input":"2025-04-06T18:13:38.184829Z","iopub.status.idle":"2025-04-06T18:13:38.200796Z","shell.execute_reply.started":"2025-04-06T18:13:38.184802Z","shell.execute_reply":"2025-04-06T18:13:38.199649Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Generate and save feature importance if available\ntry:\n    importance = predictor.feature_importance(train)\n    print(\"\\nTop 10 important features:\")\n    print(importance.head(10))\nexcept:\n    print(\"Feature importance calculation unavailable\")\n\n# Make predictions on test data\nprint(\"\\nGenerating predictions...\")\ntest_pred = predictor.predict(test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-06T18:13:38.202376Z","iopub.execute_input":"2025-04-06T18:13:38.202709Z","iopub.status.idle":"2025-04-06T18:14:40.039213Z","shell.execute_reply.started":"2025-04-06T18:13:38.202683Z","shell.execute_reply":"2025-04-06T18:14:40.038239Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Save predictions for submission\nsubmission = pd.DataFrame({\n    'Premium Amount': test_pred\n})\nsubmission.index = test.index\nsubmission.to_csv('submission.csv')\n\nprint(f\"Predictions saved to submission.csv\")\nprint(f\"Prediction summary: min={test_pred.min():.4f}, max={test_pred.max():.4f}, mean={test_pred.mean():.4f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-06T18:22:29.940743Z","iopub.execute_input":"2025-04-06T18:22:29.9412Z","iopub.status.idle":"2025-04-06T18:22:31.097885Z","shell.execute_reply.started":"2025-04-06T18:22:29.94114Z","shell.execute_reply":"2025-04-06T18:22:31.096685Z"}},"outputs":[],"execution_count":null}]}