{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"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":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport warnings\nfrom xgboost import XGBClassifier\nfrom sklearn.preprocessing import LabelEncoder,StandardScaler\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.base import TransformerMixin,BaseEstimator\nfrom sklearn.model_selection import StratifiedKFold\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score,cohen_kappa_score\nwarnings.filterwarnings(action='ignore')","metadata":{"execution":{"iopub.status.busy":"2024-11-02T16:44:28.438700Z","iopub.execute_input":"2024-11-02T16:44:28.440048Z","iopub.status.idle":"2024-11-02T16:44:32.357876Z","shell.execute_reply.started":"2024-11-02T16:44:28.439991Z","shell.execute_reply":"2024-11-02T16:44:32.356602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv('/kaggle/input/child-mind-institute-problematic-internet-use/train.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2024-11-02T16:44:32.360416Z","iopub.execute_input":"2024-11-02T16:44:32.361153Z","iopub.status.idle":"2024-11-02T16:44:32.482029Z","shell.execute_reply.started":"2024-11-02T16:44:32.361069Z","shell.execute_reply":"2024-11-02T16:44:32.480619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids=df['id']\ndf=df.drop('id',axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-11-02T16:44:32.483862Z","iopub.execute_input":"2024-11-02T16:44:32.484445Z","iopub.status.idle":"2024-11-02T16:44:32.493207Z","shell.execute_reply.started":"2024-11-02T16:44:32.484386Z","shell.execute_reply":"2024-11-02T16:44:32.491757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.describe()","metadata":{"execution":{"iopub.status.busy":"2024-11-02T16:44:32.496496Z","iopub.execute_input":"2024-11-02T16:44:32.496991Z","iopub.status.idle":"2024-11-02T16:44:32.692654Z","shell.execute_reply.started":"2024-11-02T16:44:32.496933Z","shell.execute_reply":"2024-11-02T16:44:32.691522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols_categorical=df.select_dtypes(include=['object'])\ncols_categorical","metadata":{"execution":{"iopub.status.busy":"2024-11-02T16:44:32.694327Z","iopub.execute_input":"2024-11-02T16:44:32.694823Z","iopub.status.idle":"2024-11-02T16:44:32.718615Z","shell.execute_reply.started":"2024-11-02T16:44:32.694769Z","shell.execute_reply":"2024-11-02T16:44:32.717381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(11,5))\nsns.countplot(y=df['PreInt_EduHx-Season'],hue=df['PreInt_EduHx-Season'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-02T16:44:32.720177Z","iopub.execute_input":"2024-11-02T16:44:32.720596Z","iopub.status.idle":"2024-11-02T16:44:33.111841Z","shell.execute_reply.started":"2024-11-02T16:44:32.720556Z","shell.execute_reply":"2024-11-02T16:44:33.110626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### Basic demographic: max spring(1150), min in fall(about 850) and nearly equal in summer and winter(about 1000)\n##### Children's global assessment: max in Spring(700), min in winter(570),fall and summer (around 650)\n##### Physical measures: max in Spring(940), nearly equal in fall,summer,winter(800)\n##### Fitness endurance: max in Spring(380),min summer(255),fall and winter (340)\n##### FGC: max in spring(1000),min in winter(750),summer(825),fall(750)\n##### BIA: max in summer(660),min in winter(400),fall(560), spring(510)\n##### PAQ_A: max in winter(136),min in fall(98),spring(124),summer(118)\n##### PAQ_C: max in spring(510),min in fall(350),winter(470),summer(380)\n##### PCIAT: max in spring(760),min in winter(650),fall(670),summer(660)\n##### SDS: max in spring(710),min in fall(620),winter(650),summer(630)\n##### Internet use:max in spring(980),min in summer(820),winter(920),fall(840)","metadata":{}},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2024-11-02T16:44:33.113400Z","iopub.execute_input":"2024-11-02T16:44:33.113820Z","iopub.status.idle":"2024-11-02T16:44:33.149659Z","shell.execute_reply.started":"2024-11-02T16:44:33.113780Z","shell.execute_reply":"2024-11-02T16:44:33.148235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8,5))\nsns.histplot(x=df['CGAS-CGAS_Score'],color='teal',kde=True)\nplt.xlim(0,200)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-02T16:44:33.151300Z","iopub.execute_input":"2024-11-02T16:44:33.151729Z","iopub.status.idle":"2024-11-02T16:44:34.095619Z","shell.execute_reply.started":"2024-11-02T16:44:33.151686Z","shell.execute_reply":"2024-11-02T16:44:34.094265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8,5))\nsns.histplot(x=df['Physical-BMI'],color='teal',kde=True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-02T16:44:34.097415Z","iopub.execute_input":"2024-11-02T16:44:34.097921Z","iopub.status.idle":"2024-11-02T16:44:34.705842Z","shell.execute_reply.started":"2024-11-02T16:44:34.097860Z","shell.execute_reply":"2024-11-02T16:44:34.704568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfna=pd.DataFrame(df.isna().sum())\ndfna","metadata":{"execution":{"iopub.status.busy":"2024-11-02T16:44:34.710429Z","iopub.execute_input":"2024-11-02T16:44:34.710879Z","iopub.status.idle":"2024-11-02T16:44:34.730027Z","shell.execute_reply.started":"2024-11-02T16:44:34.710836Z","shell.execute_reply":"2024-11-02T16:44:34.728418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class drop_rows(BaseEstimator,TransformerMixin):\n    def fit(self,X,y=None):\n        return self\n    def transform(self,X):\n        if 'sii' in X.columns:\n            ind=X[X['sii'].isna()].index\n            X=X.drop(ind)\n            X.reset_index(inplace=True,drop=True)\n        return X\nclass data_clean(BaseEstimator,TransformerMixin):\n    def fit(self,X,y=None):\n        return self\n    def transform(self,X):\n        cols_formean=['Physical-BMI','Physical-Height','Physical-Weight','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',\\\n                      'BIA-BIA_Fat','BIA-BIA_ICW','BIA-BIA_LDM','BIA-BIA_LST','BIA-BIA_SMM','BIA-BIA_TBW']\n        cols_toencode=['Basic_Demos-Enroll_Season','CGAS-Season','Physical-Season','Fitness_Endurance-Season',\\\n                       'FGC-Season','BIA-Season','PAQ_A-Season','PAQ_C-Season','PCIAT-Season','SDS-Season','PreInt_EduHx-Season']\n        for i in X.columns[:-1]:\n            if i in cols_toencode and i in X.columns:\n                X[i]=X[i].fillna(value=X[i].mode()[0])\n            elif i in cols_formean:\n                X[i]=X[i].fillna(value=np.mean(X[i]))\n            else:\n                X[i]=X[i].fillna(value=np.median(X[i]))\n        return X\nclass encode(BaseEstimator,TransformerMixin):\n    def fit(self,X,y=None):\n        return self\n    def transform(self,X):\n        cols_toencode=['Basic_Demos-Enroll_Season','CGAS-Season','Physical-Season','Fitness_Endurance-Season',\\\n                       'FGC-Season','BIA-Season','PAQ_A-Season','PAQ_C-Season','PCIAT-Season','SDS-Season','PreInt_EduHx-Season']\n        for i in cols_toencode:\n            le=LabelEncoder()\n            if i in X.columns:\n                X[i]=le.fit_transform(X[i])\n        return X\nclass scaling(BaseEstimator,TransformerMixin):\n    def fit(self,X,y=None):\n        return self\n    def transform(self,X):\n        sc=StandardScaler()\n        for i in X.columns[:-1]:\n            X[i]=sc.fit_transform(X[[i]])\n        return X","metadata":{"execution":{"iopub.status.busy":"2024-11-02T16:47:32.795687Z","iopub.execute_input":"2024-11-02T16:47:32.796212Z","iopub.status.idle":"2024-11-02T16:47:32.814824Z","shell.execute_reply.started":"2024-11-02T16:47:32.796159Z","shell.execute_reply":"2024-11-02T16:47:32.813471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pipe=Pipeline([\n    ('drop',drop_rows()),\n    ('encode',encode()),\n    ('filling',data_clean()),\n    ('scale',scaling())\n])\ndf=pipe.fit_transform(df)","metadata":{"execution":{"iopub.status.busy":"2024-11-02T16:44:34.750918Z","iopub.execute_input":"2024-11-02T16:44:34.751429Z","iopub.status.idle":"2024-11-02T16:44:35.108352Z","shell.execute_reply.started":"2024-11-02T16:44:34.751379Z","shell.execute_reply":"2024-11-02T16:44:35.107003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dropscols=['Physical-Waist_Circumference','Fitness_Endurance-Max_Stage','Fitness_Endurance-Time_Mins',\\\n           'Fitness_Endurance-Time_Sec','FGC-FGC_GSND','FGC-FGC_GSND_Zone','FGC-FGC_GSD','FGC-FGC_GSD_Zone','PAQ_A-PAQ_A_Total']\ndf=df.drop(columns=dropscols,axis=1)\nfor i in df.columns:\n    df[i]=df[i].fillna(method='ffill')\nfor i in df.columns:\n    df[i]=df[i].fillna(method='bfill')","metadata":{"execution":{"iopub.status.busy":"2024-11-02T16:44:35.110258Z","iopub.execute_input":"2024-11-02T16:44:35.110771Z","iopub.status.idle":"2024-11-02T16:44:35.166932Z","shell.execute_reply.started":"2024-11-02T16:44:35.110714Z","shell.execute_reply":"2024-11-02T16:44:35.165521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2024-11-02T16:44:35.168312Z","iopub.execute_input":"2024-11-02T16:44:35.168692Z","iopub.status.idle":"2024-11-02T16:44:35.186397Z","shell.execute_reply.started":"2024-11-02T16:44:35.168653Z","shell.execute_reply":"2024-11-02T16:44:35.185171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# dfna['missing_valsnew']=df.isna().sum()\n# dfna.to_csv('missing_vals.csv')","metadata":{"execution":{"iopub.status.busy":"2024-11-02T16:44:35.187924Z","iopub.execute_input":"2024-11-02T16:44:35.188440Z","iopub.status.idle":"2024-11-02T16:44:35.193951Z","shell.execute_reply.started":"2024-11-02T16:44:35.188395Z","shell.execute_reply":"2024-11-02T16:44:35.192497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=df.drop('sii',axis=1)\ny=df['sii']","metadata":{"execution":{"iopub.status.busy":"2024-11-02T16:44:35.195702Z","iopub.execute_input":"2024-11-02T16:44:35.196191Z","iopub.status.idle":"2024-11-02T16:44:35.210022Z","shell.execute_reply.started":"2024-11-02T16:44:35.196149Z","shell.execute_reply":"2024-11-02T16:44:35.208702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.33,random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-11-02T16:44:35.211615Z","iopub.execute_input":"2024-11-02T16:44:35.212117Z","iopub.status.idle":"2024-11-02T16:44:35.228775Z","shell.execute_reply.started":"2024-11-02T16:44:35.212014Z","shell.execute_reply":"2024-11-02T16:44:35.227574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xgb=XGBClassifier(seed=42,objective='multi:softmax')\nxgb.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2024-11-02T16:44:35.230804Z","iopub.execute_input":"2024-11-02T16:44:35.231425Z","iopub.status.idle":"2024-11-02T16:44:35.662823Z","shell.execute_reply.started":"2024-11-02T16:44:35.231366Z","shell.execute_reply":"2024-11-02T16:44:35.661912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=xgb.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-11-02T16:44:35.664156Z","iopub.execute_input":"2024-11-02T16:44:35.664813Z","iopub.status.idle":"2024-11-02T16:44:35.688393Z","shell.execute_reply.started":"2024-11-02T16:44:35.664769Z","shell.execute_reply":"2024-11-02T16:44:35.687367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(cohen_kappa_score(y_test,y_pred,weights='quadratic'))","metadata":{"execution":{"iopub.status.busy":"2024-11-02T16:44:35.689767Z","iopub.execute_input":"2024-11-02T16:44:35.690733Z","iopub.status.idle":"2024-11-02T16:44:35.700838Z","shell.execute_reply.started":"2024-11-02T16:44:35.690680Z","shell.execute_reply":"2024-11-02T16:44:35.699839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1=pd.read_parquet('/kaggle/input/child-mind-institute-problematic-internet-use/series_train.parquet/id=00115b9f/part-0.parquet')\ndf1","metadata":{"execution":{"iopub.status.busy":"2024-11-02T16:44:35.705523Z","iopub.execute_input":"2024-11-02T16:44:35.706060Z","iopub.status.idle":"2024-11-02T16:44:35.910411Z","shell.execute_reply.started":"2024-11-02T16:44:35.706003Z","shell.execute_reply":"2024-11-02T16:44:35.909051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set_style('darkgrid')\nplt.rc(\n    'figure',\n    autolayout=True,\n    figsize=(11,5),\n    titlesize=18,\n    titleweight='bold'\n)\nplt.rc(\n    'axes',\n    labelweight='bold',\n    labelsize='large',\n    titleweight='bold',\n    titlesize=16,\n    titlepad=10\n)\nfig,ax=plt.subplots()\nax.plot('step','light',data=df1,color='blue')\nax.set_xlabel('step')\nax.set_ylabel('Light (lux)')\nax.set_title('Variation of Light')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-02T16:44:35.911829Z","iopub.execute_input":"2024-11-02T16:44:35.912263Z","iopub.status.idle":"2024-11-02T16:44:36.504203Z","shell.execute_reply.started":"2024-11-02T16:44:35.912219Z","shell.execute_reply":"2024-11-02T16:44:36.502928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set_style('darkgrid')\nplt.rc(\n    'figure',\n    autolayout=True,\n    figsize=(11,16),\n    titlesize=18,\n    titleweight='bold'\n)\nplt.rc(\n    'axes',\n    labelweight='bold',\n    labelsize='large',\n    titleweight='bold',\n    titlesize=16,\n    titlepad=10\n)\nfig,ax=plt.subplots(3,1)\nax[0].plot('step','X',data=df1,color='teal')\nax[0].set_xlabel('step')\nax[0].set_ylabel('X (g)')\nax[0].set_title('Accelaration along X-axis')\nax[1].plot('step','Y',data=df1,color='teal')\nax[1].set_xlabel('step')\nax[1].set_ylabel('Y (g)')\nax[1].set_title('Accelaration along Y-axis')\nax[2].plot('step','Z',data=df1,color='teal')\nax[2].set_xlabel('step')\nax[2].set_ylabel('Z (g)')\nax[2].set_title('Accelaration along Z-axis')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-02T16:44:36.505624Z","iopub.execute_input":"2024-11-02T16:44:36.505989Z","iopub.status.idle":"2024-11-02T16:44:38.337873Z","shell.execute_reply.started":"2024-11-02T16:44:36.505951Z","shell.execute_reply":"2024-11-02T16:44:38.336557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set_style('darkgrid')\nplt.rc(\n    'figure',\n    autolayout=True,\n    figsize=(11,5),\n    titlesize=18,\n    titleweight='bold'\n)\nplt.rc(\n    'axes',\n    labelweight='bold',\n    labelsize='large',\n    titleweight='bold',\n    titlesize=16,\n    titlepad=10\n)\nfig,ax=plt.subplots()\nax.plot('step','battery_voltage',data=df1,color='blue')\nax.set_xlabel('step')\nax.set_ylabel('Battery voltage (mV)')\nax.set_title('Variation of Battery voltage')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-02T16:44:38.339481Z","iopub.execute_input":"2024-11-02T16:44:38.339910Z","iopub.status.idle":"2024-11-02T16:44:38.926259Z","shell.execute_reply.started":"2024-11-02T16:44:38.339865Z","shell.execute_reply":"2024-11-02T16:44:38.924680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set_style('darkgrid')\nplt.rc(\n    'figure',\n    autolayout=True,\n    figsize=(11,5),\n    titlesize=18,\n    titleweight='bold'\n)\nplt.rc(\n    'axes',\n    labelweight='bold',\n    labelsize='large',\n    titleweight='bold',\n    titlesize=16,\n    titlepad=10\n)\nfig,ax=plt.subplots()\nax.plot('step','relative_date_PCIAT',data=df1,color='blue')\nax.set_xlabel('step')\nax.set_ylabel('relative_date_PCIAT')\nax.set_title('Variation of PCIAT')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-02T16:44:38.928461Z","iopub.execute_input":"2024-11-02T16:44:38.928870Z","iopub.status.idle":"2024-11-02T16:44:39.476105Z","shell.execute_reply.started":"2024-11-02T16:44:38.928828Z","shell.execute_reply":"2024-11-02T16:44:39.475013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df=pd.read_csv('/kaggle/input/child-mind-institute-problematic-internet-use/test.csv')\nids=test_df['id']\ntest_df=test_df.drop('id',axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-11-02T16:44:39.477407Z","iopub.execute_input":"2024-11-02T16:44:39.477791Z","iopub.status.idle":"2024-11-02T16:44:39.491885Z","shell.execute_reply.started":"2024-11-02T16:44:39.477751Z","shell.execute_reply":"2024-11-02T16:44:39.490700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pipe_t=Pipeline([\n    ('drop',drop_rows()),\n    ('encode',encode()),\n    ('filling',data_clean()),\n    ('scale',scaling())\n])\ntest_df=pipe_t.fit_transform(test_df)","metadata":{"execution":{"iopub.status.busy":"2024-11-02T16:47:43.528595Z","iopub.execute_input":"2024-11-02T16:47:43.529071Z","iopub.status.idle":"2024-11-02T16:47:43.763909Z","shell.execute_reply.started":"2024-11-02T16:47:43.529030Z","shell.execute_reply":"2024-11-02T16:47:43.762779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_splits=5\nkf=StratifiedKFold(n_splits=n_splits,shuffle=True,random_state=42)\noof_pred=np.zeros(X_train.shape[0])\ntest_pred=np.zeros(test_df.shape[0])\nfor fld,(train_id,val_id) in enumerate(kf.split(X_train,y_train)):\n    print(f'Fold {fld+1} of {n_splits}')\n    X_tr,X_val=X_train.iloc[train_id],X_train.iloc[val_id]\n    y_tr,y_val=y_train.iloc[train_id],y_train.iloc[val_id]\n    model=xgb\n    model.fit(X_tr,y_tr)\n    val_pred=model.predict(X_val)\n    oof_pred[val_id]=val_pred\n    test_pred+=model.predict(test_df)/n_splits\n    fold_auc=cohen_kappa_score(y_val,val_pred,weights='quadratic')\n    print(f'AUC fold {fld+1}:',fold_auc)\noof_auc=cohen_kappa_score(y_train,oof_pred,weights='quadratic')\nprint('Auc oof:',oof_auc)","metadata":{"execution":{"iopub.status.busy":"2024-11-02T16:48:47.345503Z","iopub.execute_input":"2024-11-02T16:48:47.346019Z","iopub.status.idle":"2024-11-02T16:48:47.822432Z","shell.execute_reply.started":"2024-11-02T16:48:47.345972Z","shell.execute_reply":"2024-11-02T16:48:47.820264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict=pd.DataFrame({'id':ids,'sii':test_pred})\npredict.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2024-11-02T16:44:42.044653Z","iopub.status.idle":"2024-11-02T16:44:42.045316Z","shell.execute_reply.started":"2024-11-02T16:44:42.044969Z","shell.execute_reply":"2024-11-02T16:44:42.045001Z"},"trusted":true},"execution_count":null,"outputs":[]}]}