{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceType":"competition","sourceId":132748,"databundleVersionId":15922914}],"dockerImageVersionId":31287,"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":"2026-03-04T16:32:04.863871Z","iopub.execute_input":"2026-03-04T16:32:04.864094Z","iopub.status.idle":"2026-03-04T16:32:05.872490Z","shell.execute_reply.started":"2026-03-04T16:32:04.864072Z","shell.execute_reply":"2026-03-04T16:32:05.871841Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.compose import ColumnTransformer\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.preprocessing import OrdinalEncoder\n\n# 1. Define exact file paths\ntest_path = '/kaggle/input/competitions/econ-626-w-2026-pc-7-prediction-competition/PC7_test_without_response_v1.csv'\ntrain_2025_path = '/kaggle/input/competitions/econ-626-w-2026-pc-7-prediction-competition/archive/424_F2025_Final_PC_large_train_v1.csv'\ntrain_2024_path = '/kaggle/input/competitions/econ-626-w-2026-pc-7-prediction-competition/archive/archive/424_F2024_Final_PC_large_train_v1.csv'\n\n# 2. Load and combine the data\nprint(\"Loading data...\")\ntest_df = pd.read_csv(test_path)\ntrain_2025 = pd.read_csv(train_2025_path)\ntrain_2024 = pd.read_csv(train_2024_path)\n\n# Concatenate the historical datasets to maximize training data\ntrain_df = pd.concat([train_2025, train_2024], ignore_index=True)\nprint(f\"Combined Training shape: {train_df.shape}\")\nprint(f\"Testing shape: {test_df.shape}\")\n\n# 3. Define Target and ID columns\nTARGET_COL = 'rating' \nID_COL = 'ID_num'\n\n# Separate features and target\ny_train = train_df[TARGET_COL]\nX_train = train_df.drop(columns=[TARGET_COL, ID_COL], errors='ignore')\n\n# Ensure test set matches features (ignoring ID_num if it doesn't exist)\nX_test = test_df.drop(columns=[ID_COL], errors='ignore')\n\n# 4. Create a robust preprocessing pipeline\n# Identify numeric and categorical columns\nnumeric_cols = X_train.select_dtypes(include=['int64', 'float64']).columns\ncategorical_cols = X_train.select_dtypes(include=['object', 'category']).columns\n\n# Preprocessor for numeric data: Fill missing with median\nnumeric_transformer = SimpleImputer(strategy='median')\n\n# Preprocessor for categorical data: Fill missing with 'missing', then encode as integers\ncategorical_transformer = Pipeline(steps=[\n    ('imputer', SimpleImputer(strategy='constant', fill_value='missing')),\n    ('encoder', OrdinalEncoder(handle_unknown='use_encoded_value', unknown_value=-1))\n])\n\n# Combine preprocessors\npreprocessor = ColumnTransformer(\n    transformers=[\n        ('num', numeric_transformer, numeric_cols),\n        ('cat', categorical_transformer, categorical_cols)\n    ])\n\n# 5. Define and train the model pipeline\n# Using 50 estimators for a fast baseline. You can increase this later for better accuracy.\nmodel_pipeline = Pipeline(steps=[\n    ('preprocessor', preprocessor),\n    ('classifier', RandomForestClassifier(n_estimators=50, random_state=42, n_jobs=-1))\n])\n\nprint(\"Training model...\")\nmodel_pipeline.fit(X_train, y_train)\n\n# 6. Make Predictions and format for Kaggle\nprint(\"Making predictions...\")\npredictions = model_pipeline.predict(X_test)\n\n# Generate ID_num dynamically based on the number of rows in the test set\nprint(\"Formatting submission...\")\nsubmission = pd.DataFrame({\n    'ID_num': range(1, len(test_df) + 1),\n    'prediction': predictions\n})\n\n# Save the Kaggle submission file\nsubmission.to_csv('submission.csv', index=False)\nprint(\"Saved submission.csv successfully! You can now submit this to Kaggle.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-04T16:37:10.571647Z","iopub.execute_input":"2026-03-04T16:37:10.572441Z","iopub.status.idle":"2026-03-04T16:38:34.010308Z","shell.execute_reply.started":"2026-03-04T16:37:10.572403Z","shell.execute_reply":"2026-03-04T16:38:34.009661Z"}},"outputs":[],"execution_count":null}]}