{"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":30775,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport polars as pl\nimport pandas as pd\nfrom sklearn.base import clone\nfrom copy import deepcopy\nimport optuna\nfrom scipy.optimize import minimize\nimport os\n\nimport re\nfrom colorama import Fore, Style\n\nfrom tqdm import tqdm\nfrom IPython.display import clear_output\nfrom concurrent.futures import ThreadPoolExecutor\n\nimport warnings\nwarnings.filterwarnings('ignore')\npd.options.display.max_columns = None\n\nimport lightgbm as lgb\nfrom catboost import CatBoostRegressor, CatBoostClassifier\nfrom xgboost import XGBRegressor\nfrom sklearn.ensemble import VotingRegressor\nfrom sklearn.model_selection import *\nfrom sklearn.metrics import *\n\nSEED = 42\nn_splits = 5","metadata":{"execution":{"iopub.status.busy":"2024-10-14T14:43:29.974768Z","iopub.execute_input":"2024-10-14T14:43:29.975211Z","iopub.status.idle":"2024-10-14T14:43:33.234670Z","shell.execute_reply.started":"2024-10-14T14:43:29.975168Z","shell.execute_reply":"2024-10-14T14:43:33.233742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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')\ntrain","metadata":{"execution":{"iopub.status.busy":"2024-10-14T14:43:33.236256Z","iopub.execute_input":"2024-10-14T14:43:33.236778Z","iopub.status.idle":"2024-10-14T14:43:33.416319Z","shell.execute_reply.started":"2024-10-14T14:43:33.236734Z","shell.execute_reply":"2024-10-14T14:43:33.415334Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA dataset train.csv","metadata":{}},{"cell_type":"code","source":"train=train.drop('id',axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-10-14T16:16:26.900540Z","iopub.execute_input":"2024-10-14T16:16:26.901097Z","iopub.status.idle":"2024-10-14T16:16:26.912682Z","shell.execute_reply.started":"2024-10-14T16:16:26.901043Z","shell.execute_reply":"2024-10-14T16:16:26.911539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Dưới đây là danh sách các đặc tính (features):\n\n1. ID của người tham gia - **id**: Mã định danh duy nhất cho từng người tham gia.\n1. Mùa đăng ký - **Basic_Demos-Enroll_Season**: Thời gian đăng ký tham gia theo mùa.\n1. Tuổi của người tham gia - **Basic_Demos-Age**: Tuổi của người tham gia nghiên cứu.\n1. Giới tính của người tham gia - **Basic_Demos-Sex**: Giới tính của người tham gia (nam hoặc nữ).\n1. Điểm CGAS (Thang đo đánh giá toàn diện cho trẻ em) - **CGAS-CGAS_Score**: Điểm số đánh giá mức độ hoạt động và hành vi của trẻ em.\n1. Chỉ số khối cơ thể (BMI) - **Physical-BMI**: Chỉ số khối cơ thể, tính bằng kg/m².\n1. Chiều cao (inch) - **Physical-Height**: Chiều cao của người tham gia tính bằng inch.\n1. Cân nặng (pound) - **Physical-Weight**: Cân nặng của người tham gia tính bằng pound.\n1. Vòng eo (inch) - **Physical-Waist_Circumference**: Chu vi vòng eo của người tham gia tính bằng inch.\n1. Huyết áp tâm trương (mmHg) - **Physical-Diastolic_BP**: Huyết áp tâm trương của người tham gia, đo bằng mmHg.\n1. Nhịp tim (nhịp/phút) - **Physical-HeartRate**: Nhịp tim của người tham gia tính bằng nhịp mỗi phút.\n1. Huyết áp tâm thu (mmHg) - **Physical-Systolic_BP**: Huyết áp tâm thu của người tham gia, đo bằng mmHg.\n1. Giai đoạn tối đa đạt được trong bài kiểm tra sức bền - **Fitness_Endurance-Max_Stage**: Giai đoạn cao nhất người tham gia đạt được trong bài kiểm tra thể lực.\n1. Thời gian hoàn thành chính xác: Phút - **Fitness_Endurance-Time_Mins**: Số phút hoàn thành bài kiểm tra sức bền.\n1. Thời gian hoàn thành chính xác: Giây - **Fitness_Endurance-Time_Sec**: Số giây hoàn thành bài kiểm tra sức bền.\n1. Tổng số lần gập bụng - **FGC-FGC_CU**: Tổng số lần gập bụng của người tham gia.\n1. Vùng thể lực của gập bụng - **FGC-FGC_CU_Zone**: Vùng chỉ định thể lực dựa trên số lần gập bụng.\n1. Tổng lực nắm tay (tay không thuận) - **FGC-FGC_GSND**: Tổng lực nắm tay của tay không thuận.\n1. Vùng thể lực lực nắm tay (tay không thuận) - **FGC-FGC_GSND_Zone**: Vùng thể lực dựa trên lực nắm tay của tay không thuận.\n1. Tổng lực nắm tay (tay thuận) - **FGC-FGC_GSD**: Tổng lực nắm tay của tay thuận.\n1. Vùng thể lực lực nắm tay (tay thuận) - **FGC-FGC_GSD_Zone**: Vùng thể lực dựa trên lực nắm tay của tay thuận.\n1. Tổng số lần hít đất - **FGC-FGC_PU**: Tổng số lần hít đất của người tham gia.\n1. Vùng thể lực của hít đất - **FGC-FGC_PU_Zone**: Vùng thể lực dựa trên số lần hít đất.\n1. Tổng số lần cúi và chạm (bên trái) - **FGC-FGC_SRL**: Tổng số lần cúi và chạm đất của người tham gia bằng chân trái.\n1. Vùng thể lực cúi và chạm (bên trái) - **FGC-FGC_SRL_Zone**: Vùng thể lực dựa trên khả năng cúi và chạm của chân trái.\n1. Tổng số lần cúi và chạm (bên phải) - **FGC-FGC_SRR**: Tổng số lần cúi và chạm đất của người tham gia bằng chân phải.\n1. Vùng thể lực cúi và chạm (bên phải) - **FGC-FGC_SRR_Zone**: Vùng thể lực dựa trên khả năng cúi và chạm của chân phải.\n1. Tổng số lần nâng thân trên - **FGC-FGC_TL**: Tổng số lần nâng thân trên trong bài tập thân trên.\n1. Vùng thể lực nâng thân trên - **FGC-FGC_TL_Zone**: Vùng thể lực dựa trên khả năng nâng thân trên.\n1. Cấp độ hoạt động - **BIA-BIA_Activity_Level_num**: Chỉ số đánh giá cấp độ hoạt động thể chất của người tham gia.\n1. Hàm lượng khoáng xương - **BIA-BIA_BMC**: Lượng khoáng xương của người tham gia.\n1. Tỷ lệ trao đổi chất cơ bản - **BIA-BIA_BMR**: Lượng năng lượng cơ bản cần thiết cho các chức năng của cơ thể.\n1. Năng lượng tiêu thụ hàng ngày - **BIA-BIA_DEE**: Tổng lượng năng lượng tiêu thụ trong một ngày.\n1. Khối lượng không mỡ - **BIA-BIA_FFM**: Tổng khối lượng không mỡ trong cơ thể.\n1. Chỉ số khối lượng không mỡ - **BIA-BIA_FFMI**: Chỉ số đo lường lượng cơ bắp.\n1. Chỉ số khối lượng mỡ - **BIA-BIA_FMI**: Chỉ số đo lượng mỡ cơ thể.\n1. Tỷ lệ mỡ cơ thể - **BIA-BIA_Fat**: Tỷ lệ phần trăm mỡ cơ thể.\n1. Khung cơ thể - **BIA-BIA_Frame_num**: Kích thước và khung xương của cơ thể.\n1. Nước nội bào - **BIA-BIA_ICW**: Lượng nước bên trong tế bào.\n1. Khối lượng cơ nạc khô - **BIA-BIA_LDM**: Lượng cơ nạc không chứa mỡ.\n1. Tổng khối lượng cơ nạc - **BIA-BIA_LST**: Lượng cơ nạc toàn cơ thể.\n1. Khối lượng cơ xương - **BIA-BIA_SMM**: Tổng khối lượng cơ xương trong cơ thể.\n1. Tổng lượng nước cơ thể - **BIA-BIA_TBW**: Tổng lượng nước trong cơ thể.\n1. Điểm hoạt động tổng quan (dành cho thanh thiếu niên) - **PAQ_A-PAQ_A_Total**: Điểm tổng quát về mức độ hoạt động của thanh thiếu niên.\n1. Điểm hoạt động tổng quan (dành cho trẻ em) - **PAQ_C-PAQ_C_Total**: Điểm tổng quát về mức độ hoạt động của trẻ em.\n1. Con của bạn thường không tuân thủ giới hạn thời gian trực tuyến mà bạn đặt ra? - **PCIAT-PCIAT_01**: Câu hỏi về thói quen sử dụng Internet của con cái.\n1. Con của bạn thường bỏ qua công việc nhà để dành nhiều thời gian hơn trên mạng? - **PCIAT-PCIAT_02**: Thói quen sử dụng Internet và trách nhiệm gia đình.\n1. Con của bạn thường thích ở trên mạng hơn là ở bên gia đình? - **PCIAT-PCIAT_03**: Sự lựa chọn giữa thời gian với gia đình và Internet.\n1. Con của bạn thường tạo mối quan hệ mới với người dùng trực tuyến khác? - **PCIAT-PCIAT_04**: Sự phát triển mối quan hệ trên mạng.\n1. Bạn thường phàn nàn về lượng thời gian con bạn dành trên mạng? - **PCIAT-PCIAT_05**: Mức độ lo lắng của cha mẹ về việc sử dụng Internet của con cái.\n1. Thành tích học tập của con bạn có bị ảnh hưởng bởi thời gian trực tuyến không? - **PCIAT-PCIAT_06**: Ảnh hưởng của Internet đến học tập.\n1. Con bạn thường kiểm tra email trước khi làm việc khác? - **PCIAT-PCIAT_07**: Thói quen ưu tiên việc trực tuyến.\n1. Con bạn có trở nên xa lánh kể từ khi khám phá ra Internet? - **PCIAT-PCIAT_08**: Ảnh hưởng của Internet đến giao tiếp xã hội.\n1. Con bạn có trở nên phòng thủ hoặc bí mật khi được hỏi về việc làm gì trên mạng? - **PCIAT-PCIAT_09**: Phản ứng của con cái khi được hỏi về thói quen trực tuyến.\n1. Bạn có bắt gặp con mình lén lút sử dụng Internet trái ý muốn của bạn? - **PCIAT-PCIAT_10**: Thói quen lén lút sử dụng Internet.\n**Con bạn có thường chơi máy tính một mình trong phòng","metadata":{}},{"cell_type":"code","source":"pd.set_option('display.max_rows', None)\npd.set_option('display.max_columns', None)\nprint('----type-----')\ntrain.dtypes","metadata":{"execution":{"iopub.status.busy":"2024-10-14T16:16:31.983550Z","iopub.execute_input":"2024-10-14T16:16:31.984366Z","iopub.status.idle":"2024-10-14T16:16:31.996873Z","shell.execute_reply.started":"2024-10-14T16:16:31.984321Z","shell.execute_reply":"2024-10-14T16:16:31.995737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2024-10-14T16:16:48.946829Z","iopub.execute_input":"2024-10-14T16:16:48.947605Z","iopub.status.idle":"2024-10-14T16:16:48.953768Z","shell.execute_reply.started":"2024-10-14T16:16:48.947553Z","shell.execute_reply":"2024-10-14T16:16:48.952751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"count the number of adolescents and children based on PCA_{C/A}","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('-----describe----')\ntrain.describe().T","metadata":{"execution":{"iopub.status.busy":"2024-10-14T16:16:53.357049Z","iopub.execute_input":"2024-10-14T16:16:53.357432Z","iopub.status.idle":"2024-10-14T16:16:53.521944Z","shell.execute_reply.started":"2024-10-14T16:16:53.357398Z","shell.execute_reply":"2024-10-14T16:16:53.520989Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['sii'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-10-14T16:17:06.046605Z","iopub.execute_input":"2024-10-14T16:17:06.047016Z","iopub.status.idle":"2024-10-14T16:17:06.056708Z","shell.execute_reply.started":"2024-10-14T16:17:06.046977Z","shell.execute_reply":"2024-10-14T16:17:06.055477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['sii'].value_counts().sum()","metadata":{"execution":{"iopub.status.busy":"2024-10-14T16:17:45.983153Z","iopub.execute_input":"2024-10-14T16:17:45.983934Z","iopub.status.idle":"2024-10-14T16:17:45.991481Z","shell.execute_reply.started":"2024-10-14T16:17:45.983893Z","shell.execute_reply":"2024-10-14T16:17:45.990320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.dropna(subset='sii')\ntrain.shape","metadata":{"execution":{"iopub.status.busy":"2024-10-14T16:18:37.653424Z","iopub.execute_input":"2024-10-14T16:18:37.653863Z","iopub.status.idle":"2024-10-14T16:18:37.664548Z","shell.execute_reply.started":"2024-10-14T16:18:37.653819Z","shell.execute_reply":"2024-10-14T16:18:37.663502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Count the percentage of nan in each column","metadata":{}},{"cell_type":"code","source":"train.isna().mean()","metadata":{"execution":{"iopub.status.busy":"2024-10-14T16:20:23.001145Z","iopub.execute_input":"2024-10-14T16:20:23.002343Z","iopub.status.idle":"2024-10-14T16:20:23.017607Z","shell.execute_reply.started":"2024-10-14T16:20:23.002279Z","shell.execute_reply":"2024-10-14T16:20:23.016458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Drop all columns that have more than 80% missing values","metadata":{}},{"cell_type":"code","source":"def drop_sparse_columns(df: pd.DataFrame, threshold: float) -> pd.DataFrame:\n\n\n    ### BEGIN SOLUTION\n    missing_percentage = df.isna().mean()     \n    # Giữ lại các cột mà tỷ lệ phần trăm giá trị bị thiếu nhỏ hơn hoặc bằng threshold\n    retained_columns = missing_percentage[missing_percentage <= threshold].index\n    return df[retained_columns]\n    ### END SOLUTION\n    pass\n# Nếu cột có phần trăm giá trị bị thiếu > 60% thì sẽ bị loại bỏ\nthreshold_missing = 0.8\ntrain_modify = drop_sparse_columns(train, threshold_missing)\ntrain_modify.isna().mean()","metadata":{"execution":{"iopub.status.busy":"2024-10-14T16:20:31.151444Z","iopub.execute_input":"2024-10-14T16:20:31.151851Z","iopub.status.idle":"2024-10-14T16:20:31.170916Z","shell.execute_reply.started":"2024-10-14T16:20:31.151813Z","shell.execute_reply":"2024-10-14T16:20:31.169926Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_modify.shape","metadata":{"execution":{"iopub.status.busy":"2024-10-14T16:20:51.634297Z","iopub.execute_input":"2024-10-14T16:20:51.634696Z","iopub.status.idle":"2024-10-14T16:20:51.642251Z","shell.execute_reply.started":"2024-10-14T16:20:51.634659Z","shell.execute_reply":"2024-10-14T16:20:51.640937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Count the number of nan value in each row","metadata":{}},{"cell_type":"code","source":"# Calculate the 80% threshold\nthreshold = 0.8 * len(train_modify.columns)\n\n# Drop rows where more than 80% of the values are missing\ntrain_modify = train_modify.dropna(thresh=len(train_modify.columns) - threshold)","metadata":{"execution":{"iopub.status.busy":"2024-10-14T16:22:51.342009Z","iopub.execute_input":"2024-10-14T16:22:51.342445Z","iopub.status.idle":"2024-10-14T16:22:51.354834Z","shell.execute_reply.started":"2024-10-14T16:22:51.342407Z","shell.execute_reply":"2024-10-14T16:22:51.353763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_modify.shape","metadata":{"execution":{"iopub.status.busy":"2024-10-14T16:23:48.558766Z","iopub.execute_input":"2024-10-14T16:23:48.559204Z","iopub.status.idle":"2024-10-14T16:23:48.566253Z","shell.execute_reply.started":"2024-10-14T16:23:48.559165Z","shell.execute_reply":"2024-10-14T16:23:48.565144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('----type-----')\ntrain_modify.dtypes","metadata":{"execution":{"iopub.status.busy":"2024-10-14T16:28:49.537186Z","iopub.execute_input":"2024-10-14T16:28:49.537926Z","iopub.status.idle":"2024-10-14T16:28:49.547984Z","shell.execute_reply.started":"2024-10-14T16:28:49.537882Z","shell.execute_reply":"2024-10-14T16:28:49.546954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('-----describe----')\ntrain_modify.describe().T","metadata":{"execution":{"iopub.status.busy":"2024-10-14T16:28:44.637196Z","iopub.execute_input":"2024-10-14T16:28:44.637610Z","iopub.status.idle":"2024-10-14T16:28:44.790439Z","shell.execute_reply.started":"2024-10-14T16:28:44.637570Z","shell.execute_reply":"2024-10-14T16:28:44.789206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Chú ý: Physical_weight có min = 0 dẫn đến physical_BMI=0","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n","metadata":{"execution":{"iopub.status.busy":"2024-10-14T16:40:15.675051Z","iopub.execute_input":"2024-10-14T16:40:15.675453Z","iopub.status.idle":"2024-10-14T16:40:15.680420Z","shell.execute_reply.started":"2024-10-14T16:40:15.675417Z","shell.execute_reply":"2024-10-14T16:40:15.679234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot a histogram for a specific column, e.g., 'column_name'\ntrain_modify['Physical-Weight'].hist(bins=20, edgecolor='black')\n\n# Add labels and title\nplt.xlabel('Values')\nplt.ylabel('Frequency')\nplt.title('Histogram of column_name')\n\n# Show the plot\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-14T16:41:52.338529Z","iopub.execute_input":"2024-10-14T16:41:52.338961Z","iopub.status.idle":"2024-10-14T16:41:52.674508Z","shell.execute_reply.started":"2024-10-14T16:41:52.338919Z","shell.execute_reply":"2024-10-14T16:41:52.673325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_modify[train_modify['Physical-Weight']==0]","metadata":{"execution":{"iopub.status.busy":"2024-10-14T16:45:21.215429Z","iopub.execute_input":"2024-10-14T16:45:21.216365Z","iopub.status.idle":"2024-10-14T16:45:21.427120Z","shell.execute_reply.started":"2024-10-14T16:45:21.216321Z","shell.execute_reply":"2024-10-14T16:45:21.426066Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_modify = train_modify[train_modify['Physical-Weight']!=0]\ntrain_modify.shape","metadata":{"execution":{"iopub.status.busy":"2024-10-14T16:46:45.924885Z","iopub.execute_input":"2024-10-14T16:46:45.925320Z","iopub.status.idle":"2024-10-14T16:46:45.934826Z","shell.execute_reply.started":"2024-10-14T16:46:45.925280Z","shell.execute_reply":"2024-10-14T16:46:45.933770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('-----describe----')\ntrain_modify.describe().T","metadata":{"execution":{"iopub.status.busy":"2024-10-14T16:51:33.232670Z","iopub.execute_input":"2024-10-14T16:51:33.233136Z","iopub.status.idle":"2024-10-14T16:51:33.385423Z","shell.execute_reply.started":"2024-10-14T16:51:33.233094Z","shell.execute_reply":"2024-10-14T16:51:33.384424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"LinearFeatures= []\nCatergoricalFeatures =[]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_modify.to_csv('filename.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-10-14T17:10:11.371470Z","iopub.execute_input":"2024-10-14T17:10:11.372238Z","iopub.status.idle":"2024-10-14T17:10:11.516253Z","shell.execute_reply.started":"2024-10-14T17:10:11.372193Z","shell.execute_reply":"2024-10-14T17:10:11.515150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2024-10-14T17:50:46.401255Z","iopub.execute_input":"2024-10-14T17:50:46.402037Z","iopub.status.idle":"2024-10-14T17:50:46.665386Z","shell.execute_reply.started":"2024-10-14T17:50:46.401980Z","shell.execute_reply":"2024-10-14T17:50:46.664384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 6))\n# Create the count plot\nax = sns.countplot(x='sii', data=train_modify)\n\n# Add count labels on top of the bars\nfor p in ax.patches:\n    ax.annotate(f'{int(p.get_height())}', \n                (p.get_x() + p.get_width() / 2., p.get_height()), \n                ha='center', va='baseline', \n                fontsize=12, color='black', \n                xytext=(0, 5),  # Offset text\n                textcoords='offset points')\n\n# Add labels and title\nplt.xlabel('SII')\nplt.ylabel('Count')\nplt.title('Count of SII')\n\n# Show the plot\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-14T18:01:09.569831Z","iopub.execute_input":"2024-10-14T18:01:09.570280Z","iopub.status.idle":"2024-10-14T18:01:09.787138Z","shell.execute_reply.started":"2024-10-14T18:01:09.570238Z","shell.execute_reply":"2024-10-14T18:01:09.785973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.histplot(x=train['Basic_Demos-Age'], kde = True, hue = train_modify['sii'] ,  palette='viridis')","metadata":{"execution":{"iopub.status.busy":"2024-10-14T18:52:34.226875Z","iopub.execute_input":"2024-10-14T18:52:34.228174Z","iopub.status.idle":"2024-10-14T18:52:34.840550Z","shell.execute_reply.started":"2024-10-14T18:52:34.228121Z","shell.execute_reply":"2024-10-14T18:52:34.839502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.histplot(x=train_modify['Basic_Demos-Age'], hue = train_modify['sii'], kde = True,  palette='viridis')","metadata":{"execution":{"iopub.status.busy":"2024-10-14T17:57:13.680557Z","iopub.execute_input":"2024-10-14T17:57:13.680958Z","iopub.status.idle":"2024-10-14T17:57:14.785599Z","shell.execute_reply.started":"2024-10-14T17:57:13.680923Z","shell.execute_reply":"2024-10-14T17:57:14.784510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Nếu adolecent thì trung bình là ","metadata":{}},{"cell_type":"code","source":"sns.countplot(x=train_modify['Basic_Demos-Sex'], hue=train_modify['sii']);","metadata":{"execution":{"iopub.status.busy":"2024-10-14T18:03:35.244455Z","iopub.execute_input":"2024-10-14T18:03:35.244894Z","iopub.status.idle":"2024-10-14T18:03:35.462205Z","shell.execute_reply.started":"2024-10-14T18:03:35.244856Z","shell.execute_reply":"2024-10-14T18:03:35.460975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Plot physical statastics","metadata":{}},{"cell_type":"code","source":"cols = ['CGAS-CGAS_Score','Physical-BMI', 'Physical-Height', 'Physical-Weight', 'Physical-Diastolic_BP', 'Physical-HeartRate', \n'Physical-Systolic_BP']\n\n# Tạo subplot kích thước 3 x 3\nfig, ax = plt.subplots(3, 3, figsize=(18, 18))\nprint(fig)\nprint(ax)\nindex = 0\n\n### BEGIN SOLUTION\nax = ax.flatten()\nindex = 0\nfor i, col in enumerate(cols):\n    sns.histplot(x=train_modify[col], hue = train_modify['sii'], kde = True,  palette='viridis', ax=ax[i])\n    ax[i].set_title(f'Count of {col} by sii')\n    ax[i].set_xlabel(col)\n    ax[i].set_ylabel('Count')\n    index += 1\n### END SOLUTION\n# Xóa ô cuối cùng vì chỉ có 5 cột cần hiển thị\nax.flat[-2].set_visible(False)\nax.flat[-1].set_visible(False)\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2024-10-14T18:15:30.356248Z","iopub.execute_input":"2024-10-14T18:15:30.356687Z","iopub.status.idle":"2024-10-14T18:15:36.155630Z","shell.execute_reply.started":"2024-10-14T18:15:30.356650Z","shell.execute_reply":"2024-10-14T18:15:36.154628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols = ['Pclass', 'Sex', 'SibSp', 'Parch', 'Embarked']\n\n# Tạo subplot kích thước 2 x 3\nfig, ax = plt.subplots(2, 3, figsize=(18, 18))\nprint(fig)\nprint(ax)\nindex = 0\n\n### BEGIN SOLUTION\nax = ax.flatten()\nindex = 0\nfor i, col in enumerate(cols):\n    sns.countplot(x=df[col], hue=df['Survived'], ax=ax[i])\n    ax[i].set_title(f'Count of {col} by Survived')\n    ax[i].set_xlabel(col)\n    ax[i].set_ylabel('Count')\n    index += 1\n### END SOLUTION\n\n# Xóa ô cuối cùng vì chỉ có 5 cột cần hiển thị\nax.flat[-1].set_visible(False)\nplt.tight_layout()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Plot FGC Statistics","metadata":{}},{"cell_type":"markdown","source":"### Linear features","metadata":{}},{"cell_type":"code","source":"cols = ['FGC-FGC_CU',\n'FGC-FGC_GSND',\n'FGC-FGC_GSD',\n'FGC-FGC_PU',\n'FGC-FGC_SRL',\n'FGC-FGC_SRR',\n'FGC-FGC_TL']\n\nfig, ax = plt.subplots(4, 2, figsize=(14, 14))\nprint(fig)\nprint(ax)\nindex = 0\n\nax = ax.flatten()\nindex = 0\nfor i, col in enumerate(cols):\n    sns.histplot(x=train_modify[col], hue = train_modify['sii'], kde = True,  palette='viridis', ax=ax[i])\n    ax[i].set_title(f'Histogram of {col} by sii')\n    ax[i].set_xlabel(col)\n    ax[i].set_ylabel('Histogram')\n    index += 1\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2024-10-14T18:28:17.804540Z","iopub.execute_input":"2024-10-14T18:28:17.805337Z","iopub.status.idle":"2024-10-14T18:28:23.292209Z","shell.execute_reply.started":"2024-10-14T18:28:17.805291Z","shell.execute_reply":"2024-10-14T18:28:23.291201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Categorical features","metadata":{}},{"cell_type":"code","source":"cols = ['FGC-FGC_CU_Zone',\n'FGC-FGC_GSND_Zone',\n'FGC-FGC_GSD_Zone',\n'FGC-FGC_PU_Zone',\n'FGC-FGC_SRL_Zone',\n'FGC-FGC_SRR_Zone',\n'FGC-FGC_TL_Zone']\n\n# Create subplots\nfig, ax = plt.subplots(4, 2, figsize=(14, 14))\nax = ax.flatten()\n\nfor i, col in enumerate(cols):\n    # Create the count plot\n    sns.countplot(x=train_modify[col], hue=train_modify['sii'], palette='viridis', ax=ax[i])\n    ax[i].set_title(f'Histogram of {col} by sii')\n    ax[i].set_xlabel(col)\n    ax[i].set_ylabel('Count')\n    \n    # Calculate total counts of 0 and 1\n    total_counts = train_modify[col].value_counts()\n    \n    # Annotate total counts on top of each bar\n    for p in ax[i].patches:\n        # Get the height of the bar (count)\n        height = int(p.get_height())\n        \n        # Annotate the total counts of 0 and 1, if they exist\n        if p.get_x() + p.get_width() / 2.0 in total_counts.index:\n            total_count_label = total_counts.get(p.get_x() + p.get_width() / 2.0, 0)\n            ax[i].annotate(f'{total_count_label}', \n                           (p.get_x() + p.get_width() / 2., height), \n                           ha='center', va='baseline', \n                           fontsize=10, color='black', \n                           xytext=(0, 5),  # Offset text\n                           textcoords='offset points')\n\n# Hide the last subplot if there are less than 8 columns\nif len(cols) < 8:\n    ax.flat[-1].set_visible(False)\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-14T18:32:04.630150Z","iopub.execute_input":"2024-10-14T18:32:04.630595Z","iopub.status.idle":"2024-10-14T18:32:07.349861Z","shell.execute_reply.started":"2024-10-14T18:32:04.630526Z","shell.execute_reply":"2024-10-14T18:32:07.348694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Plot BIA-BIA","metadata":{}},{"cell_type":"code","source":"cols = ['BIA-BIA_Activity_Level_num',\n'BIA-BIA_BMC',\n'BIA-BIA_BMI',\n'BIA-BIA_BMR',\n'BIA-BIA_DEE',\n'BIA-BIA_ECW',\n'BIA-BIA_FFM',\n'BIA-BIA_FFMI',\n'BIA-BIA_FMI',\n'BIA-BIA_Fat',\n'BIA-BIA_Frame_num',\n'BIA-BIA_ICW',\n'BIA-BIA_LDM',\n'BIA-BIA_LST',\n'BIA-BIA_SMM',\n'BIA-BIA_TBW']\n\nfig, ax = plt.subplots(6, 3, figsize=(14, 14))\nprint(fig)\nprint(ax)\nindex = 0\n\nax = ax.flatten()\nindex = 0\nfor i, col in enumerate(cols):\n    sns.histplot(x=train_modify[col], hue = train_modify['sii'], kde = True,  palette='viridis', ax=ax[i])\n    ax[i].set_title(f'Histogram of {col} by sii')\n    ax[i].set_xlabel(col)\n    ax[i].set_ylabel('Histogram')\n    index += 1\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2024-10-14T19:17:16.412871Z","iopub.execute_input":"2024-10-14T19:17:16.414004Z","iopub.status.idle":"2024-10-14T19:21:16.682013Z","shell.execute_reply.started":"2024-10-14T19:17:16.413955Z","shell.execute_reply":"2024-10-14T19:21:16.680734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import itertools\ncolumns_list = train.select_dtypes(include='number').columns.tolist()\npairs = itertools.combinations(columns_list, 2)\ncorrelation_list = []\nfor pair in (pairs):\n     correlation_list.append([pair[0], pair[1],train[pair[0]].corr(train[pair[1]], method='spearman')] )\ncorrelation_train = pd.DataFrame(correlation_list)\ncorrelation_train.sort_values(by=[2], ascending=False)\ncorrelation_train","metadata":{"execution":{"iopub.status.busy":"2024-10-14T14:43:35.033441Z","iopub.status.idle":"2024-10-14T14:43:35.033870Z","shell.execute_reply.started":"2024-10-14T14:43:35.033670Z","shell.execute_reply":"2024-10-14T14:43:35.033690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import plotly.express as px\nimport plotly.graph_objects as go\nfig = go.Figure()\nfig = px.scatter_3d(x= dataset[\"feature_1\"],\ny= dataset[\"feature_2\"],\nz= dataset[\"sii\"],\ntitle=\"3D scatter plot\",\nlabels={\"x\": \"feature_1\",\n\"y\": \"feature_2\",\n\"z\": \"feature_3\"}\n)\nfig.update_traces(marker=dict(size=1.5))\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-14T14:43:35.036044Z","iopub.status.idle":"2024-10-14T14:43:35.036605Z","shell.execute_reply.started":"2024-10-14T14:43:35.036322Z","shell.execute_reply":"2024-10-14T14:43:35.036350Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plot for distribution\nimport seaborn as sns\nsns.violinplot(data=train, orient='v', bw=0.05, cut=0);\n","metadata":{"execution":{"iopub.status.busy":"2024-10-14T14:43:35.037812Z","iopub.status.idle":"2024-10-14T14:43:35.038385Z","shell.execute_reply.started":"2024-10-14T14:43:35.038102Z","shell.execute_reply":"2024-10-14T14:43:35.038129Z"},"trusted":true},"execution_count":null,"outputs":[]}]}