{"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":"gpu","dataSources":[{"sourceId":84896,"databundleVersionId":10305135,"sourceType":"competition"}],"dockerImageVersionId":30823,"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","trusted":true,"execution":{"iopub.status.busy":"2024-12-27T09:31:08.955775Z","iopub.execute_input":"2024-12-27T09:31:08.956056Z","iopub.status.idle":"2024-12-27T09:31:08.962403Z","shell.execute_reply.started":"2024-12-27T09:31:08.956036Z","shell.execute_reply":"2024-12-27T09:31:08.961393Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestRegressor\nfrom sklearn.metrics import mean_squared_log_error\nfrom sklearn.preprocessing import OneHotEncoder\nfrom sklearn.compose import ColumnTransformer\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.linear_model import Ridge\nfrom sklearn.ensemble import GradientBoostingRegressor\nfrom sklearn.feature_selection import SelectKBest, f_regression\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-27T09:31:08.963538Z","iopub.execute_input":"2024-12-27T09:31:08.96379Z","iopub.status.idle":"2024-12-27T09:31:08.983022Z","shell.execute_reply.started":"2024-12-27T09:31:08.96377Z","shell.execute_reply":"2024-12-27T09:31:08.982388Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load the data\ntrain_data = pd.read_csv('/kaggle/input/playground-series-s4e12/train.csv')\ntest_data = pd.read_csv('/kaggle/input/playground-series-s4e12/test.csv')\n\n# Separate features and target\nX = train_data.drop(columns=['Premium Amount'])\ny = train_data['Premium Amount']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-27T09:31:08.984242Z","iopub.execute_input":"2024-12-27T09:31:08.984488Z","iopub.status.idle":"2024-12-27T09:31:14.987227Z","shell.execute_reply.started":"2024-12-27T09:31:08.984463Z","shell.execute_reply":"2024-12-27T09:31:14.986515Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = X.fillna(X.median(numeric_only=True))\ntest_data = test_data.fillna(test_data.median(numeric_only=True))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-27T09:31:14.988362Z","iopub.execute_input":"2024-12-27T09:31:14.988585Z","iopub.status.idle":"2024-12-27T09:31:15.618771Z","shell.execute_reply.started":"2024-12-27T09:31:14.988566Z","shell.execute_reply":"2024-12-27T09:31:15.6181Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"categorical_cols = X.select_dtypes(include=['object', 'category']).columns\nnumerical_cols = X.select_dtypes(include=['int64', 'float64']).columns\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-27T09:31:15.619596Z","iopub.execute_input":"2024-12-27T09:31:15.619817Z","iopub.status.idle":"2024-12-27T09:31:15.843346Z","shell.execute_reply.started":"2024-12-27T09:31:15.619799Z","shell.execute_reply":"2024-12-27T09:31:15.842677Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"categorical_transformer = OneHotEncoder(handle_unknown='ignore')\n\npreprocessor = ColumnTransformer(\n    transformers=[\n        ('cat', categorical_transformer, categorical_cols)\n    ],\n    remainder='passthrough')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-27T09:31:15.844229Z","iopub.execute_input":"2024-12-27T09:31:15.844569Z","iopub.status.idle":"2024-12-27T09:31:15.848632Z","shell.execute_reply.started":"2024-12-27T09:31:15.844541Z","shell.execute_reply":"2024-12-27T09:31:15.847847Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Define the model\nmodel = Ridge(alpha=1.0, random_state=42)\n\n# Create the pipeline\npipeline = Pipeline(steps=[\n    ('preprocessor', preprocessor),\n    ('model', model)\n])\n\n# Split the data into training and validation sets\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)\n\n# Fit the model\npipeline.fit(X_train, y_train)\n\n# Validate the model\ny_pred = pipeline.predict(X_val)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-27T09:31:15.849475Z","iopub.execute_input":"2024-12-27T09:31:15.849709Z","iopub.status.idle":"2024-12-27T09:31:24.415581Z","shell.execute_reply.started":"2024-12-27T09:31:15.849691Z","shell.execute_reply":"2024-12-27T09:31:24.414883Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"rmsle = np.sqrt(mean_squared_log_error(y_val, np.maximum(0, y_pred)))\nprint(f\"RMSLE on validation data: {rmsle}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-27T09:31:24.417201Z","iopub.execute_input":"2024-12-27T09:31:24.41749Z","iopub.status.idle":"2024-12-27T09:31:24.42579Z","shell.execute_reply.started":"2024-12-27T09:31:24.417469Z","shell.execute_reply":"2024-12-27T09:31:24.425011Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Predict on test data\ntest_predictions = pipeline.predict(test_data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-27T09:31:24.426647Z","iopub.execute_input":"2024-12-27T09:31:24.426886Z","iopub.status.idle":"2024-12-27T09:31:27.466996Z","shell.execute_reply.started":"2024-12-27T09:31:24.426868Z","shell.execute_reply":"2024-12-27T09:31:27.466298Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.DataFrame({\n    'id': test_data['id'],  # Use the correct ID column from your test data\n    'Premium Amount': test_predictions\n})\n\n# Save to CSV\nsubmission.to_csv('submission_final.csv', index=False)\n\nprint(\"Submission file 'submission_final.csv' created successfully!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-27T09:31:27.467782Z","iopub.execute_input":"2024-12-27T09:31:27.468014Z","iopub.status.idle":"2024-12-27T09:31:28.810927Z","shell.execute_reply.started":"2024-12-27T09:31:27.467994Z","shell.execute_reply":"2024-12-27T09:31:28.810045Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-27T09:31:28.81187Z","iopub.execute_input":"2024-12-27T09:31:28.81219Z","iopub.status.idle":"2024-12-27T09:31:28.821681Z","shell.execute_reply.started":"2024-12-27T09:31:28.812156Z","shell.execute_reply":"2024-12-27T09:31:28.820847Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}