{"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":81933,"databundleVersionId":9643020,"sourceType":"competition"}],"dockerImageVersionId":30804,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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-05T13:30:10.845945Z","iopub.execute_input":"2024-12-05T13:30:10.846383Z","iopub.status.idle":"2024-12-05T13:30:13.802304Z","shell.execute_reply.started":"2024-12-05T13:30:10.846344Z","shell.execute_reply":"2024-12-05T13:30:13.800915Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import GradientBoostingClassifier\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.preprocessing import OneHotEncoder, StandardScaler\nfrom sklearn.compose import ColumnTransformer\nfrom sklearn.pipeline import Pipeline\n\n# Load dataset\ntrain_df = pd.read_csv('/kaggle/input/child-mind-institute-problematic-internet-use/train.csv')\ntest_df = pd.read_csv('/kaggle/input/child-mind-institute-problematic-internet-use/test.csv')\n\n# Drop rows with missing target in train dataset\ntrain_df = train_df.dropna(subset=['sii'])\n\n# Separate features and target\nX = train_df.drop(columns=['id', 'sii'])\ny = train_df['sii'].astype(int)\nX_test = test_df.drop(columns=['id'])\n\n# Identify categorical and numerical columns\ncategorical_cols = X.select_dtypes(include=['object']).columns\nnumerical_cols = X.select_dtypes(include=['number']).columns\n\n# Preprocessing pipeline\nnum_transformer = Pipeline(steps=[\n    ('imputer', SimpleImputer(strategy='mean')),\n    ('scaler', StandardScaler())\n])\n\ncat_transformer = Pipeline(steps=[\n    ('imputer', SimpleImputer(strategy='most_frequent')),\n    ('onehot', OneHotEncoder(handle_unknown='ignore'))\n])\n\npreprocessor = ColumnTransformer(transformers=[\n    ('num', num_transformer, numerical_cols),\n    ('cat', cat_transformer, categorical_cols)\n])\n\n# Full pipeline with Gradient Boosting model\nmodel = Pipeline(steps=[\n    ('preprocessor', preprocessor),\n    ('classifier', GradientBoostingClassifier(random_state=42))\n])\n\n# Align test columns with training columns\nX_test = X_test.reindex(columns=X.columns, fill_value=0)\n\n# Train-test split for local validation\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)\n\n# Train the model\nmodel.fit(X_train, y_train)\n\n# Evaluate model performance on validation set\nval_score = model.score(X_val, y_val)\nprint(f\"Validation Accuracy: {val_score:.2f}\")\n\n# Process the test dataset using the same pipeline\nX_test_processed = model.named_steps['preprocessor'].transform(X_test)\n\n# Predict on the test dataset\ny_test_pred = model.named_steps['classifier'].predict(X_test_processed)\n\n# Create submission file\nsubmission = pd.DataFrame({'id': test_df['id'], 'sii': y_test_pred})\nsubmission.to_csv('submission.csv', index=False)\nprint(\"Submission file created as 'submission.csv'.\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T13:51:06.304317Z","iopub.execute_input":"2024-12-05T13:51:06.304783Z","iopub.status.idle":"2024-12-05T13:51:12.216867Z","shell.execute_reply.started":"2024-12-05T13:51:06.304744Z","shell.execute_reply":"2024-12-05T13:51:12.215575Z"}},"outputs":[],"execution_count":null}]}