{"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":"markdown","source":"# Import","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os","metadata":{"execution":{"iopub.status.busy":"2024-10-22T09:10:18.139986Z","iopub.execute_input":"2024-10-22T09:10:18.140463Z","iopub.status.idle":"2024-10-22T09:10:19.346870Z","shell.execute_reply.started":"2024-10-22T09:10:18.140421Z","shell.execute_reply":"2024-10-22T09:10:19.345389Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import cross_val_score, StratifiedKFold\nimport xgboost as xgb\nimport plotly.express as px\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import make_scorer, cohen_kappa_score\nimport eli5\nfrom eli5.sklearn import PermutationImportance\n","metadata":{"execution":{"iopub.status.busy":"2024-10-22T09:10:19.349931Z","iopub.execute_input":"2024-10-22T09:10:19.350610Z","iopub.status.idle":"2024-10-22T09:10:39.081413Z","shell.execute_reply.started":"2024-10-22T09:10:19.350558Z","shell.execute_reply":"2024-10-22T09:10:39.080038Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Data","metadata":{}},{"cell_type":"code","source":"path = '../input/child-mind-institute-problematic-internet-use/'\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())","metadata":{"execution":{"iopub.status.busy":"2024-10-22T09:10:39.083152Z","iopub.execute_input":"2024-10-22T09:10:39.084239Z","iopub.status.idle":"2024-10-22T09:10:39.222587Z","shell.execute_reply.started":"2024-10-22T09:10:39.084187Z","shell.execute_reply":"2024-10-22T09:10:39.221014Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Feature ","metadata":{}},{"cell_type":"code","source":"#Preprocessing các column \"season\" có trong train, fill nan với giá trị 0\ntrain_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":{"execution":{"iopub.status.busy":"2024-10-22T09:10:39.226401Z","iopub.execute_input":"2024-10-22T09:10:39.226863Z","iopub.status.idle":"2024-10-22T09:10:39.300071Z","shell.execute_reply.started":"2024-10-22T09:10:39.226820Z","shell.execute_reply":"2024-10-22T09:10:39.298863Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Preprocessing các column \"season\" có trong test, fill nan với giá trị 0\ntest_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":{"execution":{"iopub.status.busy":"2024-10-22T09:10:39.301466Z","iopub.execute_input":"2024-10-22T09:10:39.301872Z","iopub.status.idle":"2024-10-22T09:10:39.327829Z","shell.execute_reply.started":"2024-10-22T09:10:39.301830Z","shell.execute_reply":"2024-10-22T09:10:39.326237Z"},"trusted":true},"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))","metadata":{"execution":{"iopub.status.busy":"2024-10-22T09:10:39.330021Z","iopub.execute_input":"2024-10-22T09:10:39.330541Z","iopub.status.idle":"2024-10-22T09:10:39.341255Z","shell.execute_reply.started":"2024-10-22T09:10:39.330496Z","shell.execute_reply":"2024-10-22T09:10:39.339413Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Bài này có hai hướng tiếp cần là classification trực tiếp với ground truth là sii\n# hoặc là regression về PCIAT-Total score rùi convert sang sii\n# như boxplot dưới thì từ 0-30 là sii=0, 30-50 là sii=1,...\nfig = px.scatter(train, x = 'PCIAT-PCIAT_Total', color = 'sii', marginal_x=\"box\", title = 'PCIAT Total')\nfig = fig.update_layout(yaxis_title=\"\")\nfig.update_yaxes(showticklabels=False)","metadata":{"execution":{"iopub.status.busy":"2024-10-22T09:10:39.343772Z","iopub.execute_input":"2024-10-22T09:10:39.344228Z","iopub.status.idle":"2024-10-22T09:10:41.936351Z","shell.execute_reply.started":"2024-10-22T09:10:39.344184Z","shell.execute_reply":"2024-10-22T09:10:41.934675Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.sii.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-10-22T09:10:41.938391Z","iopub.execute_input":"2024-10-22T09:10:41.938979Z","iopub.status.idle":"2024-10-22T09:10:41.964788Z","shell.execute_reply.started":"2024-10-22T09:10:41.938920Z","shell.execute_reply":"2024-10-22T09:10:41.963242Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"PCIAT_cols.remove('PCIAT-PCIAT_Total') #Column này sử dụng để làm giá trị Ground truth cho train\ntrain = train.drop(columns = PCIAT_cols) #Test không có các column PCIAT nên drop để về đúng format.","metadata":{"execution":{"iopub.status.busy":"2024-10-22T09:10:41.966920Z","iopub.execute_input":"2024-10-22T09:10:41.967474Z","iopub.status.idle":"2024-10-22T09:10:41.989054Z","shell.execute_reply.started":"2024-10-22T09:10:41.967404Z","shell.execute_reply":"2024-10-22T09:10:41.987795Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(train.shape)\ntrain","metadata":{"execution":{"iopub.status.busy":"2024-10-22T09:10:41.992983Z","iopub.execute_input":"2024-10-22T09:10:41.993420Z","iopub.status.idle":"2024-10-22T09:10:42.056976Z","shell.execute_reply.started":"2024-10-22T09:10:41.993378Z","shell.execute_reply":"2024-10-22T09:10:42.055503Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Plot\n","metadata":{}},{"cell_type":"code","source":"sns.countplot(train, x = 'sii').set_title('Count of sii') \n#label đang bị imbalance nhưng làm theo hướng regression nên ảnh hưởng không quá lớn\n","metadata":{"execution":{"iopub.status.busy":"2024-10-22T09:10:42.059078Z","iopub.execute_input":"2024-10-22T09:10:42.059588Z","iopub.status.idle":"2024-10-22T09:10:42.438023Z","shell.execute_reply.started":"2024-10-22T09:10:42.059542Z","shell.execute_reply":"2024-10-22T09:10:42.436708Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Plot sii theo thời gian sử dụng Internet\nvals = ['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":{"execution":{"iopub.status.busy":"2024-10-22T09:10:42.440037Z","iopub.execute_input":"2024-10-22T09:10:42.440913Z","iopub.status.idle":"2024-10-22T09:10:43.652068Z","shell.execute_reply.started":"2024-10-22T09:10:42.440866Z","shell.execute_reply":"2024-10-22T09:10:43.650669Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = train.dropna(subset='sii')\ntrain","metadata":{"execution":{"iopub.status.busy":"2024-10-22T09:10:43.653827Z","iopub.execute_input":"2024-10-22T09:10:43.654318Z","iopub.status.idle":"2024-10-22T09:10:43.711460Z","shell.execute_reply.started":"2024-10-22T09:10:43.654245Z","shell.execute_reply":"2024-10-22T09:10:43.709848Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Correlation","metadata":{}},{"cell_type":"code","source":"#Đánh gái mức độ tương quan giữa các column với ground truth\ncorr = pd.DataFrame(train.corr()['PCIAT-PCIAT_Total'].sort_values(ascending = False))\ncorr","metadata":{"execution":{"iopub.status.busy":"2024-10-22T09:10:43.713570Z","iopub.execute_input":"2024-10-22T09:10:43.714108Z","iopub.status.idle":"2024-10-22T09:10:43.760317Z","shell.execute_reply.started":"2024-10-22T09:10:43.714051Z","shell.execute_reply":"2024-10-22T09:10:43.758726Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Chọn ra các feat có corr cao với PCIAT_Total và loại bớt các feat có độ tương đồng thấp hoặc tương đương với các feat đã chọn\nselection = 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')\nselection","metadata":{"execution":{"iopub.status.busy":"2024-10-22T09:32:59.154977Z","iopub.execute_input":"2024-10-22T09:32:59.155515Z","iopub.status.idle":"2024-10-22T09:32:59.164670Z","shell.execute_reply.started":"2024-10-22T09:32:59.155470Z","shell.execute_reply":"2024-10-22T09:32:59.163111Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Missing value","metadata":{}},{"cell_type":"code","source":"train.isna().sum().sort_values(ascending = False).head(46)","metadata":{"execution":{"iopub.status.busy":"2024-10-22T09:46:46.024012Z","iopub.execute_input":"2024-10-22T09:46:46.024856Z","iopub.status.idle":"2024-10-22T09:46:46.037446Z","shell.execute_reply.started":"2024-10-22T09:46:46.024812Z","shell.execute_reply":"2024-10-22T09:46:46.036161Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Lọc ra những column có tỉ lệ nan > 0.5 \nhalf_missing = [val for val in train.columns[train.isnull().sum()>len(train)/2]]\nhalf_missing","metadata":{"execution":{"iopub.status.busy":"2024-10-22T09:50:57.926575Z","iopub.execute_input":"2024-10-22T09:50:57.927016Z","iopub.status.idle":"2024-10-22T09:50:57.938537Z","shell.execute_reply.started":"2024-10-22T09:50:57.926975Z","shell.execute_reply":"2024-10-22T09:50:57.937376Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#từ các feat đã chọn trước, lọc lại các feat có tỉ lệ nan < 0.5\nselection = [i for i in selection if i not in half_missing]\nselection","metadata":{"execution":{"iopub.status.busy":"2024-10-22T09:51:46.235978Z","iopub.execute_input":"2024-10-22T09:51:46.236504Z","iopub.status.idle":"2024-10-22T09:51:46.245657Z","shell.execute_reply.started":"2024-10-22T09:51:46.236460Z","shell.execute_reply":"2024-10-22T09:51:46.244381Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Selected Feature","metadata":{}},{"cell_type":"code","source":"describe = train[selection].describe().T\ndescribe[['min','max']].sort_index()","metadata":{"execution":{"iopub.status.busy":"2024-10-22T09:53:23.457800Z","iopub.execute_input":"2024-10-22T09:53:23.458281Z","iopub.status.idle":"2024-10-22T09:53:23.522161Z","shell.execute_reply.started":"2024-10-22T09:53:23.458224Z","shell.execute_reply":"2024-10-22T09:53:23.520674Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train[selection].hist(figsize=(10,10), grid = True)\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2024-10-22T09:53:39.031818Z","iopub.execute_input":"2024-10-22T09:53:39.032338Z","iopub.status.idle":"2024-10-22T09:53:42.981134Z","shell.execute_reply.started":"2024-10-22T09:53:39.032296Z","shell.execute_reply":"2024-10-22T09:53:42.979824Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Model","metadata":{}},{"cell_type":"code","source":"X = train[selection]\ntest = test[selection]\ny = train['PCIAT-PCIAT_Total']","metadata":{"execution":{"iopub.status.busy":"2024-10-22T09:54:24.198016Z","iopub.execute_input":"2024-10-22T09:54:24.198498Z","iopub.status.idle":"2024-10-22T09:54:24.207204Z","shell.execute_reply.started":"2024-10-22T09:54:24.198457Z","shell.execute_reply":"2024-10-22T09:54:24.205973Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def convert(scores):\n#Convert tu PCAIT_Total sang sii\n    scores = np.array(scores)*1.25\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","metadata":{"execution":{"iopub.status.busy":"2024-10-22T09:54:32.185055Z","iopub.execute_input":"2024-10-22T09:54:32.186202Z","iopub.status.idle":"2024-10-22T09:54:32.193804Z","shell.execute_reply.started":"2024-10-22T09:54:32.186149Z","shell.execute_reply":"2024-10-22T09:54:32.192425Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def 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)","metadata":{"execution":{"iopub.status.busy":"2024-10-22T09:55:04.049968Z","iopub.execute_input":"2024-10-22T09:55:04.050530Z","iopub.status.idle":"2024-10-22T09:55:04.056984Z","shell.execute_reply.started":"2024-10-22T09:55:04.050489Z","shell.execute_reply":"2024-10-22T09:55:04.055779Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"params = {'max_depth': 3, 'n_estimators': 59, 'learning_rate': 0.075, 'subsample': 0.6, 'colsample_bytree': 0.91}","metadata":{"execution":{"iopub.status.busy":"2024-10-22T09:55:35.913358Z","iopub.execute_input":"2024-10-22T09:55:35.913862Z","iopub.status.idle":"2024-10-22T09:55:35.920371Z","shell.execute_reply.started":"2024-10-22T09:55:35.913819Z","shell.execute_reply":"2024-10-22T09:55:35.919030Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"skf = StratifiedKFold(n_splits=10)\nmodel = xgb.XGBRegressor(**params)","metadata":{"execution":{"iopub.status.busy":"2024-10-22T09:56:35.679804Z","iopub.execute_input":"2024-10-22T09:56:35.680296Z","iopub.status.idle":"2024-10-22T09:56:35.686989Z","shell.execute_reply.started":"2024-10-22T09:56:35.680240Z","shell.execute_reply":"2024-10-22T09:56:35.685730Z"},"trusted":true},"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":{"execution":{"iopub.status.busy":"2024-10-22T09:56:37.494960Z","iopub.execute_input":"2024-10-22T09:56:37.495433Z","iopub.status.idle":"2024-10-22T09:56:38.312364Z","shell.execute_reply.started":"2024-10-22T09:56:37.495390Z","shell.execute_reply":"2024-10-22T09:56:38.311120Z"},"trusted":true},"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":{"execution":{"iopub.status.busy":"2024-10-22T09:57:41.996613Z","iopub.execute_input":"2024-10-22T09:57:41.997065Z","iopub.status.idle":"2024-10-22T09:57:42.085322Z","shell.execute_reply.started":"2024-10-22T09:57:41.997014Z","shell.execute_reply":"2024-10-22T09:57:42.084356Z"},"trusted":true},"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":{"execution":{"iopub.status.busy":"2024-10-22T09:58:01.665935Z","iopub.execute_input":"2024-10-22T09:58:01.667430Z","iopub.status.idle":"2024-10-22T09:58:02.101554Z","shell.execute_reply.started":"2024-10-22T09:58:01.667373Z","shell.execute_reply":"2024-10-22T09:58:02.100048Z"},"trusted":true},"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":{"execution":{"iopub.status.busy":"2024-10-22T09:58:28.837542Z","iopub.execute_input":"2024-10-22T09:58:28.838027Z","iopub.status.idle":"2024-10-22T09:58:29.381416Z","shell.execute_reply.started":"2024-10-22T09:58:28.837986Z","shell.execute_reply":"2024-10-22T09:58:29.380279Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Submission","metadata":{}},{"cell_type":"code","source":"model.fit(X,y)\npreds = model.predict(test)\npreds = convert(preds) # convert raw scores to sii categories if using regressor\npreds = pd.Series(preds)\npreds.index = test.index\npreds.to_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2024-10-22T09:58:52.550472Z","iopub.execute_input":"2024-10-22T09:58:52.550946Z","iopub.status.idle":"2024-10-22T09:58:52.637636Z","shell.execute_reply.started":"2024-10-22T09:58:52.550906Z","shell.execute_reply":"2024-10-22T09:58:52.636355Z"},"trusted":true},"outputs":[],"execution_count":null}]}