{"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":"import numpy as np, pandas as pd, os\nfrom sklearn.model_selection import cross_val_score, StratifiedKFold\nimport xgboost as xgb\nimport plotly.express as px, seaborn as sns, matplotlib.pyplot as plt\nsns.set_style('darkgrid')\nfrom sklearn.metrics import make_scorer, cohen_kappa_score\nimport eli5\nfrom eli5.sklearn import PermutationImportance\nimport warnings\nwarnings.simplefilter('ignore')\n\npath = '../input/child-mind-institute-problematic-internet-use/'\n\ntrain = pd.read_csv(path + 'train.csv', index_col = 'id')\nprint(\"The train data has the shape: \",train.shape)\ntest = pd.read_csv(path + 'test.csv', index_col = 'id')\nprint(\"The test data has the shape: \",test.shape)\nprint(\"\")\nprint(\"Total number of missing training values: \", train.isna().sum().sum())\ndata_dictionary = pd.read_csv(path + 'data_dictionary.csv')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-11-24T14:13:31.827021Z","iopub.execute_input":"2024-11-24T14:13:31.827924Z","iopub.status.idle":"2024-11-24T14:13:49.114834Z","shell.execute_reply.started":"2024-11-24T14:13:31.827859Z","shell.execute_reply":"2024-11-24T14:13:49.113836Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_cat_columns = train.select_dtypes(exclude = 'number').columns\n\nfor season in train_cat_columns:\n    train[season] = train[season].fillna(0)\n    train[season] = train[season].replace({'Spring':1, 'Summer':2, 'Fall':3, 'Winter':4})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T14:13:54.135120Z","iopub.execute_input":"2024-11-24T14:13:54.135478Z","iopub.status.idle":"2024-11-24T14:13:54.179200Z","shell.execute_reply.started":"2024-11-24T14:13:54.135446Z","shell.execute_reply":"2024-11-24T14:13:54.178035Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_cat_columns = test.select_dtypes(exclude = 'number').columns\n\nfor season in test_cat_columns:\n    test[season] = test[season].fillna(0)\n    test[season] = test[season].replace({'Spring':1, 'Summer':2, 'Fall':3, 'Winter':4})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T14:14:16.529769Z","iopub.execute_input":"2024-11-24T14:14:16.530748Z","iopub.status.idle":"2024-11-24T14:14:16.552029Z","shell.execute_reply.started":"2024-11-24T14:14:16.530691Z","shell.execute_reply":"2024-11-24T14:14:16.550993Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"PCIAT_cols = [val for val in train.columns[train.columns.str.contains('PCIAT')]]\nprint('Number of PCIAT features = ' , len(PCIAT_cols))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T14:14:22.966474Z","iopub.execute_input":"2024-11-24T14:14:22.967162Z","iopub.status.idle":"2024-11-24T14:14:22.973304Z","shell.execute_reply.started":"2024-11-24T14:14:22.967122Z","shell.execute_reply":"2024-11-24T14:14:22.972179Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pd.set_option('display.max_colwidth', None)\nquestions = data_dictionary[data_dictionary.Field.str.contains('PCIAT-PCIAT')]\nquestions[['Field','Description']]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T14:14:26.494372Z","iopub.execute_input":"2024-11-24T14:14:26.495340Z","iopub.status.idle":"2024-11-24T14:14:26.518105Z","shell.execute_reply.started":"2024-11-24T14:14:26.495296Z","shell.execute_reply":"2024-11-24T14:14:26.517072Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"corr = train[PCIAT_cols].corr()['PCIAT-PCIAT_Total'].sort_values(ascending = False)\ncorr = pd.DataFrame(corr)\ncorr.style.background_gradient(cmap='YlOrRd')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T14:14:31.217732Z","iopub.execute_input":"2024-11-24T14:14:31.218646Z","iopub.status.idle":"2024-11-24T14:14:31.289989Z","shell.execute_reply.started":"2024-11-24T14:14:31.218609Z","shell.execute_reply":"2024-11-24T14:14:31.289036Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.boxplot(train, x = 'PCIAT-PCIAT_Total').set_title('Boxplot of PCIAT Total Scores')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T14:14:35.722372Z","iopub.execute_input":"2024-11-24T14:14:35.723345Z","iopub.status.idle":"2024-11-24T14:14:36.027398Z","shell.execute_reply.started":"2024-11-24T14:14:35.723304Z","shell.execute_reply":"2024-11-24T14:14:36.026246Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(train[train['PCIAT-PCIAT_Total']<=30].sii.value_counts())\nprint(train[(train['PCIAT-PCIAT_Total']>30) \n    & (train['PCIAT-PCIAT_Total']<50)].sii.value_counts())\nprint(train[(train['PCIAT-PCIAT_Total']>=50) \n    & (train['PCIAT-PCIAT_Total']<80)].sii.value_counts())\nprint(train[train['PCIAT-PCIAT_Total']>=80].sii.value_counts())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T14:14:39.928194Z","iopub.execute_input":"2024-11-24T14:14:39.928896Z","iopub.status.idle":"2024-11-24T14:14:39.948208Z","shell.execute_reply.started":"2024-11-24T14:14:39.928856Z","shell.execute_reply":"2024-11-24T14:14:39.947020Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.sii.value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T14:14:46.819587Z","iopub.execute_input":"2024-11-24T14:14:46.820439Z","iopub.status.idle":"2024-11-24T14:14:46.828974Z","shell.execute_reply.started":"2024-11-24T14:14:46.820385Z","shell.execute_reply":"2024-11-24T14:14:46.827810Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"PCIAT_cols.remove('PCIAT-PCIAT_Total')\ntrain = train.drop(columns = PCIAT_cols)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T14:14:50.594977Z","iopub.execute_input":"2024-11-24T14:14:50.595771Z","iopub.status.idle":"2024-11-24T14:14:50.602183Z","shell.execute_reply.started":"2024-11-24T14:14:50.595689Z","shell.execute_reply":"2024-11-24T14:14:50.601056Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.countplot(train, x = 'sii').set_title('Count of sii')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T14:15:08.850541Z","iopub.execute_input":"2024-11-24T14:15:08.850961Z","iopub.status.idle":"2024-11-24T14:15:09.142562Z","shell.execute_reply.started":"2024-11-24T14:15:08.850919Z","shell.execute_reply":"2024-11-24T14:15:09.141421Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"vals = ['PIU = 0', 'PIU = 1','PIU = 2', 'PIU = 3']\n\nfor i in range(4):\n    plt.figure()\n    plot = sns.countplot(x = train[train.sii==i]['PreInt_EduHx-computerinternet_hoursday'])\n    plot.set_title(vals[i])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T14:15:34.424869Z","iopub.execute_input":"2024-11-24T14:15:34.425227Z","iopub.status.idle":"2024-11-24T14:15:35.447510Z","shell.execute_reply.started":"2024-11-24T14:15:34.425195Z","shell.execute_reply":"2024-11-24T14:15:35.446479Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = train.dropna(subset='sii')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T14:15:54.583727Z","iopub.execute_input":"2024-11-24T14:15:54.584119Z","iopub.status.idle":"2024-11-24T14:15:54.593303Z","shell.execute_reply.started":"2024-11-24T14:15:54.584084Z","shell.execute_reply":"2024-11-24T14:15:54.592132Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"corr = pd.DataFrame(train.corr()['PCIAT-PCIAT_Total'].sort_values(ascending = False))\ncorr.style.background_gradient(cmap='YlOrRd')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T14:16:08.319386Z","iopub.execute_input":"2024-11-24T14:16:08.319767Z","iopub.status.idle":"2024-11-24T14:16:08.364471Z","shell.execute_reply.started":"2024-11-24T14:16:08.319729Z","shell.execute_reply":"2024-11-24T14:16:08.363314Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"selection = corr[(corr['PCIAT-PCIAT_Total']>.1) | (corr['PCIAT-PCIAT_Total']<-.1)]\nselection = [val for val in selection.index]\nselection.remove('PCIAT-PCIAT_Total')\nselection.remove('sii')\nselection.remove('Physical-BMI')\nselection.remove('SDS-SDS_Total_Raw')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T14:16:27.534366Z","iopub.execute_input":"2024-11-24T14:16:27.534747Z","iopub.status.idle":"2024-11-24T14:16:27.541626Z","shell.execute_reply.started":"2024-11-24T14:16:27.534711Z","shell.execute_reply":"2024-11-24T14:16:27.540495Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"selection","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T14:16:39.532466Z","iopub.execute_input":"2024-11-24T14:16:39.533451Z","iopub.status.idle":"2024-11-24T14:16:39.539599Z","shell.execute_reply.started":"2024-11-24T14:16:39.533408Z","shell.execute_reply":"2024-11-24T14:16:39.538454Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"null = train.isna().sum().sort_values(ascending = False).head(46)\nnull = pd.DataFrame(null)\nnull = null.rename(columns= {0:'Missing'})\nnull.style.background_gradient(cmap='YlOrRd')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T14:16:57.107259Z","iopub.execute_input":"2024-11-24T14:16:57.107632Z","iopub.status.idle":"2024-11-24T14:16:57.128010Z","shell.execute_reply.started":"2024-11-24T14:16:57.107596Z","shell.execute_reply":"2024-11-24T14:16:57.126834Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"half_missing = [val for val in train.columns[train.isnull().sum()>len(train)/2]]\nhalf_missing","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T14:17:13.880359Z","iopub.execute_input":"2024-11-24T14:17:13.881287Z","iopub.status.idle":"2024-11-24T14:17:13.891634Z","shell.execute_reply.started":"2024-11-24T14:17:13.881235Z","shell.execute_reply":"2024-11-24T14:17:13.890453Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"selection = [i for i in selection if i not in half_missing]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T14:17:31.678304Z","iopub.execute_input":"2024-11-24T14:17:31.678683Z","iopub.status.idle":"2024-11-24T14:17:31.684093Z","shell.execute_reply.started":"2024-11-24T14:17:31.678639Z","shell.execute_reply":"2024-11-24T14:17:31.682846Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"describe = train[selection].describe().T\ndescribe = describe[['min','max']].sort_index()\ndescribe.style.background_gradient(cmap='YlOrRd')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T14:17:48.261902Z","iopub.execute_input":"2024-11-24T14:17:48.262876Z","iopub.status.idle":"2024-11-24T14:17:48.311604Z","shell.execute_reply.started":"2024-11-24T14:17:48.262833Z","shell.execute_reply":"2024-11-24T14:17:48.310524Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train[selection].hist(figsize=(10,10), grid = True, color = 'chocolate')\nplt.tight_layout()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T14:18:06.359890Z","iopub.execute_input":"2024-11-24T14:18:06.360289Z","iopub.status.idle":"2024-11-24T14:18:10.610094Z","shell.execute_reply.started":"2024-11-24T14:18:06.360253Z","shell.execute_reply":"2024-11-24T14:18:10.609016Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = train[selection]\ntest = test[selection]\ny = train.sii","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T14:18:32.643474Z","iopub.execute_input":"2024-11-24T14:18:32.643958Z","iopub.status.idle":"2024-11-24T14:18:32.650918Z","shell.execute_reply.started":"2024-11-24T14:18:32.643915Z","shell.execute_reply":"2024-11-24T14:18:32.649920Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def quadratic_kappa(y_true, y_pred):\n    return cohen_kappa_score(y_true, y_pred, weights='quadratic')\nkappa_scorer = make_scorer(quadratic_kappa)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T14:18:47.729630Z","iopub.execute_input":"2024-11-24T14:18:47.730600Z","iopub.status.idle":"2024-11-24T14:18:47.735249Z","shell.execute_reply.started":"2024-11-24T14:18:47.730559Z","shell.execute_reply":"2024-11-24T14:18:47.734163Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"params = {'max_depth': 3, 'n_estimators': 202, 'learning_rate': 0.07956777025142073, 'subsample': 0.8197358255094112, 'colsample_bytree': 0.645036755035947}\nskf = StratifiedKFold(n_splits=10)\nclf = xgb.XGBClassifier(**params)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T14:19:02.300182Z","iopub.execute_input":"2024-11-24T14:19:02.301227Z","iopub.status.idle":"2024-11-24T14:19:02.306217Z","shell.execute_reply.started":"2024-11-24T14:19:02.301184Z","shell.execute_reply":"2024-11-24T14:19:02.305164Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"scores = cross_val_score(clf, X, y, cv=skf, scoring=kappa_scorer)\nprint(\"QWK Scores:\", scores)\nprint(\"Mean QWK Score:\", np.mean(scores))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T14:19:15.551019Z","iopub.execute_input":"2024-11-24T14:19:15.551404Z","iopub.status.idle":"2024-11-24T14:19:20.191392Z","shell.execute_reply.started":"2024-11-24T14:19:15.551370Z","shell.execute_reply":"2024-11-24T14:19:20.190496Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"clf.fit(X,y)\nfeature_imp = pd.Series(clf.feature_importances_,index=X.columns).sort_values(ascending=False)\nfeature_imp","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T14:19:41.556868Z","iopub.execute_input":"2024-11-24T14:19:41.557256Z","iopub.status.idle":"2024-11-24T14:19:42.021141Z","shell.execute_reply.started":"2024-11-24T14:19:41.557222Z","shell.execute_reply":"2024-11-24T14:19:42.020072Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.barplot(x=feature_imp, y=feature_imp.index)\nplt.xlabel('Feature Importance Score')\nplt.title(\"Feature Importances\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T14:20:02.347306Z","iopub.execute_input":"2024-11-24T14:20:02.347684Z","iopub.status.idle":"2024-11-24T14:20:02.704981Z","shell.execute_reply.started":"2024-11-24T14:20:02.347649Z","shell.execute_reply":"2024-11-24T14:20:02.703852Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"perm = PermutationImportance(clf, random_state=1).fit(X,y)\neli5.show_weights(perm, feature_names = X.columns.tolist())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T14:20:17.085354Z","iopub.execute_input":"2024-11-24T14:20:17.086154Z","iopub.status.idle":"2024-11-24T14:20:18.791487Z","shell.execute_reply.started":"2024-11-24T14:20:17.086112Z","shell.execute_reply":"2024-11-24T14:20:18.790299Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = train[selection]\ntest = test[selection]\ny = train['PCIAT-PCIAT_Total']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T14:20:57.295490Z","iopub.execute_input":"2024-11-24T14:20:57.295998Z","iopub.status.idle":"2024-11-24T14:20:57.303004Z","shell.execute_reply.started":"2024-11-24T14:20:57.295961Z","shell.execute_reply":"2024-11-24T14:20:57.301842Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def convert(scores):\n    scores = np.array(scores)*1.252\n    bins = np.zeros_like(scores)\n    bins[scores <= 30] = 0\n    bins[(scores > 30) & (scores < 50)] = 1\n    bins[(scores >= 50) & (scores < 80)] = 2\n    bins[scores >= 80] = 3\n    return bins\n\ndef quadratic_kappa(y_true, y_pred):\n    y_true_cat = convert(y_true)\n    y_pred_cat = convert(y_pred)\n    return cohen_kappa_score(y_true_cat, y_pred_cat, weights='quadratic')\n\nkappa_scorer = make_scorer(quadratic_kappa, greater_is_better=True)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T14:21:23.666582Z","iopub.execute_input":"2024-11-24T14:21:23.667385Z","iopub.status.idle":"2024-11-24T14:21:23.673752Z","shell.execute_reply.started":"2024-11-24T14:21:23.667342Z","shell.execute_reply":"2024-11-24T14:21:23.672643Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"params = {'max_depth': 3, 'n_estimators': 59, 'learning_rate': 0.07327652118259573, 'subsample': 0.5968194045365575, 'colsample_bytree': 0.9123669348125403}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T14:21:39.034907Z","iopub.execute_input":"2024-11-24T14:21:39.035288Z","iopub.status.idle":"2024-11-24T14:21:39.040264Z","shell.execute_reply.started":"2024-11-24T14:21:39.035252Z","shell.execute_reply":"2024-11-24T14:21:39.039035Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = xgb.XGBRegressor(**params)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T14:21:50.577188Z","iopub.execute_input":"2024-11-24T14:21:50.577921Z","iopub.status.idle":"2024-11-24T14:21:50.582327Z","shell.execute_reply.started":"2024-11-24T14:21:50.577880Z","shell.execute_reply":"2024-11-24T14:21:50.581194Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"scores = cross_val_score(model, X, y, cv=skf, scoring=kappa_scorer)\nprint(\"QWK Scores:\", scores)\nprint(\"Mean QWK Score:\", np.mean(scores))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T14:22:05.722428Z","iopub.execute_input":"2024-11-24T14:22:05.722937Z","iopub.status.idle":"2024-11-24T14:22:06.346121Z","shell.execute_reply.started":"2024-11-24T14:22:05.722888Z","shell.execute_reply":"2024-11-24T14:22:06.344923Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.fit(X,y)\nfeature_imp = pd.Series(model.feature_importances_,index=X.columns).sort_values(ascending=False)\nfeature_imp","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T14:22:20.930691Z","iopub.execute_input":"2024-11-24T14:22:20.931670Z","iopub.status.idle":"2024-11-24T14:22:20.995883Z","shell.execute_reply.started":"2024-11-24T14:22:20.931619Z","shell.execute_reply":"2024-11-24T14:22:20.994727Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.barplot(x=feature_imp, y=feature_imp.index)\nplt.xlabel('Feature Importance Score')\nplt.title(\"Feature Importances\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T14:22:40.294308Z","iopub.execute_input":"2024-11-24T14:22:40.295100Z","iopub.status.idle":"2024-11-24T14:22:40.673235Z","shell.execute_reply.started":"2024-11-24T14:22:40.295056Z","shell.execute_reply":"2024-11-24T14:22:40.672208Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"perm = PermutationImportance(model, random_state=1).fit(X,y)\neli5.show_weights(perm, feature_names = X.columns.tolist())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T14:22:53.895997Z","iopub.execute_input":"2024-11-24T14:22:53.896380Z","iopub.status.idle":"2024-11-24T14:22:54.311425Z","shell.execute_reply.started":"2024-11-24T14:22:53.896345Z","shell.execute_reply":"2024-11-24T14:22:54.310292Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}