{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":81933,"databundleVersionId":9643020,"sourceType":"competition"}],"dockerImageVersionId":30804,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## ****Librarys****","metadata":{}},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport torch\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\nfrom sklearn.model_selection import GridSearchCV, StratifiedKFold\nfrom sklearn.metrics import classification_report, confusion_matrix, roc_auc_score, RocCurveDisplay, accuracy_score\nfrom sklearn.metrics import roc_curve, auc\nfrom sklearn.preprocessing import StandardScaler, label_binarize, PolynomialFeatures\nfrom sklearn import preprocessing\nfrom sklearn.decomposition import PCA\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score\nfrom xgboost import XGBClassifier\nfrom sklearn.neural_network import MLPClassifier\nfrom lightgbm import LGBMClassifier\nfrom sklearn.svm import SVC\nfrom sklearn.ensemble import VotingClassifier, RandomForestClassifier, GradientBoostingClassifier\nfrom sklearn.linear_model import LogisticRegression","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:23.586663Z","iopub.execute_input":"2024-12-04T02:04:23.587288Z","iopub.status.idle":"2024-12-04T02:04:33.272197Z","shell.execute_reply.started":"2024-12-04T02:04:23.587253Z","shell.execute_reply":"2024-12-04T02:04:33.271484Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Dataset","metadata":{}},{"cell_type":"code","source":"train_ds = pd.read_csv('/kaggle/input/child-mind-institute-problematic-internet-use/train.csv', index_col='id')\ntest_ds = pd.read_csv('/kaggle/input/child-mind-institute-problematic-internet-use/test.csv', index_col='id')\ndata_dictionary = pd.read_csv('/kaggle/input/child-mind-institute-problematic-internet-use/data_dictionary.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:33.273628Z","iopub.execute_input":"2024-12-04T02:04:33.274575Z","iopub.status.idle":"2024-12-04T02:04:33.352925Z","shell.execute_reply.started":"2024-12-04T02:04:33.274545Z","shell.execute_reply":"2024-12-04T02:04:33.352021Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## What's in the ds?","metadata":{}},{"cell_type":"code","source":"train_ds.tail()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:33.353948Z","iopub.execute_input":"2024-12-04T02:04:33.354313Z","iopub.status.idle":"2024-12-04T02:04:33.384530Z","shell.execute_reply.started":"2024-12-04T02:04:33.354272Z","shell.execute_reply":"2024-12-04T02:04:33.383779Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_ds.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:33.386765Z","iopub.execute_input":"2024-12-04T02:04:33.387107Z","iopub.status.idle":"2024-12-04T02:04:33.507306Z","shell.execute_reply.started":"2024-12-04T02:04:33.387071Z","shell.execute_reply":"2024-12-04T02:04:33.506498Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_ds.columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:33.508300Z","iopub.execute_input":"2024-12-04T02:04:33.508551Z","iopub.status.idle":"2024-12-04T02:04:33.515178Z","shell.execute_reply.started":"2024-12-04T02:04:33.508527Z","shell.execute_reply":"2024-12-04T02:04:33.514400Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_ds.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:33.516188Z","iopub.execute_input":"2024-12-04T02:04:33.516457Z","iopub.status.idle":"2024-12-04T02:04:33.527209Z","shell.execute_reply.started":"2024-12-04T02:04:33.516432Z","shell.execute_reply":"2024-12-04T02:04:33.526364Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_ds.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:33.528124Z","iopub.execute_input":"2024-12-04T02:04:33.528382Z","iopub.status.idle":"2024-12-04T02:04:33.689291Z","shell.execute_reply.started":"2024-12-04T02:04:33.528357Z","shell.execute_reply":"2024-12-04T02:04:33.688351Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_ds.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:33.690402Z","iopub.execute_input":"2024-12-04T02:04:33.690682Z","iopub.status.idle":"2024-12-04T02:04:33.703013Z","shell.execute_reply.started":"2024-12-04T02:04:33.690641Z","shell.execute_reply":"2024-12-04T02:04:33.702174Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## A little Analysis","metadata":{}},{"cell_type":"markdown","source":"~ Using as a reference the notebook with 0.463 of accuracy","metadata":{}},{"cell_type":"code","source":"train_cat_columns = train_ds.select_dtypes(exclude = 'number').columns\n\nfor season in train_cat_columns:\n    train_ds[season] = train_ds[season].fillna(0)\n    train_ds[season] = train_ds[season].replace({'Spring':1, 'Summer':2, 'Fall':3, 'Winter':4})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:33.704054Z","iopub.execute_input":"2024-12-04T02:04:33.704351Z","iopub.status.idle":"2024-12-04T02:04:33.741126Z","shell.execute_reply.started":"2024-12-04T02:04:33.704318Z","shell.execute_reply":"2024-12-04T02:04:33.740241Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_cat_columns = test_ds.select_dtypes(exclude = 'number').columns\n\nfor season in test_cat_columns:\n    test_ds[season] = test_ds[season].fillna(0)\n    test_ds[season] = test_ds[season].replace({'Spring':1, 'Summer':2, 'Fall':3, 'Winter':4})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:33.743621Z","iopub.execute_input":"2024-12-04T02:04:33.743944Z","iopub.status.idle":"2024-12-04T02:04:33.758049Z","shell.execute_reply.started":"2024-12-04T02:04:33.743919Z","shell.execute_reply":"2024-12-04T02:04:33.757240Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"PCIAT_cols = [val for val in train_ds.columns[train_ds.columns.str.contains('PCIAT')]]\nprint('Number of PCIAT features = ' , len(PCIAT_cols))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:33.759057Z","iopub.execute_input":"2024-12-04T02:04:33.759303Z","iopub.status.idle":"2024-12-04T02:04:33.770632Z","shell.execute_reply.started":"2024-12-04T02:04:33.759279Z","shell.execute_reply":"2024-12-04T02:04:33.769789Z"}},"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-12-04T02:04:33.771619Z","iopub.execute_input":"2024-12-04T02:04:33.771846Z","iopub.status.idle":"2024-12-04T02:04:33.790945Z","shell.execute_reply.started":"2024-12-04T02:04:33.771823Z","shell.execute_reply":"2024-12-04T02:04:33.790178Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Correlations ","metadata":{}},{"cell_type":"code","source":"#Correlacion\ncorr = train_ds[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-12-04T02:04:33.792005Z","iopub.execute_input":"2024-12-04T02:04:33.792335Z","iopub.status.idle":"2024-12-04T02:04:33.849853Z","shell.execute_reply.started":"2024-12-04T02:04:33.792298Z","shell.execute_reply":"2024-12-04T02:04:33.849003Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.boxplot(train_ds, x = 'PCIAT-PCIAT_Total').set_title('Boxplot of PCIAT Total Scores')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:33.850956Z","iopub.execute_input":"2024-12-04T02:04:33.851209Z","iopub.status.idle":"2024-12-04T02:04:34.067738Z","shell.execute_reply.started":"2024-12-04T02:04:33.851184Z","shell.execute_reply":"2024-12-04T02:04:34.066912Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(train_ds[train_ds['PCIAT-PCIAT_Total']<=30].sii.value_counts())\nprint(train_ds[(train_ds['PCIAT-PCIAT_Total']>30) \n    & (train_ds['PCIAT-PCIAT_Total']<50)].sii.value_counts())\nprint(train_ds[(train_ds['PCIAT-PCIAT_Total']>=50) \n    & (train_ds['PCIAT-PCIAT_Total']<80)].sii.value_counts())\nprint(train_ds[train_ds['PCIAT-PCIAT_Total']>=80].sii.value_counts())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:34.068679Z","iopub.execute_input":"2024-12-04T02:04:34.068948Z","iopub.status.idle":"2024-12-04T02:04:34.083579Z","shell.execute_reply.started":"2024-12-04T02:04:34.068923Z","shell.execute_reply":"2024-12-04T02:04:34.082752Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_ds.sii.value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:34.084491Z","iopub.execute_input":"2024-12-04T02:04:34.084735Z","iopub.status.idle":"2024-12-04T02:04:34.092903Z","shell.execute_reply.started":"2024-12-04T02:04:34.084712Z","shell.execute_reply":"2024-12-04T02:04:34.092045Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"PCIAT_cols.remove('PCIAT-PCIAT_Total')\ntrain_ds = train_ds.drop(columns = PCIAT_cols)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:34.093801Z","iopub.execute_input":"2024-12-04T02:04:34.094072Z","iopub.status.idle":"2024-12-04T02:04:34.105024Z","shell.execute_reply.started":"2024-12-04T02:04:34.094047Z","shell.execute_reply":"2024-12-04T02:04:34.104223Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.countplot(train_ds, x = 'sii').set_title('Count of sii')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:34.105903Z","iopub.execute_input":"2024-12-04T02:04:34.106115Z","iopub.status.idle":"2024-12-04T02:04:34.312759Z","shell.execute_reply.started":"2024-12-04T02:04:34.106093Z","shell.execute_reply":"2024-12-04T02:04:34.311972Z"}},"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_ds[train_ds.sii==i]['PreInt_EduHx-computerinternet_hoursday'])\n    plot.set_title(vals[i])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:34.313758Z","iopub.execute_input":"2024-12-04T02:04:34.314016Z","iopub.status.idle":"2024-12-04T02:04:34.926112Z","shell.execute_reply.started":"2024-12-04T02:04:34.313992Z","shell.execute_reply":"2024-12-04T02:04:34.925280Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_ds = train_ds.dropna(subset='sii')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:34.927207Z","iopub.execute_input":"2024-12-04T02:04:34.927466Z","iopub.status.idle":"2024-12-04T02:04:34.934210Z","shell.execute_reply.started":"2024-12-04T02:04:34.927438Z","shell.execute_reply":"2024-12-04T02:04:34.933363Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Correlations with 'PCIAT-PCIAT_Total'","metadata":{}},{"cell_type":"code","source":"corr = pd.DataFrame(train_ds.corr()['PCIAT-PCIAT_Total'].sort_values(ascending = False))\ncorr.style.background_gradient(cmap='YlOrRd')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:34.935221Z","iopub.execute_input":"2024-12-04T02:04:34.935555Z","iopub.status.idle":"2024-12-04T02:04:34.974575Z","shell.execute_reply.started":"2024-12-04T02:04:34.935518Z","shell.execute_reply":"2024-12-04T02:04:34.973819Z"}},"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-12-04T02:04:34.975559Z","iopub.execute_input":"2024-12-04T02:04:34.975800Z","iopub.status.idle":"2024-12-04T02:04:34.981350Z","shell.execute_reply.started":"2024-12-04T02:04:34.975776Z","shell.execute_reply":"2024-12-04T02:04:34.980522Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"selection","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:34.982426Z","iopub.execute_input":"2024-12-04T02:04:34.982687Z","iopub.status.idle":"2024-12-04T02:04:34.994873Z","shell.execute_reply.started":"2024-12-04T02:04:34.982662Z","shell.execute_reply":"2024-12-04T02:04:34.994147Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"null = train_ds.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-12-04T02:04:34.995943Z","iopub.execute_input":"2024-12-04T02:04:34.996301Z","iopub.status.idle":"2024-12-04T02:04:35.015709Z","shell.execute_reply.started":"2024-12-04T02:04:34.996265Z","shell.execute_reply":"2024-12-04T02:04:35.014776Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"half_missing = [val for val in train_ds.columns[train_ds.isnull().sum()>len(train_ds)/2]]\nhalf_missing","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:35.016782Z","iopub.execute_input":"2024-12-04T02:04:35.017130Z","iopub.status.idle":"2024-12-04T02:04:35.024409Z","shell.execute_reply.started":"2024-12-04T02:04:35.017094Z","shell.execute_reply":"2024-12-04T02:04:35.023581Z"}},"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-12-04T02:04:35.025458Z","iopub.execute_input":"2024-12-04T02:04:35.025813Z","iopub.status.idle":"2024-12-04T02:04:35.037187Z","shell.execute_reply.started":"2024-12-04T02:04:35.025767Z","shell.execute_reply":"2024-12-04T02:04:35.036368Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"describe = train_ds[selection].describe().T\ndescribe = describe[['min','max']].sort_index()\ndescribe.style.background_gradient(cmap='YlOrRd')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:35.038048Z","iopub.execute_input":"2024-12-04T02:04:35.038305Z","iopub.status.idle":"2024-12-04T02:04:35.080020Z","shell.execute_reply.started":"2024-12-04T02:04:35.038282Z","shell.execute_reply":"2024-12-04T02:04:35.079268Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_ds[selection].hist(figsize=(10,10), grid = True, color = 'blue')\nplt.tight_layout()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:35.084292Z","iopub.execute_input":"2024-12-04T02:04:35.084544Z","iopub.status.idle":"2024-12-04T02:04:37.661399Z","shell.execute_reply.started":"2024-12-04T02:04:35.084520Z","shell.execute_reply":"2024-12-04T02:04:37.660584Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_ds = train_ds.dropna(subset=selection)\nX = train_ds[selection]\ny = train_ds['sii']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:37.662464Z","iopub.execute_input":"2024-12-04T02:04:37.662727Z","iopub.status.idle":"2024-12-04T02:04:37.671500Z","shell.execute_reply.started":"2024-12-04T02:04:37.662701Z","shell.execute_reply":"2024-12-04T02:04:37.670653Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# División en entrenamiento y validación\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:37.672578Z","iopub.execute_input":"2024-12-04T02:04:37.672884Z","iopub.status.idle":"2024-12-04T02:04:37.681603Z","shell.execute_reply.started":"2024-12-04T02:04:37.672838Z","shell.execute_reply":"2024-12-04T02:04:37.680902Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#PCA\ndef apply_pca(X_train, X_val, test_data, n_components=10):\n    scaler = preprocessing.RobustScaler()\n    X_train_scaled = scaler.fit_transform(X_train)\n    X_val_scaled = scaler.transform(X_val)\n    test_scaled = scaler.transform(test_data)\n\n    pca = PCA(n_components=n_components)\n    X_train_pca = pca.fit_transform(X_train_scaled)\n    X_val_pca = pca.transform(X_val_scaled)\n    test_pca = pca.transform(test_scaled)\n\n    print(f\"Varianza explicada por los {n_components} componentes principales: {sum(pca.explained_variance_ratio_):.2f}\")\n    return X_train_pca, X_val_pca, test_pca","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:37.682543Z","iopub.execute_input":"2024-12-04T02:04:37.682842Z","iopub.status.idle":"2024-12-04T02:04:37.690348Z","shell.execute_reply.started":"2024-12-04T02:04:37.682816Z","shell.execute_reply":"2024-12-04T02:04:37.689612Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def visualize_pca(X, y, title=\"PCA Visualization\"):\n    from sklearn.decomposition import PCA\n    X_numpy = X.cpu().numpy() if isinstance(X, torch.Tensor) else X  # Convertir tensores a NumPy si es necesario\n    pca = PCA(n_components=2)\n    X_pca = pca.fit_transform(X_numpy)\n    plt.figure(figsize=(8, 6))\n    scatter = plt.scatter(X_pca[:, 0], X_pca[:, 1], c=y, cmap='viridis', edgecolor='k', alpha=0.7)\n    plt.title(title)\n    plt.xlabel(\"Principal Component 1\")\n    plt.ylabel(\"Principal Component 2\")\n    plt.colorbar(scatter, label=\"Classes\")\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:37.691521Z","iopub.execute_input":"2024-12-04T02:04:37.691889Z","iopub.status.idle":"2024-12-04T02:04:37.700380Z","shell.execute_reply.started":"2024-12-04T02:04:37.691831Z","shell.execute_reply":"2024-12-04T02:04:37.699484Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_ds[selection].isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:37.701480Z","iopub.execute_input":"2024-12-04T02:04:37.701839Z","iopub.status.idle":"2024-12-04T02:04:37.713571Z","shell.execute_reply.started":"2024-12-04T02:04:37.701802Z","shell.execute_reply":"2024-12-04T02:04:37.712811Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_ds_cleanV1 = test_ds.dropna(subset=selection)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:37.714498Z","iopub.execute_input":"2024-12-04T02:04:37.714730Z","iopub.status.idle":"2024-12-04T02:04:37.724773Z","shell.execute_reply.started":"2024-12-04T02:04:37.714708Z","shell.execute_reply":"2024-12-04T02:04:37.724163Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_ds_V2 = test_ds.dropna()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:37.725840Z","iopub.execute_input":"2024-12-04T02:04:37.726177Z","iopub.status.idle":"2024-12-04T02:04:37.735909Z","shell.execute_reply.started":"2024-12-04T02:04:37.726151Z","shell.execute_reply":"2024-12-04T02:04:37.735049Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_ds_imputed = test_ds.fillna(test_ds.mean())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:37.736930Z","iopub.execute_input":"2024-12-04T02:04:37.737192Z","iopub.status.idle":"2024-12-04T02:04:37.764696Z","shell.execute_reply.started":"2024-12-04T02:04:37.737157Z","shell.execute_reply":"2024-12-04T02:04:37.763908Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_ds_selec = test_ds_imputed[selection]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:37.765512Z","iopub.execute_input":"2024-12-04T02:04:37.765736Z","iopub.status.idle":"2024-12-04T02:04:37.771123Z","shell.execute_reply.started":"2024-12-04T02:04:37.765713Z","shell.execute_reply":"2024-12-04T02:04:37.770325Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_pca, X_val_pca, test_pca = apply_pca(X_train, X_val, test_ds_selec, n_components=10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:42.445353Z","iopub.execute_input":"2024-12-04T02:04:42.445683Z","iopub.status.idle":"2024-12-04T02:04:42.482023Z","shell.execute_reply.started":"2024-12-04T02:04:42.445655Z","shell.execute_reply":"2024-12-04T02:04:42.480315Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"visualize_pca(X_train_pca, y_train, title=\"PCA on Training Data\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:42.622676Z","iopub.execute_input":"2024-12-04T02:04:42.622962Z","iopub.status.idle":"2024-12-04T02:04:42.968622Z","shell.execute_reply.started":"2024-12-04T02:04:42.622936Z","shell.execute_reply":"2024-12-04T02:04:42.967674Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### DATA AUMENTATION!!!","metadata":{}},{"cell_type":"code","source":"poly = PolynomialFeatures(degree=3, interaction_only=False, include_bias=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:44.598312Z","iopub.execute_input":"2024-12-04T02:04:44.599187Z","iopub.status.idle":"2024-12-04T02:04:44.603089Z","shell.execute_reply.started":"2024-12-04T02:04:44.599150Z","shell.execute_reply":"2024-12-04T02:04:44.602138Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_poly = poly.fit_transform(X_train)\nfeature_names = poly.get_feature_names_out(input_features=X_train.columns)\nX_train = pd.DataFrame(X_poly, columns=feature_names)\n\nX_poly_val = poly.fit_transform(X_val)\nfeature_names_val = poly.get_feature_names_out(input_features=X_val.columns)\nX_val = pd.DataFrame(X_poly_val, columns=feature_names)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:48.459918Z","iopub.execute_input":"2024-12-04T02:04:48.460802Z","iopub.status.idle":"2024-12-04T02:04:48.491073Z","shell.execute_reply.started":"2024-12-04T02:04:48.460766Z","shell.execute_reply":"2024-12-04T02:04:48.490209Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Ensemble","metadata":{}},{"cell_type":"markdown","source":"We will use XGBClassifier, MLPClassifier y RandomForest","metadata":{}},{"cell_type":"code","source":"max_depth = 5\nnum_leaves = 2 ** max_depth - 1\n\n#Base models\nrf_model = RandomForestClassifier(\n    n_estimators=200,\n    max_depth=10, # not 5\n    random_state=42\n)\n\nxgb_model = XGBClassifier(\n    objective='multi:softprob',\n    num_class=4,\n    n_estimators=100,\n    max_depth=max_depth,\n    learning_rate=0.1,\n    subsample=0.8,\n    random_state=42\n)\n\nmlp_model = MLPClassifier(\n    hidden_layer_sizes=(128, 64),\n    activation='relu',\n    solver='adam',\n    max_iter=300,\n    random_state=42\n)\n\nlgbm_model = LGBMClassifier(\n    objective='multiclass',\n    num_class=4,\n    n_estimators=100,\n    max_depth=max_depth,\n    num_leaves=num_leaves,\n    learning_rate=0.1,\n    subsample=0.8,\n    random_state=42\n)\n\nsvc_model = SVC(\n    probability=True, #Proba activated\n    kernel='rbf',\n    C=1,\n    random_state=42\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:51.471023Z","iopub.execute_input":"2024-12-04T02:04:51.471589Z","iopub.status.idle":"2024-12-04T02:04:51.477370Z","shell.execute_reply.started":"2024-12-04T02:04:51.471556Z","shell.execute_reply":"2024-12-04T02:04:51.476549Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#FULL POWER >:D\n\nensemble_model = VotingClassifier(estimators=[\n    ('rf', rf_model),\n    ('xgb', xgb_model),\n    ('mlp', mlp_model),\n    ('lgbm', lgbm_model),\n    ('svc', svc_model)\n], voting='soft')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:52.655602Z","iopub.execute_input":"2024-12-04T02:04:52.655964Z","iopub.status.idle":"2024-12-04T02:04:52.660391Z","shell.execute_reply.started":"2024-12-04T02:04:52.655934Z","shell.execute_reply":"2024-12-04T02:04:52.659416Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ensemble_model.fit(X_train, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:04:54.570940Z","iopub.execute_input":"2024-12-04T02:04:54.571506Z","iopub.status.idle":"2024-12-04T02:05:17.996541Z","shell.execute_reply.started":"2024-12-04T02:04:54.571469Z","shell.execute_reply":"2024-12-04T02:05:17.995657Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_val_pred = ensemble_model.predict(X_val)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:05:18.259989Z","iopub.execute_input":"2024-12-04T02:05:18.260781Z","iopub.status.idle":"2024-12-04T02:05:18.599288Z","shell.execute_reply.started":"2024-12-04T02:05:18.260749Z","shell.execute_reply":"2024-12-04T02:05:18.598394Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Métricas de evaluación\nprint(\"Classification Report:\\n\", classification_report(y_val, y_val_pred))\nprint(\"Accuracy:\", accuracy_score(y_val, y_val_pred))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:05:20.332392Z","iopub.execute_input":"2024-12-04T02:05:20.333314Z","iopub.status.idle":"2024-12-04T02:05:20.352296Z","shell.execute_reply.started":"2024-12-04T02:05:20.333277Z","shell.execute_reply":"2024-12-04T02:05:20.351334Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### CONFUSION MATRIX","metadata":{}},{"cell_type":"code","source":"# Matriz de confusión\nconf_matrix = confusion_matrix(y_val, y_val_pred)\nsns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues')\nplt.title(\"Confusion Matrix\")\nplt.xlabel(\"Predicted\")\nplt.ylabel(\"True\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:05:24.405703Z","iopub.execute_input":"2024-12-04T02:05:24.406068Z","iopub.status.idle":"2024-12-04T02:05:24.680154Z","shell.execute_reply.started":"2024-12-04T02:05:24.406035Z","shell.execute_reply":"2024-12-04T02:05:24.679353Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Predictions","metadata":{}},{"cell_type":"markdown","source":"We will get predictions with the Pipeline for this version.","metadata":{}},{"cell_type":"code","source":"test_poly = poly.fit_transform(test_ds[selection].fillna(test_ds[selection].mean()))\nfeature_names_test = poly.get_feature_names_out(input_features=test_ds[selection].fillna(test_ds[selection].mean()).columns)\ntest = pd.DataFrame(test_poly, columns=feature_names_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:07:58.047141Z","iopub.execute_input":"2024-12-04T02:07:58.047489Z","iopub.status.idle":"2024-12-04T02:07:58.076825Z","shell.execute_reply.started":"2024-12-04T02:07:58.047456Z","shell.execute_reply":"2024-12-04T02:07:58.076215Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_test_pred = ensemble_model.predict(test)\ndf = pd.read_csv(\"/kaggle/input/child-mind-institute-problematic-internet-use/sample_submission.csv\")\n\nx_sub = df[[\"id\"]].copy()\nx_sub[\"sii\"] = y_test_pred.astype(int)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:08:10.650461Z","iopub.execute_input":"2024-12-04T02:08:10.651400Z","iopub.status.idle":"2024-12-04T02:08:10.845361Z","shell.execute_reply.started":"2024-12-04T02:08:10.651361Z","shell.execute_reply":"2024-12-04T02:08:10.843375Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_sub.to_csv('submission.csv', index=False)\nprint(\"Submission file created: submission.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T02:08:12.862930Z","iopub.execute_input":"2024-12-04T02:08:12.863258Z","iopub.status.idle":"2024-12-04T02:08:12.870511Z","shell.execute_reply.started":"2024-12-04T02:08:12.863231Z","shell.execute_reply":"2024-12-04T02:08:12.869674Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Made by:\n\n~ Angel David Durazo Bartolini\n~ Jehu Jonathan Ramirez Ramirez\n~ Gael Balderrama Dominguez\n\nfor the Pattern Recognition class (UNISON)","metadata":{}}]}