{"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":false,"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-12-03T16:54:00.349592Z","iopub.execute_input":"2024-12-03T16:54:00.350006Z","iopub.status.idle":"2024-12-03T16:54:00.355149Z","shell.execute_reply.started":"2024-12-03T16:54:00.349968Z","shell.execute_reply":"2024-12-03T16:54:00.353886Z"},"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-12-03T16:54:00.358424Z","iopub.execute_input":"2024-12-03T16:54:00.358773Z","iopub.status.idle":"2024-12-03T16:54:00.372469Z","shell.execute_reply.started":"2024-12-03T16:54:00.358740Z","shell.execute_reply":"2024-12-03T16:54:00.371280Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from lightgbm import LGBMRegressor  \n\nfrom catboost import CatBoostRegressor  ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T16:54:00.374157Z","iopub.execute_input":"2024-12-03T16:54:00.374496Z","iopub.status.idle":"2024-12-03T16:54:00.386932Z","shell.execute_reply.started":"2024-12-03T16:54:00.374464Z","shell.execute_reply":"2024-12-03T16:54:00.385852Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Data","metadata":{}},{"cell_type":"code","source":"path = '/kaggle/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-12-03T16:54:00.388199Z","iopub.execute_input":"2024-12-03T16:54:00.388622Z","iopub.status.idle":"2024-12-03T16:54:00.457485Z","shell.execute_reply.started":"2024-12-03T16:54:00.388574Z","shell.execute_reply":"2024-12-03T16:54:00.456359Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T16:54:00.460324Z","iopub.execute_input":"2024-12-03T16:54:00.460662Z","iopub.status.idle":"2024-12-03T16:54:00.467907Z","shell.execute_reply.started":"2024-12-03T16:54:00.460629Z","shell.execute_reply":"2024-12-03T16:54:00.466838Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Feature ","metadata":{}},{"cell_type":"code","source":"most_common_train = train['Physical-Season'].mode()[0]\nmost_common_train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T16:54:00.469370Z","iopub.execute_input":"2024-12-03T16:54:00.469742Z","iopub.status.idle":"2024-12-03T16:54:00.482602Z","shell.execute_reply.started":"2024-12-03T16:54:00.469711Z","shell.execute_reply":"2024-12-03T16:54:00.481542Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['Physical-Season'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T16:54:00.484318Z","iopub.execute_input":"2024-12-03T16:54:00.485178Z","iopub.status.idle":"2024-12-03T16:54:00.501468Z","shell.execute_reply.started":"2024-12-03T16:54:00.485128Z","shell.execute_reply":"2024-12-03T16:54:00.500189Z"}},"outputs":[],"execution_count":null},{"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(1)\n    train[season] = train[season].replace({'Spring':1, 'Summer':2, 'Fall':3, 'Winter':4})","metadata":{"execution":{"iopub.status.busy":"2024-12-03T16:54:00.502684Z","iopub.execute_input":"2024-12-03T16:54:00.503041Z","iopub.status.idle":"2024-12-03T16:54:00.553868Z","shell.execute_reply.started":"2024-12-03T16:54:00.503008Z","shell.execute_reply":"2024-12-03T16:54:00.552692Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['Physical-Season'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T16:54:00.555133Z","iopub.execute_input":"2024-12-03T16:54:00.555451Z","iopub.status.idle":"2024-12-03T16:54:00.563469Z","shell.execute_reply.started":"2024-12-03T16:54:00.555419Z","shell.execute_reply":"2024-12-03T16:54:00.562360Z"}},"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(1)\n    test[season] = test[season].replace({'Spring':1, 'Summer':2, 'Fall':3, 'Winter':4})","metadata":{"execution":{"iopub.status.busy":"2024-12-03T16:54:00.566215Z","iopub.execute_input":"2024-12-03T16:54:00.566584Z","iopub.status.idle":"2024-12-03T16:54:00.590389Z","shell.execute_reply.started":"2024-12-03T16:54:00.566540Z","shell.execute_reply":"2024-12-03T16:54:00.589194Z"},"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-12-03T16:54:00.592026Z","iopub.execute_input":"2024-12-03T16:54:00.592479Z","iopub.status.idle":"2024-12-03T16:54:00.599362Z","shell.execute_reply.started":"2024-12-03T16:54:00.592430Z","shell.execute_reply":"2024-12-03T16:54:00.598411Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\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-12-03T16:54:00.600723Z","iopub.execute_input":"2024-12-03T16:54:00.601107Z","iopub.status.idle":"2024-12-03T16:54:00.700812Z","shell.execute_reply.started":"2024-12-03T16:54:00.601074Z","shell.execute_reply":"2024-12-03T16:54:00.699727Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.sii.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-12-03T16:54:00.702203Z","iopub.execute_input":"2024-12-03T16:54:00.702605Z","iopub.status.idle":"2024-12-03T16:54:00.712760Z","shell.execute_reply.started":"2024-12-03T16:54:00.702564Z","shell.execute_reply":"2024-12-03T16:54:00.711564Z"},"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-12-03T16:54:00.714007Z","iopub.execute_input":"2024-12-03T16:54:00.714295Z","iopub.status.idle":"2024-12-03T16:54:00.727896Z","shell.execute_reply.started":"2024-12-03T16:54:00.714267Z","shell.execute_reply":"2024-12-03T16:54:00.726671Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(train.shape)\ntrain","metadata":{"execution":{"iopub.status.busy":"2024-12-03T16:54:00.729157Z","iopub.execute_input":"2024-12-03T16:54:00.729524Z","iopub.status.idle":"2024-12-03T16:54:00.767924Z","shell.execute_reply.started":"2024-12-03T16:54:00.729489Z","shell.execute_reply":"2024-12-03T16:54:00.766808Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['PCIAT-PCIAT_Total'].describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T16:54:00.769098Z","iopub.execute_input":"2024-12-03T16:54:00.769482Z","iopub.status.idle":"2024-12-03T16:54:00.781045Z","shell.execute_reply.started":"2024-12-03T16:54:00.769448Z","shell.execute_reply":"2024-12-03T16:54:00.779697Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['Basic_Demos-Age'].describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T16:54:00.782423Z","iopub.execute_input":"2024-12-03T16:54:00.782810Z","iopub.status.idle":"2024-12-03T16:54:00.797729Z","shell.execute_reply.started":"2024-12-03T16:54:00.782759Z","shell.execute_reply":"2024-12-03T16:54:00.796545Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T16:54:00.799260Z","iopub.execute_input":"2024-12-03T16:54:00.799590Z","iopub.status.idle":"2024-12-03T16:54:00.808539Z","shell.execute_reply.started":"2024-12-03T16:54:00.799558Z","shell.execute_reply":"2024-12-03T16:54:00.807510Z"}},"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-12-03T16:54:00.809897Z","iopub.execute_input":"2024-12-03T16:54:00.810243Z","iopub.status.idle":"2024-12-03T16:54:01.071270Z","shell.execute_reply.started":"2024-12-03T16:54:00.810213Z","shell.execute_reply":"2024-12-03T16:54:01.069872Z"},"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-12-03T16:54:01.072831Z","iopub.execute_input":"2024-12-03T16:54:01.073186Z","iopub.status.idle":"2024-12-03T16:54:01.951996Z","shell.execute_reply.started":"2024-12-03T16:54:01.073152Z","shell.execute_reply":"2024-12-03T16:54:01.950871Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = train.dropna(subset='sii')\ntrain","metadata":{"execution":{"iopub.status.busy":"2024-12-03T16:54:01.957883Z","iopub.execute_input":"2024-12-03T16:54:01.958232Z","iopub.status.idle":"2024-12-03T16:54:01.994567Z","shell.execute_reply.started":"2024-12-03T16:54:01.958201Z","shell.execute_reply":"2024-12-03T16:54:01.993334Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Correlation","metadata":{}},{"cell_type":"code","source":"#Đánh giá 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-12-03T16:54:01.995733Z","iopub.execute_input":"2024-12-03T16:54:01.996133Z","iopub.status.idle":"2024-12-03T16:54:02.037039Z","shell.execute_reply.started":"2024-12-03T16:54:01.996099Z","shell.execute_reply":"2024-12-03T16:54:02.035778Z"},"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']>.05) | (corr['PCIAT-PCIAT_Total']<-.05)]\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-12-03T16:54:02.038243Z","iopub.execute_input":"2024-12-03T16:54:02.038569Z","iopub.status.idle":"2024-12-03T16:54:02.048230Z","shell.execute_reply.started":"2024-12-03T16:54:02.038537Z","shell.execute_reply":"2024-12-03T16:54:02.047218Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T16:54:02.049519Z","iopub.execute_input":"2024-12-03T16:54:02.049960Z","iopub.status.idle":"2024-12-03T16:54:02.066207Z","shell.execute_reply.started":"2024-12-03T16:54:02.049925Z","shell.execute_reply":"2024-12-03T16:54:02.065069Z"}},"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-12-03T16:54:02.067311Z","iopub.execute_input":"2024-12-03T16:54:02.067631Z","iopub.status.idle":"2024-12-03T16:54:02.084812Z","shell.execute_reply.started":"2024-12-03T16:54:02.067599Z","shell.execute_reply":"2024-12-03T16:54:02.083563Z"},"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)/3]]\nhalf_missing","metadata":{"execution":{"iopub.status.busy":"2024-12-03T16:54:02.086131Z","iopub.execute_input":"2024-12-03T16:54:02.086469Z","iopub.status.idle":"2024-12-03T16:54:02.099538Z","shell.execute_reply.started":"2024-12-03T16:54:02.086436Z","shell.execute_reply":"2024-12-03T16:54:02.098405Z"},"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-12-03T16:54:02.101093Z","iopub.execute_input":"2024-12-03T16:54:02.101469Z","iopub.status.idle":"2024-12-03T16:54:02.114119Z","shell.execute_reply.started":"2024-12-03T16:54:02.101422Z","shell.execute_reply":"2024-12-03T16:54:02.112840Z"},"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-12-03T16:54:02.115385Z","iopub.execute_input":"2024-12-03T16:54:02.115761Z","iopub.status.idle":"2024-12-03T16:54:02.184404Z","shell.execute_reply.started":"2024-12-03T16:54:02.115724Z","shell.execute_reply":"2024-12-03T16:54:02.183358Z"},"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-12-03T16:54:02.186307Z","iopub.execute_input":"2024-12-03T16:54:02.186717Z","iopub.status.idle":"2024-12-03T16:54:06.060772Z","shell.execute_reply.started":"2024-12-03T16:54:02.186683Z","shell.execute_reply":"2024-12-03T16:54:06.059615Z"},"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-12-03T16:54:06.062070Z","iopub.execute_input":"2024-12-03T16:54:06.062356Z","iopub.status.idle":"2024-12-03T16:54:06.070833Z","shell.execute_reply.started":"2024-12-03T16:54:06.062329Z","shell.execute_reply":"2024-12-03T16:54:06.069849Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T16:54:06.072095Z","iopub.execute_input":"2024-12-03T16:54:06.072426Z","iopub.status.idle":"2024-12-03T16:54:06.112000Z","shell.execute_reply.started":"2024-12-03T16:54:06.072394Z","shell.execute_reply":"2024-12-03T16:54:06.110875Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def convert(scores):\n#Convert tu PCAIT_Total sang sii\n    scores = np.array(scores)*1.02\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-12-03T16:56:09.210417Z","iopub.execute_input":"2024-12-03T16:56:09.210833Z","iopub.status.idle":"2024-12-03T16:56:09.216892Z","shell.execute_reply.started":"2024-12-03T16:56:09.210782Z","shell.execute_reply":"2024-12-03T16:56:09.215755Z"},"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\n","metadata":{"execution":{"iopub.status.busy":"2024-12-03T16:56:10.040493Z","iopub.execute_input":"2024-12-03T16:56:10.040901Z","iopub.status.idle":"2024-12-03T16:56:10.047297Z","shell.execute_reply.started":"2024-12-03T16:56:10.040867Z","shell.execute_reply":"2024-12-03T16:56:10.046135Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"kappa_scorer = make_scorer(quadratic_kappa, greater_is_better=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T16:54:06.138063Z","iopub.execute_input":"2024-12-03T16:54:06.138489Z","iopub.status.idle":"2024-12-03T16:54:06.151533Z","shell.execute_reply.started":"2024-12-03T16:54:06.138441Z","shell.execute_reply":"2024-12-03T16:54:06.150196Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"XGB_Params ={\n    'max_depth': 3,\n    'n_estimators': 59,\n    'learning_rate': 0.075,\n    'subsample': 0.6,\n    'colsample_bytree': 0.91\n}\nlgb_Params =  {\n    'learning_rate': 0.046,\n    'max_depth': 12,\n    'num_leaves': 478,\n    'min_data_in_leaf': 13,\n    'feature_fraction': 0.893,\n    'bagging_fraction': 0.784,\n    'bagging_freq': 4,\n    'lambda_l1': 10,  # Increased from 6.59\n    'lambda_l2': 0.01,  # Increased from 2.68e-06\n    'device': 'cpu',\n}\n\n\n\nCAT_Params = {\n    'iterations': 500,\n    'learning_rate': 0.009,\n    'depth': 6,\n}","metadata":{"execution":{"iopub.status.busy":"2024-12-03T16:54:06.153022Z","iopub.execute_input":"2024-12-03T16:54:06.153464Z","iopub.status.idle":"2024-12-03T16:54:06.165620Z","shell.execute_reply.started":"2024-12-03T16:54:06.153417Z","shell.execute_reply":"2024-12-03T16:54:06.164626Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.ensemble import VotingRegressor  ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T16:54:06.166872Z","iopub.execute_input":"2024-12-03T16:54:06.167259Z","iopub.status.idle":"2024-12-03T16:54:06.184187Z","shell.execute_reply.started":"2024-12-03T16:54:06.167211Z","shell.execute_reply":"2024-12-03T16:54:06.183063Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lgb_model = LGBMRegressor(**lgb_Params, silent=True)\nxgb_model = xgb.XGBRegressor(**XGB_Params)\ncat_model = CatBoostRegressor(**CAT_Params, silent=True)\n\n# Define a voting regressor\nvoting_model = VotingRegressor(estimators=[\n    # ('lgb', lgb_model),\n    ('xgb', xgb_model),\n    ('cat', cat_model)\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T16:54:06.185499Z","iopub.execute_input":"2024-12-03T16:54:06.186006Z","iopub.status.idle":"2024-12-03T16:54:06.200815Z","shell.execute_reply.started":"2024-12-03T16:54:06.185953Z","shell.execute_reply":"2024-12-03T16:54:06.199757Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"skf = StratifiedKFold(n_splits=10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T16:54:06.202219Z","iopub.execute_input":"2024-12-03T16:54:06.202643Z","iopub.status.idle":"2024-12-03T16:54:06.215289Z","shell.execute_reply.started":"2024-12-03T16:54:06.202598Z","shell.execute_reply":"2024-12-03T16:54:06.213991Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"scores = cross_val_score(voting_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-12-03T16:56:15.445470Z","iopub.execute_input":"2024-12-03T16:56:15.445900Z","iopub.status.idle":"2024-12-03T16:56:23.836106Z","shell.execute_reply.started":"2024-12-03T16:56:15.445866Z","shell.execute_reply":"2024-12-03T16:56:23.835111Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# QWK Scores: [0.41115612 0.48684027 0.52362393 0.44191886 0.50664025 0.42045433\n#  0.45230024 0.40100143 0.48210898 0.43408074]\n# Mean QWK Score: 0.4560125145441126","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# skf = StratifiedKFold(n_splits=10)\n\n# scores = cross_val_score(cat_model, X, y, cv=skf, scoring=kappa_scorer)\n# print(\"QWK Scores:\", scores)\n# print(\"Mean QWK Score:\", np.mean(scores))","metadata":{"execution":{"iopub.status.busy":"2024-12-03T16:54:14.661659Z","iopub.execute_input":"2024-12-03T16:54:14.662137Z","iopub.status.idle":"2024-12-03T16:54:14.667400Z","shell.execute_reply.started":"2024-12-03T16:54:14.662089Z","shell.execute_reply":"2024-12-03T16:54:14.666100Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# scores = cross_val_score(xgb_model, X, y, cv=skf, scoring=kappa_scorer)\n# print(\"QWK Scores:\", scores)\n# print(\"Mean QWK Score:\", np.mean(scores))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T16:54:14.668678Z","iopub.execute_input":"2024-12-03T16:54:14.669157Z","iopub.status.idle":"2024-12-03T16:54:14.680206Z","shell.execute_reply.started":"2024-12-03T16:54:14.669116Z","shell.execute_reply":"2024-12-03T16:54:14.678707Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# scores = cross_val_score(lgb_model, X, y, cv=skf, scoring=kappa_scorer, verbose=-1)\n# print(\"QWK Scores:\", scores)\n# print(\"Mean QWK Score:\", np.mean(scores))","metadata":{"execution":{"iopub.status.busy":"2024-12-03T16:54:14.681589Z","iopub.execute_input":"2024-12-03T16:54:14.682081Z","iopub.status.idle":"2024-12-03T16:54:14.694215Z","shell.execute_reply.started":"2024-12-03T16:54:14.681988Z","shell.execute_reply":"2024-12-03T16:54:14.692953Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Submission","metadata":{}},{"cell_type":"code","source":"# cat_model.fit(X,y)\n# preds_cat = cat_model.predict(test)\n# print(preds_cat)\n# preds_cat = convert(preds_cat) # convert raw scores to sii categories if using regressor\n# preds_cat = pd.Series(preds_cat)\n# preds_cat\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T16:54:14.695496Z","iopub.execute_input":"2024-12-03T16:54:14.695833Z","iopub.status.idle":"2024-12-03T16:54:14.707287Z","shell.execute_reply.started":"2024-12-03T16:54:14.695776Z","shell.execute_reply":"2024-12-03T16:54:14.706227Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# xgb_model.fit(X,y);\n# preds_xgb = xgb_model.predict(test)\n# print(preds_xgb)\n# preds_xgb = convert(preds_xgb) # convert raw scores to sii categories if using regressor\n# preds_xgb = pd.Series(preds_xgb)\n# preds_xgb\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T16:54:14.708502Z","iopub.execute_input":"2024-12-03T16:54:14.708877Z","iopub.status.idle":"2024-12-03T16:54:14.718672Z","shell.execute_reply.started":"2024-12-03T16:54:14.708843Z","shell.execute_reply":"2024-12-03T16:54:14.717683Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"# lgb_model.fit(X,y,verbose);\n# preds_lgb= lgb_model.predict(test)\n# print(preds_lgb)\n# preds_lgb = convert(preds_lgb) # convert raw scores to sii categories if using regressor\n# preds_lgb = pd.Series(preds_lgb)\n# preds_lgb\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T16:54:14.720240Z","iopub.execute_input":"2024-12-03T16:54:14.720581Z","iopub.status.idle":"2024-12-03T16:54:14.734421Z","shell.execute_reply.started":"2024-12-03T16:54:14.720548Z","shell.execute_reply":"2024-12-03T16:54:14.733290Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"voting_model.fit(X,y)\npreds = voting_model.predict(test)\nprint(preds)\npreds = convert(preds) # convert raw scores to sii categories if using regressor\npreds = pd.Series(preds)\npreds","metadata":{"execution":{"iopub.status.busy":"2024-12-03T16:56:54.397553Z","iopub.execute_input":"2024-12-03T16:56:54.397983Z","iopub.status.idle":"2024-12-03T16:56:55.327671Z","shell.execute_reply.started":"2024-12-03T16:56:54.397937Z","shell.execute_reply":"2024-12-03T16:56:55.325863Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# scores_qwk = cross_val_score(voting_model, X, y, cv=skf, scoring=kappa_scorer)\n# print(\"QWK Scores:\", scores_qwk)\n# print(\"Mean QWK Score:\", np.mean(scores_qwk))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T16:54:15.614478Z","iopub.execute_input":"2024-12-03T16:54:15.614827Z","iopub.status.idle":"2024-12-03T16:54:15.619219Z","shell.execute_reply.started":"2024-12-03T16:54:15.614769Z","shell.execute_reply":"2024-12-03T16:54:15.618220Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# merged_preds = pd.DataFrame({\n#     'sii_cat': preds_cat,\n#     'sii_xgb': preds_xgb,\n#     'sii_lgb': preds_lgb\n# })\n# merged_preds['vote'] = merged_preds.apply(lambda row: np.bincount(row.astype(int)).argmax(), axis=1)\n# merged_preds","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T16:54:15.620403Z","iopub.execute_input":"2024-12-03T16:54:15.620720Z","iopub.status.idle":"2024-12-03T16:54:15.633030Z","shell.execute_reply.started":"2024-12-03T16:54:15.620691Z","shell.execute_reply":"2024-12-03T16:54:15.631867Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.DataFrame({\n        'id': test.index,  \n        'sii': preds #merged_preds['vote']\n})\nsubmission.to_csv('submission.csv', index=False)\nsubmission","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T16:54:15.634502Z","iopub.execute_input":"2024-12-03T16:54:15.634971Z","iopub.status.idle":"2024-12-03T16:54:15.655466Z","shell.execute_reply.started":"2024-12-03T16:54:15.634925Z","shell.execute_reply":"2024-12-03T16:54:15.654004Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 0     1.0\n# 1     0.0\n# 2     1.0\n# 3     0.0\n# 4     1.0\n# 5     1.0\n# 6     1.0\n# 7     1.0\n# 8     1.0\n# 9     1.0\n# 10    1.0\n# 11    1.0\n# 12    1.0\n# 13    1.0\n# 14    1.0\n# 15    1.0\n# 16    0.0\n# 17    0.0\n# 18    1.0\n# 19    1.0","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T16:54:15.657289Z","iopub.execute_input":"2024-12-03T16:54:15.658023Z","iopub.status.idle":"2024-12-03T16:54:15.664160Z","shell.execute_reply.started":"2024-12-03T16:54:15.657967Z","shell.execute_reply":"2024-12-03T16:54:15.662678Z"}},"outputs":[],"execution_count":null}]}