{"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":30786,"isInternetEnabled":true,"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","execution":{"iopub.status.busy":"2024-10-28T13:39:34.367773Z","iopub.execute_input":"2024-10-28T13:39:34.368680Z","iopub.status.idle":"2024-10-28T13:39:38.786195Z","shell.execute_reply.started":"2024-10-28T13:39:34.368629Z","shell.execute_reply":"2024-10-28T13:39:38.784994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv(r\"/kaggle/input/child-mind-institute-problematic-internet-use/train.csv\")\ntest_data = pd.read_csv(r\"/kaggle/input/child-mind-institute-problematic-internet-use/test.csv\")\ndata_dict = pd.read_csv(r\"/kaggle/input/child-mind-institute-problematic-internet-use/data_dictionary.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-10-28T13:39:38.788187Z","iopub.execute_input":"2024-10-28T13:39:38.788684Z","iopub.status.idle":"2024-10-28T13:39:38.897059Z","shell.execute_reply.started":"2024-10-28T13:39:38.788644Z","shell.execute_reply":"2024-10-28T13:39:38.895170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_data.head(20))","metadata":{"execution":{"iopub.status.busy":"2024-10-28T13:39:38.899390Z","iopub.execute_input":"2024-10-28T13:39:38.899777Z","iopub.status.idle":"2024-10-28T13:39:38.940528Z","shell.execute_reply.started":"2024-10-28T13:39:38.899738Z","shell.execute_reply":"2024-10-28T13:39:38.939167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#計算資料缺失程度\nmissing_data = train_data.isnull().mean() * 100\nprint(missing_data[missing_data > 0].sort_values(ascending=False))","metadata":{"execution":{"iopub.status.busy":"2024-10-28T13:39:38.942801Z","iopub.execute_input":"2024-10-28T13:39:38.943398Z","iopub.status.idle":"2024-10-28T13:39:38.961995Z","shell.execute_reply.started":"2024-10-28T13:39:38.943347Z","shell.execute_reply":"2024-10-28T13:39:38.960639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# #把缺失太嚴重的欄位(attribute)去掉\ncolumns_to_drop = missing_data[missing_data > 50].index\ntrain_data = train_data.drop(columns=columns_to_drop)\n\nprint(train_data.head(20))","metadata":{"execution":{"iopub.status.busy":"2024-10-28T13:39:38.963764Z","iopub.execute_input":"2024-10-28T13:39:38.964251Z","iopub.status.idle":"2024-10-28T13:39:38.991132Z","shell.execute_reply.started":"2024-10-28T13:39:38.964199Z","shell.execute_reply":"2024-10-28T13:39:38.989688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#剩下採用插補(以下方法為目前做法，若後續因此預測出的SSI不準確需要調整可以採其他方式\n# numerical data 用 中位數插補\nnumerical_columns = [\n    'Physical-BMI', 'Physical-Height', 'Physical-Weight', 'CGAS-CGAS_Score', 'PCIAT-PCIAT_Total', \n    'SDS-SDS_Total_Raw', 'SDS-SDS_Total_T', 'Physical-Diastolic_BP', 'Physical-HeartRate', 'Physical-Systolic_BP', \n    'FGC-FGC_CU', 'FGC-FGC_PU', 'FGC-FGC_SRL', 'FGC-FGC_SRR', 'FGC-FGC_TL', 'BIA-BIA_BMC', 'BIA-BIA_BMI', \n    'BIA-BIA_BMR', 'BIA-BIA_DEE', 'BIA-BIA_ECW', 'BIA-BIA_FFM', 'BIA-BIA_FFMI', 'BIA-BIA_FMI', 'BIA-BIA_Fat', \n    'BIA-BIA_ICW', 'BIA-BIA_LDM', 'BIA-BIA_LST', 'BIA-BIA_SMM', 'BIA-BIA_TBW', 'PCIAT-PCIAT_01', 'PCIAT-PCIAT_02', \n    'PCIAT-PCIAT_03', 'PCIAT-PCIAT_04', 'PCIAT-PCIAT_05', 'PCIAT-PCIAT_06', 'PCIAT-PCIAT_07', 'PCIAT-PCIAT_08', \n    'PCIAT-PCIAT_09', '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]\n\nfor col in numerical_columns:\n    train_data.loc[:, col] = train_data[col].fillna(train_data[col].median())\n\n# 類別變數用眾數插補\ncategorical_columns = [\n    'Basic_Demos-Sex', 'PreInt_EduHx-computerinternet_hoursday', 'Basic_Demos-Enroll_Season', 'CGAS-Season', \n    'Physical-Season', 'SDS-Season', 'PreInt_EduHx-Season', 'FGC-Season', 'FGC-FGC_CU_Zone', 'FGC-FGC_PU_Zone', \n    'FGC-FGC_SRL_Zone', 'FGC-FGC_SRR_Zone', 'FGC-FGC_TL_Zone', 'BIA-Season', 'BIA-BIA_Activity_Level_num', 'BIA-BIA_Frame_num'\n]\n\nfor col in categorical_columns:\n    train_data.loc[:, col] = train_data[col].fillna(train_data[col].mode()[0])\n\n# Drop rows where 'sii' is missing (target variable)\ntrain_data = train_data[train_data['sii'].notna()]\n\n# 檢視是否還有缺失值\nmissing_values = train_data.isnull().sum()\nprint(missing_values[missing_values > 0]) \n\ntrain_data","metadata":{"execution":{"iopub.status.busy":"2024-10-28T13:39:38.993319Z","iopub.execute_input":"2024-10-28T13:39:38.994078Z","iopub.status.idle":"2024-10-28T13:39:39.109416Z","shell.execute_reply.started":"2024-10-28T13:39:38.994026Z","shell.execute_reply":"2024-10-28T13:39:39.108196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**清理後相比原本少了1222筆資料**","metadata":{}},{"cell_type":"markdown","source":"## Data Visualization","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2024-10-28T13:39:39.110912Z","iopub.execute_input":"2024-10-28T13:39:39.111333Z","iopub.status.idle":"2024-10-28T13:39:40.027682Z","shell.execute_reply.started":"2024-10-28T13:39:39.111286Z","shell.execute_reply":"2024-10-28T13:39:40.026369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tableone import TableOne","metadata":{"execution":{"iopub.status.busy":"2024-10-28T13:43:54.096940Z","iopub.execute_input":"2024-10-28T13:43:54.097466Z","iopub.status.idle":"2024-10-28T13:43:54.179917Z","shell.execute_reply.started":"2024-10-28T13:43:54.097405Z","shell.execute_reply":"2024-10-28T13:43:54.179036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define numerical and categorical columns\nnumerical_columns = [\n    'Physical-BMI', 'Physical-Height', 'Physical-Weight', 'CGAS-CGAS_Score', 'PCIAT-PCIAT_Total',\n    'SDS-SDS_Total_Raw', 'SDS-SDS_Total_T', 'Physical-Diastolic_BP', 'Physical-HeartRate', 'Physical-Systolic_BP',\n    'FGC-FGC_CU', 'FGC-FGC_PU', 'FGC-FGC_SRL', 'FGC-FGC_SRR', 'FGC-FGC_TL', 'BIA-BIA_BMC', 'BIA-BIA_BMI',\n    'BIA-BIA_BMR', 'BIA-BIA_DEE', 'BIA-BIA_ECW', 'BIA-BIA_FFM', 'BIA-BIA_FFMI', 'BIA-BIA_FMI', 'BIA-BIA_Fat',\n    'BIA-BIA_ICW', 'BIA-BIA_LDM', 'BIA-BIA_LST', 'BIA-BIA_SMM', 'BIA-BIA_TBW', 'PCIAT-PCIAT_01', 'PCIAT-PCIAT_02',\n    'PCIAT-PCIAT_03', 'PCIAT-PCIAT_04', 'PCIAT-PCIAT_05', 'PCIAT-PCIAT_06', 'PCIAT-PCIAT_07', 'PCIAT-PCIAT_08',\n    'PCIAT-PCIAT_09', '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]\n\ncategorical_columns = [\n    'Basic_Demos-Sex', 'PreInt_EduHx-computerinternet_hoursday', 'Basic_Demos-Enroll_Season', 'CGAS-Season',\n    'Physical-Season', 'SDS-Season', 'PreInt_EduHx-Season', 'FGC-Season', 'FGC-FGC_CU_Zone', 'FGC-FGC_PU_Zone',\n    'FGC-FGC_SRL_Zone', 'FGC-FGC_SRR_Zone', 'FGC-FGC_TL_Zone', 'BIA-Season', 'BIA-BIA_Activity_Level_num', 'BIA-BIA_Frame_num'\n]\n\n# Create the TableOne object for descriptive statistics\ntable1 = TableOne(train_data, columns=numerical_columns + categorical_columns, categorical=categorical_columns, pval=False)\n\n# Print the summary table to the console\nprint(table1)","metadata":{"execution":{"iopub.status.busy":"2024-10-28T13:43:57.162072Z","iopub.execute_input":"2024-10-28T13:43:57.162487Z","iopub.status.idle":"2024-10-28T13:43:57.523684Z","shell.execute_reply.started":"2024-10-28T13:43:57.162448Z","shell.execute_reply":"2024-10-28T13:43:57.522481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create the TableOne object with grouping by 'sii'\ntable1_grouped = TableOne(train_data, columns=numerical_columns + categorical_columns, categorical=categorical_columns, groupby='sii', pval=False, htest_name=False)\n\n# Print the grouped summary table\nprint(table1_grouped)","metadata":{"execution":{"iopub.status.busy":"2024-10-28T13:47:24.590317Z","iopub.execute_input":"2024-10-28T13:47:24.591613Z","iopub.status.idle":"2024-10-28T13:47:25.720502Z","shell.execute_reply.started":"2024-10-28T13:47:24.591523Z","shell.execute_reply":"2024-10-28T13:47:25.719319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Distribution Of SII (Target Variable)\n\nplt.figure(figsize=(10, 6))\nsns.countplot(x='sii', data=train_data, palette='viridis')\nplt.title('SII (Target Variable) Distribution')\nplt.xlabel('SII')\nplt.ylabel('Number of Data')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-28T13:39:57.784633Z","iopub.execute_input":"2024-10-28T13:39:57.785045Z","iopub.status.idle":"2024-10-28T13:39:58.117269Z","shell.execute_reply.started":"2024-10-28T13:39:57.785005Z","shell.execute_reply":"2024-10-28T13:39:58.115277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Age Distribution\n\nplt.figure(figsize=(10, 6))\nsns.histplot(train_data['Basic_Demos-Age'], kde=True, bins=20, color='skyblue')\nplt.title('Age Distribution')\nplt.xlabel('Age')\nplt.ylabel('Frequency')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-28T13:40:03.289703Z","iopub.execute_input":"2024-10-28T13:40:03.290138Z","iopub.status.idle":"2024-10-28T13:40:03.734113Z","shell.execute_reply.started":"2024-10-28T13:40:03.290097Z","shell.execute_reply":"2024-10-28T13:40:03.732447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Gender Distribution\n\nplt.figure(figsize=(10, 6))\nsns.countplot(x='Basic_Demos-Sex', data=train_data, palette='Set2')\nplt.title('Gender Distribution')\nplt.xlabel('Gender (0 = Male, 1 = Female)')\nplt.ylabel('Count')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-28T13:40:06.724853Z","iopub.execute_input":"2024-10-28T13:40:06.725266Z","iopub.status.idle":"2024-10-28T13:40:06.968349Z","shell.execute_reply.started":"2024-10-28T13:40:06.725228Z","shell.execute_reply":"2024-10-28T13:40:06.967150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Relationship Plot - Age vs SII\n\nplt.figure(figsize=(12, 8))\nsns.boxplot(x='sii', y='Basic_Demos-Age', data=train_data, palette='coolwarm')\nplt.title('Age vs SII')\nplt.xlabel('SII')\nplt.ylabel('Age')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-28T13:40:13.845938Z","iopub.execute_input":"2024-10-28T13:40:13.846334Z","iopub.status.idle":"2024-10-28T13:40:14.161314Z","shell.execute_reply.started":"2024-10-28T13:40:13.846298Z","shell.execute_reply":"2024-10-28T13:40:14.160006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Correlation Heatmap\n\nplt.figure(figsize=(15, 12))\nnumerical_features = train_data.select_dtypes(include=['float64', 'int64'])\ncorrelation_matrix = numerical_features.corr()\nsns.heatmap(correlation_matrix, annot=False, cmap='RdBu_r', linewidths=0.5)\nplt.title('Correlation Heatmap of Numerical Features')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-28T13:40:17.354745Z","iopub.execute_input":"2024-10-28T13:40:17.355513Z","iopub.status.idle":"2024-10-28T13:40:18.861918Z","shell.execute_reply.started":"2024-10-28T13:40:17.355471Z","shell.execute_reply":"2024-10-28T13:40:18.860716Z"},"trusted":true},"execution_count":null,"outputs":[]}]}