{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<font color='blue' size=10><center><u> IDAO Insomnia Prediction </u></center> </font>","metadata":{}},{"cell_type":"markdown","source":"\n\n   <img src='https://www.evimdeterapi.com/blog/wp-content/uploads/insomnia-belirtileri.jpg' width='800'>","metadata":{}},{"cell_type":"markdown","source":"<a id=\"TOC\"></a>\n\n<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n<h1 class=\"list-group-item list-group-item-action active\" data-toggle=\"list\" style='background:blue; border:0' role=\"tab\" aria-controls=\"home\"><center>Table of Content</center></h1>\n    ","metadata":{}},{"cell_type":"markdown","source":"1. [Python Libary](#1)\n2. [Read Data and Columns](#2)\n3. [Feature Analysis](#3)\n4.  [Missing Value](#4)\n5. [Modeling](#5)","metadata":{}},{"cell_type":"markdown","source":"<a id=\"1\"></a>\n<h1 style='background:blue; border:0; color:white'><center>Libraries</center></h1>\n\n<a href=\"#TOC\" class=\"btn btn-primary btn-sm\" role=\"button\" aria-pressed=\"true\" style=\"color:white\" data-toggle=\"popover\">Go to TOC</a>","metadata":{}},{"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 seaborn as sns\nimport matplotlib.pyplot as plt\n\nfrom sklearn.preprocessing import LabelEncoder\nfrom collections import Counter\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\n\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n\n\nfrom sklearn.linear_model import ElasticNet, Lasso,  BayesianRidge, LassoLarsIC\nfrom sklearn.ensemble import RandomForestRegressor,  GradientBoostingRegressor\nfrom sklearn.kernel_ridge import KernelRidge\nfrom sklearn.pipeline import make_pipeline\nfrom sklearn.preprocessing import RobustScaler\nfrom sklearn.base import BaseEstimator, TransformerMixin, RegressorMixin, clone\nfrom sklearn.model_selection import KFold, cross_val_score, train_test_split\nfrom sklearn.metrics import mean_squared_error\nimport xgboost as xgb\nimport lightgbm as lgb\nfrom sklearn.preprocessing import LabelEncoder\nfrom lightgbm import LGBMRegressor\nfrom xgboost import XGBRegressor\nfrom sklearn.linear_model import Ridge, RidgeCV\nfrom sklearn.kernel_ridge import KernelRidge\nfrom sklearn.svm import SVR\nfrom mlxtend.regressor import StackingCVRegressor\n\n\n# Stats\nfrom scipy.stats import skew, norm\nfrom scipy.special import boxcox1p\nfrom scipy.stats import boxcox_normmax\n\n\n\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":"2022-07-20T19:57:54.958243Z","iopub.execute_input":"2022-07-20T19:57:54.958871Z","iopub.status.idle":"2022-07-20T19:57:57.568700Z","shell.execute_reply.started":"2022-07-20T19:57:54.958772Z","shell.execute_reply":"2022-07-20T19:57:57.567868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"2\"></a>\n<h1 style='background:blue; border:0; color:white'><center>Read Data and Columns</center></h1>\n\n<a href=\"#TOC\" class=\"btn btn-primary btn-sm\" role=\"button\" aria-pressed=\"true\" style=\"color:white\" data-toggle=\"popover\">Go to TOC</a>","metadata":{}},{"cell_type":"code","source":"train=pd.read_csv(\"/kaggle/input/idao-2022-bootcamp-insomnia/TRAIN.csv\")\ntest=pd.read_csv(\"/kaggle/input/idao-2022-bootcamp-insomnia/TEST.csv\")\nsubmission=pd.read_csv(\"/kaggle/input/idao-2022-bootcamp-insomnia/sample_submission.csv\")\n","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:57:57.570655Z","iopub.execute_input":"2022-07-20T19:57:57.571276Z","iopub.status.idle":"2022-07-20T19:57:57.806147Z","shell.execute_reply.started":"2022-07-20T19:57:57.571233Z","shell.execute_reply":"2022-07-20T19:57:57.805221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Columns**","metadata":{}},{"cell_type":"markdown","source":"\n- id - person's identificator\n- age - person's age in years\n- weight- person's weight in kilograms\n- height- person's height in centimeters\n- sex - person's sex\n- stress - level of stress during last month (1, 2, 3 - higher values correspond to larger stress)\n- doctor - relative number of visits to doctor previously (1, 2, 3 - higher values correspond to greater number of visits)\n- sport - is person physically active or not (binary)\n- pernicious_1 - does person have some bad habit or not (binary)\n- pernicious_2 - does person have some another bad habit or not (binary)\n- ubp/lbp - upper/lower blood pressure in mmHg\n- insomnia - target, does person have sleep disorder or not (binary)","metadata":{}},{"cell_type":"markdown","source":"<a id=\"3\"></a>\n<h1 style='background:blue; border:0; color:white'><center>Feature Analysis</center></h1>\n\n<a href=\"#TOC\" class=\"btn btn-primary btn-sm\" role=\"button\" aria-pressed=\"true\" style=\"color:white\" data-toggle=\"popover\">Go to TOC</a>","metadata":{}},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:57:57.807469Z","iopub.execute_input":"2022-07-20T19:57:57.808420Z","iopub.status.idle":"2022-07-20T19:57:57.830054Z","shell.execute_reply.started":"2022-07-20T19:57:57.808377Z","shell.execute_reply":"2022-07-20T19:57:57.829224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:57:57.832396Z","iopub.execute_input":"2022-07-20T19:57:57.833339Z","iopub.status.idle":"2022-07-20T19:57:57.851969Z","shell.execute_reply.started":"2022-07-20T19:57:57.833297Z","shell.execute_reply":"2022-07-20T19:57:57.851195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:57:57.852931Z","iopub.execute_input":"2022-07-20T19:57:57.853493Z","iopub.status.idle":"2022-07-20T19:57:57.862473Z","shell.execute_reply.started":"2022-07-20T19:57:57.853462Z","shell.execute_reply":"2022-07-20T19:57:57.861675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:57:57.863940Z","iopub.execute_input":"2022-07-20T19:57:57.864469Z","iopub.status.idle":"2022-07-20T19:57:57.872735Z","shell.execute_reply.started":"2022-07-20T19:57:57.864430Z","shell.execute_reply":"2022-07-20T19:57:57.871958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:57:57.874185Z","iopub.execute_input":"2022-07-20T19:57:57.874651Z","iopub.status.idle":"2022-07-20T19:57:57.882345Z","shell.execute_reply.started":"2022-07-20T19:57:57.874613Z","shell.execute_reply":"2022-07-20T19:57:57.881763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target=train[\"insomnia\"]","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:57:57.883287Z","iopub.execute_input":"2022-07-20T19:57:57.883743Z","iopub.status.idle":"2022-07-20T19:57:57.894204Z","shell.execute_reply.started":"2022-07-20T19:57:57.883712Z","shell.execute_reply":"2022-07-20T19:57:57.893649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**we are going to reconfigure sex columns. sex columns to be 0 and 1 not 1 and 2**","metadata":{}},{"cell_type":"code","source":"combine_list=[train]\nfor combine in combine_list:\n    combine['sex'] -= 1","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:57:57.895406Z","iopub.execute_input":"2022-07-20T19:57:57.895852Z","iopub.status.idle":"2022-07-20T19:57:57.908615Z","shell.execute_reply.started":"2022-07-20T19:57:57.895813Z","shell.execute_reply":"2022-07-20T19:57:57.907920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def correlation_heatmap(df):\n    _ , ax = plt.subplots(figsize =(14, 12))\n    colormap = sns.diverging_palette(220, 10, as_cmap = True)\n    \n    _ = sns.heatmap(\n        df.corr(), \n        cmap = colormap,\n        square=True, \n        cbar_kws={'shrink':.9 }, \n        ax=ax,\n        annot=True, \n        linewidths=0.1,vmax=1.0, linecolor='white',\n        annot_kws={'fontsize':12 }\n    )\n    \n    plt.title('Pearson Correlation of Features', y=1.05, size=15)\n\ncorrelation_heatmap(train)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:57:57.911824Z","iopub.execute_input":"2022-07-20T19:57:57.912045Z","iopub.status.idle":"2022-07-20T19:57:59.265670Z","shell.execute_reply.started":"2022-07-20T19:57:57.912021Z","shell.execute_reply":"2022-07-20T19:57:59.264871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**when we look at corelation heatmap, \"age\",\"weight\",\"stress\" columns is bigest efect to target column.**","metadata":{}},{"cell_type":"code","source":"import_feature=[\"age\",\"weight\",\"stress\"]","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:57:59.267373Z","iopub.execute_input":"2022-07-20T19:57:59.267700Z","iopub.status.idle":"2022-07-20T19:57:59.272348Z","shell.execute_reply.started":"2022-07-20T19:57:59.267660Z","shell.execute_reply":"2022-07-20T19:57:59.271263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**we divide to 2 as numerical and categorical for we easier to visualize data**","metadata":{}},{"cell_type":"code","source":"numerical_columns=['age', 'weight','height','stress','ubp','lbp']\ncategorical_columns=['sex','sport','pernicious_1','pernicious_2','insomnia']","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:57:59.273801Z","iopub.execute_input":"2022-07-20T19:57:59.274261Z","iopub.status.idle":"2022-07-20T19:57:59.282652Z","shell.execute_reply.started":"2022-07-20T19:57:59.274185Z","shell.execute_reply":"2022-07-20T19:57:59.282086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**we make boxplot on numerical values for  see outlier values**","metadata":{}},{"cell_type":"code","source":"for i in numerical_columns:\n    plt.figure(figsize=(5,5))\n    sns.boxplot(x=train[i])","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:57:59.283701Z","iopub.execute_input":"2022-07-20T19:57:59.284048Z","iopub.status.idle":"2022-07-20T19:58:00.304937Z","shell.execute_reply.started":"2022-07-20T19:57:59.284011Z","shell.execute_reply":"2022-07-20T19:58:00.304076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.boxenplot(x=\"insomnia\",y=\"lbp\",data=train)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:58:00.308527Z","iopub.execute_input":"2022-07-20T19:58:00.309152Z","iopub.status.idle":"2022-07-20T19:58:00.532976Z","shell.execute_reply.started":"2022-07-20T19:58:00.309117Z","shell.execute_reply":"2022-07-20T19:58:00.532388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.boxenplot(x=\"insomnia\",y=\"ubp\",data=train)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:58:00.534211Z","iopub.execute_input":"2022-07-20T19:58:00.534522Z","iopub.status.idle":"2022-07-20T19:58:00.778232Z","shell.execute_reply.started":"2022-07-20T19:58:00.534484Z","shell.execute_reply":"2022-07-20T19:58:00.777303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.boxenplot(x=\"insomnia\",y=\"height\",data=train)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:58:00.779204Z","iopub.execute_input":"2022-07-20T19:58:00.779406Z","iopub.status.idle":"2022-07-20T19:58:01.160356Z","shell.execute_reply.started":"2022-07-20T19:58:00.779382Z","shell.execute_reply":"2022-07-20T19:58:01.159536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.boxenplot(x=\"insomnia\",y=\"weight\",data=train)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:58:01.161806Z","iopub.execute_input":"2022-07-20T19:58:01.162222Z","iopub.status.idle":"2022-07-20T19:58:01.400308Z","shell.execute_reply.started":"2022-07-20T19:58:01.162180Z","shell.execute_reply":"2022-07-20T19:58:01.399720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"combine_list=[train]\nfor combine in combine_list:\n    combine.drop(combine[combine[\"ubp\"]>=12000].index,inplace=True)\n    combine.drop(combine[combine[\"weight\"]>=175].index,inplace=True)\n    combine.drop(combine[combine[\"height\"]>=200].index,inplace=True)\n    combine.drop(combine[combine[\"height\"]<=60].index,inplace=True)\n    \n","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:58:01.401724Z","iopub.execute_input":"2022-07-20T19:58:01.402012Z","iopub.status.idle":"2022-07-20T19:58:01.441609Z","shell.execute_reply.started":"2022-07-20T19:58:01.401976Z","shell.execute_reply":"2022-07-20T19:58:01.440710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.boxenplot(x=\"insomnia\",y=\"weight\",data=train)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:58:01.443025Z","iopub.execute_input":"2022-07-20T19:58:01.443265Z","iopub.status.idle":"2022-07-20T19:58:01.691652Z","shell.execute_reply.started":"2022-07-20T19:58:01.443238Z","shell.execute_reply":"2022-07-20T19:58:01.690836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.boxenplot(x=\"insomnia\",y=\"height\",data=train)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:58:01.693012Z","iopub.execute_input":"2022-07-20T19:58:01.693291Z","iopub.status.idle":"2022-07-20T19:58:01.926340Z","shell.execute_reply.started":"2022-07-20T19:58:01.693249Z","shell.execute_reply":"2022-07-20T19:58:01.925791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"le=LabelEncoder()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:58:01.927338Z","iopub.execute_input":"2022-07-20T19:58:01.927687Z","iopub.status.idle":"2022-07-20T19:58:01.931653Z","shell.execute_reply.started":"2022-07-20T19:58:01.927660Z","shell.execute_reply":"2022-07-20T19:58:01.930872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"AgeBin\"]=pd.qcut(train[\"age\"],5)\ntrain[[\"AgeBin\",\"insomnia\"]].groupby([\"AgeBin\"],as_index=False).mean().sort_values(by=\"AgeBin\",ascending=True)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:58:01.933078Z","iopub.execute_input":"2022-07-20T19:58:01.933454Z","iopub.status.idle":"2022-07-20T19:58:01.972318Z","shell.execute_reply.started":"2022-07-20T19:58:01.933415Z","shell.execute_reply":"2022-07-20T19:58:01.971781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"combine_list=[train,test]\nfor combine in combine_list:\n    \n    combine.loc[(combine[\"age\"]>=29.561999999999998)&(combine[\"age\"]<47.136),\"age\"]=0\n    combine.loc[(combine[\"age\"]>=47.136)&(combine[\"age\"]<51.964),\"age\"]=1\n    combine.loc[(combine[\"age\"]>=51.964)&(combine[\"age\"]<55.871),\"age\"]=2\n    combine.loc[(combine[\"age\"]>=55.871)&(combine[\"age\"]<59.822),\"age\"]=3\n    combine.loc[(combine[\"age\"]>=59.822)&(combine[\"age\"]<64.923),\"age\"]=4\n    combine[\"age\"]=combine[\"age\"].astype(int)\n\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:58:01.973282Z","iopub.execute_input":"2022-07-20T19:58:01.973933Z","iopub.status.idle":"2022-07-20T19:58:02.007337Z","shell.execute_reply.started":"2022-07-20T19:58:01.973893Z","shell.execute_reply":"2022-07-20T19:58:02.006767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntrain[\"WeightBin\"]=pd.qcut(train[\"weight\"],5)\ntrain[[\"WeightBin\",\"insomnia\"]].groupby([\"WeightBin\"],as_index=False).mean().sort_values(by=\"WeightBin\",ascending=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:58:02.008241Z","iopub.execute_input":"2022-07-20T19:58:02.009003Z","iopub.status.idle":"2022-07-20T19:58:02.039786Z","shell.execute_reply.started":"2022-07-20T19:58:02.008968Z","shell.execute_reply":"2022-07-20T19:58:02.039144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"combine_list=[train,test]\nfor combine in combine_list:\n    \n    combine.loc[(combine[\"weight\"]>9.999)&(combine[\"weight\"]<=63.0),\"weight\"]=0\n    combine.loc[(combine[\"weight\"]>63.0)&(combine[\"weight\"]<=69.0),\"weight\"]=1\n    combine.loc[(combine[\"weight\"]>69.0)&(combine[\"weight\"]<=75.0),\"weight\"]=2\n    combine.loc[(combine[\"weight\"]>75.0)&(combine[\"weight\"]<=85.0),\"weight\"]=3\n    combine.loc[(combine[\"weight\"]>85.0)&(combine[\"weight\"]<=172.0),\"weight\"]=4\n    combine[\"weight\"]=combine[\"weight\"].astype(int)\n\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:58:02.040701Z","iopub.execute_input":"2022-07-20T19:58:02.041327Z","iopub.status.idle":"2022-07-20T19:58:02.078410Z","shell.execute_reply.started":"2022-07-20T19:58:02.041294Z","shell.execute_reply":"2022-07-20T19:58:02.077344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"4\"></a>\n<h1 style='background:blue; border:0; color:white'><center>Missing Value Percentage</center></h1>\n\n<a href=\"#TOC\" class=\"btn btn-primary btn-sm\" role=\"button\" aria-pressed=\"true\" style=\"color:white\" data-toggle=\"popover\">Go to TOC</a>","metadata":{}},{"cell_type":"markdown","source":"**Train set missing value**","metadata":{}},{"cell_type":"code","source":"def percent_missing(df):\n    data = pd.DataFrame(train)\n    df_cols = list(pd.DataFrame(data))\n    dict_x = {}\n    for i in range(0, len(df_cols)):\n        dict_x.update({df_cols[i]: round(data[df_cols[i]].isnull().mean()*100,2)})\n    \n    return dict_x\n\nmissing = percent_missing(train)\ndf_miss = sorted(missing.items(), key=lambda x: x[1], reverse=True)\nprint('Percent of missing data')\ndf_miss[0:20]\n\n","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:58:02.079656Z","iopub.execute_input":"2022-07-20T19:58:02.079919Z","iopub.status.idle":"2022-07-20T19:58:02.097714Z","shell.execute_reply.started":"2022-07-20T19:58:02.079890Z","shell.execute_reply":"2022-07-20T19:58:02.096644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Test set missing value**","metadata":{}},{"cell_type":"code","source":"def percent_missing(df):\n    data = pd.DataFrame(test)\n    df_cols = list(pd.DataFrame(data))\n    dict_x = {}\n    for i in range(0, len(df_cols)):\n        dict_x.update({df_cols[i]: round(data[df_cols[i]].isnull().mean()*100,2)})\n    \n    return dict_x\n\nmissing = percent_missing(test)\ndf_miss = sorted(missing.items(), key=lambda x: x[1], reverse=True)\nprint('Percent of missing data')\ndf_miss[0:20]","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:58:02.099231Z","iopub.execute_input":"2022-07-20T19:58:02.099464Z","iopub.status.idle":"2022-07-20T19:58:02.114793Z","shell.execute_reply.started":"2022-07-20T19:58:02.099436Z","shell.execute_reply":"2022-07-20T19:58:02.114128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"combine_list=[train,test]\nfor combine in combine_list:\n    combine.drop([\"id\",\"age\",\"weight\"],axis=1,inplace=True)\ntrain.drop([\"AgeBin\",\"WeightBin\"],axis=1,inplace=True)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:58:02.119245Z","iopub.execute_input":"2022-07-20T19:58:02.119736Z","iopub.status.idle":"2022-07-20T19:58:02.144880Z","shell.execute_reply.started":"2022-07-20T19:58:02.119705Z","shell.execute_reply":"2022-07-20T19:58:02.143885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:58:02.146141Z","iopub.execute_input":"2022-07-20T19:58:02.146395Z","iopub.status.idle":"2022-07-20T19:58:02.154784Z","shell.execute_reply.started":"2022-07-20T19:58:02.146368Z","shell.execute_reply":"2022-07-20T19:58:02.153928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[test[\"sport\"].isnull()]","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:58:02.155886Z","iopub.execute_input":"2022-07-20T19:58:02.156481Z","iopub.status.idle":"2022-07-20T19:58:02.175029Z","shell.execute_reply.started":"2022-07-20T19:58:02.156430Z","shell.execute_reply":"2022-07-20T19:58:02.174274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[\"pernicious_1\"].median()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:58:02.176083Z","iopub.execute_input":"2022-07-20T19:58:02.176441Z","iopub.status.idle":"2022-07-20T19:58:02.181962Z","shell.execute_reply.started":"2022-07-20T19:58:02.176411Z","shell.execute_reply":"2022-07-20T19:58:02.181198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[\"sport\"].fillna(test[\"sport\"].median(),inplace=True)\n\ntest.pernicious_1.fillna(0,inplace = True)\n\ntest.pernicious_2.fillna(0,inplace = True)\ntest.sport=test.sport.astype(\"int64\")\ntest.pernicious_1=test.pernicious_1.astype(\"int64\")\ntest.pernicious_2=test.pernicious_2.astype(\"int64\")\n","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:58:02.183350Z","iopub.execute_input":"2022-07-20T19:58:02.183694Z","iopub.status.idle":"2022-07-20T19:58:02.199339Z","shell.execute_reply.started":"2022-07-20T19:58:02.183657Z","shell.execute_reply":"2022-07-20T19:58:02.198364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:58:02.200777Z","iopub.execute_input":"2022-07-20T19:58:02.201077Z","iopub.status.idle":"2022-07-20T19:58:02.214143Z","shell.execute_reply.started":"2022-07-20T19:58:02.201040Z","shell.execute_reply":"2022-07-20T19:58:02.213324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:58:02.215396Z","iopub.execute_input":"2022-07-20T19:58:02.215961Z","iopub.status.idle":"2022-07-20T19:58:02.228390Z","shell.execute_reply.started":"2022-07-20T19:58:02.215927Z","shell.execute_reply":"2022-07-20T19:58:02.227647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def correlation_heatmap(df):\n    _ , ax = plt.subplots(figsize =(14, 12))\n    colormap = sns.diverging_palette(220, 10, as_cmap = True)\n    \n    _ = sns.heatmap(\n        df.corr(), \n        cmap = colormap,\n        square=True, \n        cbar_kws={'shrink':.9 }, \n        ax=ax,\n        annot=True, \n        linewidths=0.1,vmax=1.0, linecolor='white',\n        annot_kws={'fontsize':12 }\n    )\n    \n    plt.title('Pearson Correlation of Features', y=1.05, size=15)\n\ncorrelation_heatmap(train)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:58:02.229738Z","iopub.execute_input":"2022-07-20T19:58:02.230177Z","iopub.status.idle":"2022-07-20T19:58:03.166380Z","shell.execute_reply.started":"2022-07-20T19:58:02.230149Z","shell.execute_reply":"2022-07-20T19:58:03.165776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"5\"></a>\n\n<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n<h1 class=\"list-group-item list-group-item-action active\" data-toggle=\"list\" style='background:blue; border:0' role=\"tab\" aria-controls=\"home\"><center>Modeling</center></h1>\n    ","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split, StratifiedKFold, GridSearchCV\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.svm import SVC\nfrom sklearn.ensemble import RandomForestClassifier, VotingClassifier\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.metrics import accuracy_score","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:58:03.167379Z","iopub.execute_input":"2022-07-20T19:58:03.167874Z","iopub.status.idle":"2022-07-20T19:58:03.173271Z","shell.execute_reply.started":"2022-07-20T19:58:03.167839Z","shell.execute_reply":"2022-07-20T19:58:03.172436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:58:03.174333Z","iopub.execute_input":"2022-07-20T19:58:03.174551Z","iopub.status.idle":"2022-07-20T19:58:03.191068Z","shell.execute_reply.started":"2022-07-20T19:58:03.174523Z","shell.execute_reply":"2022-07-20T19:58:03.190209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:58:03.192312Z","iopub.execute_input":"2022-07-20T19:58:03.193036Z","iopub.status.idle":"2022-07-20T19:58:03.210168Z","shell.execute_reply.started":"2022-07-20T19:58:03.192999Z","shell.execute_reply":"2022-07-20T19:58:03.209375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:58:03.211239Z","iopub.execute_input":"2022-07-20T19:58:03.211813Z","iopub.status.idle":"2022-07-20T19:58:03.225624Z","shell.execute_reply.started":"2022-07-20T19:58:03.211781Z","shell.execute_reply":"2022-07-20T19:58:03.224826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split features and labels\ntarget = train['insomnia'].reset_index(drop=True)\ntrain_features = train.drop(['insomnia'], axis=1)\ntest_features = test\n\n# Combine train and test features in order to apply the feature transformation pipeline to the entire dataset\nall_features = pd.concat([train_features, test_features]).reset_index(drop=True)\nall_features.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:58:03.227114Z","iopub.execute_input":"2022-07-20T19:58:03.227581Z","iopub.status.idle":"2022-07-20T19:58:03.257012Z","shell.execute_reply.started":"2022-07-20T19:58:03.227541Z","shell.execute_reply":"2022-07-20T19:58:03.256227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = all_features.iloc[:len(target), :]\nX_test = all_features.iloc[len(target):, :]\nX.shape, target.shape, X_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:58:03.258152Z","iopub.execute_input":"2022-07-20T19:58:03.258543Z","iopub.status.idle":"2022-07-20T19:58:03.265209Z","shell.execute_reply.started":"2022-07-20T19:58:03.258513Z","shell.execute_reply":"2022-07-20T19:58:03.264451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Setup cross validation folds\nkf = KFold(n_splits=3, random_state=42, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:58:03.266251Z","iopub.execute_input":"2022-07-20T19:58:03.266779Z","iopub.status.idle":"2022-07-20T19:58:03.274926Z","shell.execute_reply.started":"2022-07-20T19:58:03.266724Z","shell.execute_reply":"2022-07-20T19:58:03.274156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define error metrics\ndef rmsle(y, y_pred):\n    return np.sqrt(mean_squared_error(y, y_pred))\n\ndef cv_rmse(model, X=X):\n    rmse = np.sqrt(-cross_val_score(model, X, target, scoring=\"neg_mean_squared_error\", cv=kf))\n    return (rmse)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:58:03.276639Z","iopub.execute_input":"2022-07-20T19:58:03.277054Z","iopub.status.idle":"2022-07-20T19:58:03.286375Z","shell.execute_reply.started":"2022-07-20T19:58:03.277016Z","shell.execute_reply":"2022-07-20T19:58:03.285829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Light Gradient Boosting Regressor\nlightgbm = LGBMRegressor(objective='regression', \n                       num_leaves=6,\n                       learning_rate=0.01, \n                       n_estimators=7000,\n                       max_bin=200, \n                       bagging_fraction=0.8,\n                       bagging_freq=4, \n                       bagging_seed=8,\n                       feature_fraction=0.2,\n                       feature_fraction_seed=8,\n                       min_sum_hessian_in_leaf = 11,\n                       verbose=-1,\n                       random_state=42)\n\n","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:58:03.287880Z","iopub.execute_input":"2022-07-20T19:58:03.288190Z","iopub.status.idle":"2022-07-20T19:58:03.297978Z","shell.execute_reply.started":"2022-07-20T19:58:03.288146Z","shell.execute_reply":"2022-07-20T19:58:03.297024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores = {}\n\nscore = cv_rmse(lightgbm)\nprint(\"lightgbm: {:.4f} ({:.4f})\".format(score.mean(), score.std()))\nscores['lgb'] = (score.mean(), score.std())","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:58:03.298955Z","iopub.execute_input":"2022-07-20T19:58:03.299638Z","iopub.status.idle":"2022-07-20T19:58:48.221269Z","shell.execute_reply.started":"2022-07-20T19:58:03.299597Z","shell.execute_reply":"2022-07-20T19:58:48.220591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('lightgbm')\nlgb_model_full_data = lightgbm.fit(X, target)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:58:48.222448Z","iopub.execute_input":"2022-07-20T19:58:48.222868Z","iopub.status.idle":"2022-07-20T19:59:05.088705Z","shell.execute_reply.started":"2022-07-20T19:58:48.222836Z","shell.execute_reply":"2022-07-20T19:59:05.087678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:59:05.090182Z","iopub.execute_input":"2022-07-20T19:59:05.090624Z","iopub.status.idle":"2022-07-20T19:59:05.096597Z","shell.execute_reply.started":"2022-07-20T19:59:05.090594Z","shell.execute_reply":"2022-07-20T19:59:05.095690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:59:05.097463Z","iopub.execute_input":"2022-07-20T19:59:05.097670Z","iopub.status.idle":"2022-07-20T19:59:05.106866Z","shell.execute_reply.started":"2022-07-20T19:59:05.097645Z","shell.execute_reply":"2022-07-20T19:59:05.106137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.iloc[:,1] = lgb_model_full_data.predict(test)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:59:05.107949Z","iopub.execute_input":"2022-07-20T19:59:05.108168Z","iopub.status.idle":"2022-07-20T19:59:09.308407Z","shell.execute_reply.started":"2022-07-20T19:59:05.108143Z","shell.execute_reply":"2022-07-20T19:59:09.307685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T19:59:09.309817Z","iopub.execute_input":"2022-07-20T19:59:09.310292Z","iopub.status.idle":"2022-07-20T19:59:09.430323Z","shell.execute_reply.started":"2022-07-20T19:59:09.310259Z","shell.execute_reply":"2022-07-20T19:59:09.429638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# If you like this kernel, please give it an upvote. Thank you!","metadata":{}}]}