{"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"},{"sourceId":10018610,"sourceType":"datasetVersion","datasetId":6168788}],"dockerImageVersionId":30786,"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\nimport matplotlib.pyplot as plt\nimport seaborn as sns\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\nfrom pathlib import Path\n\ndataset_name = 'bmi-for-age-5-to-19-percentile-from-who-2007'\nkaggle_datadir = Path(f'/kaggle/input/{dataset_name}')\ndatadir = kaggle_datadir if kaggle_datadir.exists() else Path(f'./{dataset_name}')\n\nfor path in datadir.glob('**/*'):\n    if path.is_file():\n        print(path)\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-11-26T14:14:30.984966Z","iopub.execute_input":"2024-11-26T14:14:30.985285Z","iopub.status.idle":"2024-11-26T14:14:30.994200Z","shell.execute_reply.started":"2024-11-26T14:14:30.985254Z","shell.execute_reply":"2024-11-26T14:14:30.992834Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"bmi_boys = pd.read_excel(datadir/'bmi-boys-perc-who2007-exp.xlsx')\nbmi_girls = pd.read_excel(datadir/'bmi-girls-perc-who2007-exp.xlsx')\nbmi_girls.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-26T14:14:30.996038Z","iopub.execute_input":"2024-11-26T14:14:30.996484Z","iopub.status.idle":"2024-11-26T14:14:31.150819Z","shell.execute_reply.started":"2024-11-26T14:14:30.996433Z","shell.execute_reply":"2024-11-26T14:14:31.149617Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"bmi_boys['Sex'] = np.full(len(bmi_boys), fill_value='MALE')\nbmi_girls['Sex'] = np.full(len(bmi_girls), fill_value='FEMALE')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-26T14:14:31.152235Z","iopub.execute_input":"2024-11-26T14:14:31.152642Z","iopub.status.idle":"2024-11-26T14:14:31.159652Z","shell.execute_reply.started":"2024-11-26T14:14:31.152599Z","shell.execute_reply":"2024-11-26T14:14:31.158562Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"bmi = pd.concat([bmi_boys, bmi_girls])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-26T14:14:31.161181Z","iopub.execute_input":"2024-11-26T14:14:31.161859Z","iopub.status.idle":"2024-11-26T14:14:31.175233Z","shell.execute_reply.started":"2024-11-26T14:14:31.161806Z","shell.execute_reply":"2024-11-26T14:14:31.173938Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"bmi['Age'] = (bmi['Month'] - 1) // 12\nbmi","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-26T14:14:31.176800Z","iopub.execute_input":"2024-11-26T14:14:31.177272Z","iopub.status.idle":"2024-11-26T14:14:31.215120Z","shell.execute_reply.started":"2024-11-26T14:14:31.177220Z","shell.execute_reply":"2024-11-26T14:14:31.213923Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## BMI Categorization Recommendations for children and teens\n\n| Category | Percentile Range |\n| --- | --- |\n| Underweight | <5% |\n| Healthy weight | 5% - 85% |\n| At risk of overweight | 85% - 95% |\n| Overweight | >95% |\n\n[CDC Recommendation referenced by calculator.net](https://www.calculator.net/bmi-calculator.html)","metadata":{}},{"cell_type":"code","source":"g = sns.FacetGrid(bmi, col='Sex', height=8, aspect=0.6)\ng.map_dataframe(sns.lineplot, x='Age', y='P5', color='green', label='P5')\ng.map_dataframe(sns.lineplot, x='Age', y='P85', color='cyan', label='P85')\ng.map_dataframe(sns.lineplot, x='Age', y='P95', color='red', label='P95')\ng.set_ylabels('BMI')\ng.add_legend()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-26T14:14:31.216405Z","iopub.execute_input":"2024-11-26T14:14:31.216811Z","iopub.status.idle":"2024-11-26T14:14:33.800688Z","shell.execute_reply.started":"2024-11-26T14:14:31.216774Z","shell.execute_reply":"2024-11-26T14:14:33.799464Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Categorify BMI for [Child Mind Institude -- Problematic Internet Use](https://www.kaggle.com/competitions/child-mind-institute-problematic-internet-use)","metadata":{}},{"cell_type":"code","source":"bmi_map = bmi[['Age', 'Sex', 'P5', 'P85', 'P95']].groupby(by=['Sex', 'Age']).median()\nbmi_map.T","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-26T14:14:33.802042Z","iopub.execute_input":"2024-11-26T14:14:33.802374Z","iopub.status.idle":"2024-11-26T14:14:33.834637Z","shell.execute_reply.started":"2024-11-26T14:14:33.802341Z","shell.execute_reply":"2024-11-26T14:14:33.833526Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_cmi_train = pd.read_csv('/kaggle/input/child-mind-institute-problematic-internet-use/train.csv')\ndf_cmi_train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-26T14:14:33.835952Z","iopub.execute_input":"2024-11-26T14:14:33.836277Z","iopub.status.idle":"2024-11-26T14:14:33.905188Z","shell.execute_reply.started":"2024-11-26T14:14:33.836244Z","shell.execute_reply":"2024-11-26T14:14:33.903755Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def get_category_bmi(row: pd.Series, bmi: float):\n    if bmi > row['P95']:\n        return 3\n    elif bmi >= row['P85']:\n        return 2\n    elif bmi >= row['P5']:\n        return 1\n    elif np.isnan(bmi):\n        return -1\n    else:\n        return 0\n\n\ndef apply_func_category_bmi(row: pd.Series):\n    sex = 'MALE' if row['Basic_Demos-Sex'] == 0 else 'FEMALE'\n    age = row['Basic_Demos-Age']\n    bmi = row['Physical-BMI']\n    if age >= 18:\n        return get_category_bmi(bmi_map.loc[(sex, 18)], bmi)\n    elif age >= 5:\n        return get_category_bmi(bmi_map.loc[(sex, age)], bmi)\n    else:\n        return get_category_bmi(bmi_map.loc[(sex, 5)], bmi)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-26T14:37:07.179560Z","iopub.execute_input":"2024-11-26T14:37:07.180103Z","iopub.status.idle":"2024-11-26T14:37:07.190649Z","shell.execute_reply.started":"2024-11-26T14:37:07.180058Z","shell.execute_reply":"2024-11-26T14:37:07.188359Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"category_bmi = df_cmi_train.apply(apply_func_category_bmi, axis=1)\ncategory_bmi","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-26T14:37:07.193348Z","iopub.execute_input":"2024-11-26T14:37:07.193807Z","iopub.status.idle":"2024-11-26T14:37:07.636341Z","shell.execute_reply.started":"2024-11-26T14:37:07.193766Z","shell.execute_reply":"2024-11-26T14:37:07.634421Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_cmi_train['Physical-Category_BMI'] = category_bmi\ng = sns.catplot(data=df_cmi_train, x='Physical-Category_BMI', col='Basic_Demos-Sex',\n                hue='sii', kind='count', height=8, aspect=.8)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-26T14:37:07.639269Z","iopub.execute_input":"2024-11-26T14:37:07.639800Z","iopub.status.idle":"2024-11-26T14:37:08.500468Z","shell.execute_reply.started":"2024-11-26T14:37:07.639747Z","shell.execute_reply":"2024-11-26T14:37:08.498564Z"}},"outputs":[],"execution_count":null}]}