{"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":81933,"databundleVersionId":9643020,"sourceType":"competition"}],"dockerImageVersionId":30822,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib.gridspec as gridspec\nimport seaborn as sns\nimport warnings\nimport xgboost as xgb\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import mean_squared_error\nfrom sklearn.preprocessing import LabelEncoder\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-01-01T16:56:54.392246Z","iopub.execute_input":"2025-01-01T16:56:54.392655Z","iopub.status.idle":"2025-01-01T16:56:54.398569Z","shell.execute_reply.started":"2025-01-01T16:56:54.392621Z","shell.execute_reply":"2025-01-01T16:56:54.397116Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"warnings.filterwarnings('ignore', category=FutureWarning)\n\nsns.set(style=\"whitegrid\")\n%matplotlib inline","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T16:56:54.400056Z","iopub.execute_input":"2025-01-01T16:56:54.400508Z","iopub.status.idle":"2025-01-01T16:56:54.424635Z","shell.execute_reply.started":"2025-01-01T16:56:54.400471Z","shell.execute_reply":"2025-01-01T16:56:54.423564Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/child-mind-institute-problematic-internet-use/train.csv')\ntest = pd.read_csv('/kaggle/input/child-mind-institute-problematic-internet-use/test.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T16:56:54.426303Z","iopub.execute_input":"2025-01-01T16:56:54.426681Z","iopub.status.idle":"2025-01-01T16:56:54.502704Z","shell.execute_reply.started":"2025-01-01T16:56:54.426632Z","shell.execute_reply":"2025-01-01T16:56:54.501236Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"numerical_columns_train = train.select_dtypes(include=['number']).columns.tolist()\ncategorical_columns_train = train.select_dtypes(include=['object', 'category']).columns.tolist()\nnumerical_columns_test = test.select_dtypes(include=['number']).columns.tolist()\ncategorical_columns_test = test.select_dtypes(include=['object', 'category']).columns.tolist()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T16:56:54.504253Z","iopub.execute_input":"2025-01-01T16:56:54.504572Z","iopub.status.idle":"2025-01-01T16:56:54.512333Z","shell.execute_reply.started":"2025-01-01T16:56:54.504543Z","shell.execute_reply":"2025-01-01T16:56:54.511330Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Numerical Columns:\", numerical_columns_train)\nprint(\"Categorical Columns:\", categorical_columns_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T16:56:54.513565Z","iopub.execute_input":"2025-01-01T16:56:54.513981Z","iopub.status.idle":"2025-01-01T16:56:54.533756Z","shell.execute_reply.started":"2025-01-01T16:56:54.513942Z","shell.execute_reply":"2025-01-01T16:56:54.532575Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"columns_in_train_not_in_test = [col for col in train.columns if col not in test.columns]\nprint(\"Columns in train but not in test:\", columns_in_train_not_in_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T16:56:54.534832Z","iopub.execute_input":"2025-01-01T16:56:54.535237Z","iopub.status.idle":"2025-01-01T16:56:54.554443Z","shell.execute_reply.started":"2025-01-01T16:56:54.535196Z","shell.execute_reply":"2025-01-01T16:56:54.553278Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# target_columns = [\n#     'PCIAT-Season', 'PCIAT-PCIAT_01', 'PCIAT-PCIAT_02', 'PCIAT-PCIAT_03', 'PCIAT-PCIAT_04',\n#     'PCIAT-PCIAT_05', 'PCIAT-PCIAT_06', 'PCIAT-PCIAT_07', 'PCIAT-PCIAT_08', 'PCIAT-PCIAT_09',\n#     'PCIAT-PCIAT_10', 'PCIAT-PCIAT_11', 'PCIAT-PCIAT_12', 'PCIAT-PCIAT_13', 'PCIAT-PCIAT_14',\n#     'PCIAT-PCIAT_15', 'PCIAT-PCIAT_16', 'PCIAT-PCIAT_17', 'PCIAT-PCIAT_18', 'PCIAT-PCIAT_19', 'PCIAT-PCIAT_20'\n# ]\ntarget_columns = [\n     'PCIAT-PCIAT_01', 'PCIAT-PCIAT_02', 'PCIAT-PCIAT_03', 'PCIAT-PCIAT_04',\n    'PCIAT-PCIAT_05', 'PCIAT-PCIAT_06', 'PCIAT-PCIAT_07', 'PCIAT-PCIAT_08', 'PCIAT-PCIAT_09',\n    'PCIAT-PCIAT_10', 'PCIAT-PCIAT_11', 'PCIAT-PCIAT_12', 'PCIAT-PCIAT_13', 'PCIAT-PCIAT_14',\n    'PCIAT-PCIAT_15', 'PCIAT-PCIAT_16', 'PCIAT-PCIAT_17', 'PCIAT-PCIAT_18', 'PCIAT-PCIAT_19', 'PCIAT-PCIAT_20'\n]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T16:56:54.555673Z","iopub.execute_input":"2025-01-01T16:56:54.556070Z","iopub.status.idle":"2025-01-01T16:56:54.576019Z","shell.execute_reply.started":"2025-01-01T16:56:54.556041Z","shell.execute_reply":"2025-01-01T16:56:54.574885Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# train2 = train.drop(columns=['PCIAT-PCIAT_Total', 'sii', 'id'])\ntrain2 = train.drop(columns=['PCIAT-PCIAT_Total', 'sii', 'id','PCIAT-Season'])\ntrain2 = train2.dropna(subset=target_columns)\nX_train = train2.drop(columns=target_columns)\ny_train = train2[target_columns]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T16:56:54.578598Z","iopub.execute_input":"2025-01-01T16:56:54.578910Z","iopub.status.idle":"2025-01-01T16:56:54.607411Z","shell.execute_reply.started":"2025-01-01T16:56:54.578883Z","shell.execute_reply":"2025-01-01T16:56:54.606310Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"numerical_columns = X_train.select_dtypes(include=['number']).columns.tolist()\ncategorical_columns = X_train.select_dtypes(include=['object', 'category']).columns.tolist()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T16:56:54.608848Z","iopub.execute_input":"2025-01-01T16:56:54.609281Z","iopub.status.idle":"2025-01-01T16:56:54.619676Z","shell.execute_reply.started":"2025-01-01T16:56:54.609242Z","shell.execute_reply":"2025-01-01T16:56:54.618133Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for col in categorical_columns:\n    X_train[col] = X_train[col].astype('category')\n\nX_test = test.drop(columns='id')\nfor col in categorical_columns:\n    X_test[col] = X_test[col].astype('category')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T16:56:54.621015Z","iopub.execute_input":"2025-01-01T16:56:54.621473Z","iopub.status.idle":"2025-01-01T16:56:54.656257Z","shell.execute_reply.started":"2025-01-01T16:56:54.621433Z","shell.execute_reply":"2025-01-01T16:56:54.655082Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions = {}\nfor target_col in target_columns:\n    print(f\"Training model for {target_col}...\")\n    \n    # Initialize the model\n    model = xgb.XGBRegressor(\n        objective='reg:squarederror',  # Regression task\n        eval_metric='rmse',\n        learning_rate=0.1,\n        max_depth=6,\n        n_estimators=100,\n        tree_method='hist',  # Use 'gpu_hist' if a GPU is available\n        enable_categorical=True  # Enable native handling of categorical data\n    )\n    \n    # Fit the model\n    model.fit(\n        X_train, train2[target_col], \n        verbose=False\n    )\n    \n    # Predict on the test set\n    predictions[target_col] = model.predict(X_test)\n    predictions[target_col] = predictions[target_col].round().clip(0, 5).astype(int)\n\n\n# Create a DataFrame with predictions\npredicted_test = pd.DataFrame(predictions)\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T17:18:57.192520Z","iopub.execute_input":"2025-01-01T17:18:57.192937Z","iopub.status.idle":"2025-01-01T17:19:15.239869Z","shell.execute_reply.started":"2025-01-01T17:18:57.192903Z","shell.execute_reply":"2025-01-01T17:19:15.239040Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(predicted_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T17:19:39.704952Z","iopub.execute_input":"2025-01-01T17:19:39.705347Z","iopub.status.idle":"2025-01-01T17:19:39.716245Z","shell.execute_reply.started":"2025-01-01T17:19:39.705315Z","shell.execute_reply":"2025-01-01T17:19:39.715164Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predicted_test['PCIAT-PCIAT_Total'] = predicted_test.sum(axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T17:33:39.672599Z","iopub.execute_input":"2025-01-01T17:33:39.672949Z","iopub.status.idle":"2025-01-01T17:33:39.679257Z","shell.execute_reply.started":"2025-01-01T17:33:39.672923Z","shell.execute_reply":"2025-01-01T17:33:39.678248Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(predicted_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T17:33:42.058830Z","iopub.execute_input":"2025-01-01T17:33:42.059165Z","iopub.status.idle":"2025-01-01T17:33:42.071868Z","shell.execute_reply.started":"2025-01-01T17:33:42.059139Z","shell.execute_reply":"2025-01-01T17:33:42.070544Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pciat_min_max = train.groupby('sii')['PCIAT-PCIAT_Total'].agg(['min', 'max'])\npciat_min_max = pciat_min_max.rename(\n    columns={'min': 'Minimum PCIAT total Score', 'max': 'Maximum total PCIAT Score'}\n)\npciat_min_max","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T17:35:50.273466Z","iopub.execute_input":"2025-01-01T17:35:50.273842Z","iopub.status.idle":"2025-01-01T17:35:50.308205Z","shell.execute_reply.started":"2025-01-01T17:35:50.273815Z","shell.execute_reply":"2025-01-01T17:35:50.307217Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def map_sii(total_score):\n    if total_score <= 30:\n        return 0.0\n    elif 31 <= total_score <= 49:\n        return 1.0\n    elif 50 <= total_score <= 79:\n        return 2.0\n    elif 80 <= total_score <= 93:\n        return 3.0\n    else:\n        return None  # Handle unexpected scores gracefully\n\n# Apply the function to calculate the 'sii' column\npredicted_test['sii'] = predicted_test['PCIAT-PCIAT_Total'].apply(map_sii)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T17:53:33.119923Z","iopub.execute_input":"2025-01-01T17:53:33.120337Z","iopub.status.idle":"2025-01-01T17:53:33.127921Z","shell.execute_reply.started":"2025-01-01T17:53:33.120304Z","shell.execute_reply":"2025-01-01T17:53:33.126371Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(predicted_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T17:53:36.055607Z","iopub.execute_input":"2025-01-01T17:53:36.055985Z","iopub.status.idle":"2025-01-01T17:53:36.069585Z","shell.execute_reply.started":"2025-01-01T17:53:36.055953Z","shell.execute_reply":"2025-01-01T17:53:36.068477Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.DataFrame({\n    'id': test['id'],  # Take the 'id' column from the test set\n    'sii': predicted_test['sii']  # Use the predicted 'sii' values\n})\n\n# Save the submission to a CSV file\nsubmission.to_csv('submission.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T17:56:19.094495Z","iopub.execute_input":"2025-01-01T17:56:19.094855Z","iopub.status.idle":"2025-01-01T17:56:19.107035Z","shell.execute_reply.started":"2025-01-01T17:56:19.094827Z","shell.execute_reply":"2025-01-01T17:56:19.105871Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-01T17:58:00.102819Z","iopub.execute_input":"2025-01-01T17:58:00.103223Z","iopub.status.idle":"2025-01-01T17:58:00.114096Z","shell.execute_reply.started":"2025-01-01T17:58:00.103153Z","shell.execute_reply":"2025-01-01T17:58:00.113147Z"}},"outputs":[],"execution_count":null}]}