{"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":30804,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"\nimport pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.preprocessing import StandardScaler, OneHotEncoder\nfrom sklearn.compose import ColumnTransformer\nfrom sklearn.decomposition import PCA\nfrom sklearn.model_selection import train_test_split, GridSearchCV\nfrom sklearn.metrics import accuracy_score, classification_report, confusion_matrix\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.svm import SVC\nfrom xgboost import XGBClassifier\nfrom sklearn.pipeline import Pipeline","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-06T04:46:48.272398Z","iopub.execute_input":"2024-12-06T04:46:48.272727Z","iopub.status.idle":"2024-12-06T04:46:51.963336Z","shell.execute_reply.started":"2024-12-06T04:46:48.272697Z","shell.execute_reply":"2024-12-06T04:46:51.962110Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"metadata = pd.read_csv('/kaggle/input/child-mind-institute-problematic-internet-use/data_dictionary.csv')\ndata_train = pd.read_csv('/kaggle/input/child-mind-institute-problematic-internet-use/train.csv')\ndata_test = pd.read_csv('/kaggle/input/child-mind-institute-problematic-internet-use/test.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T04:46:51.965426Z","iopub.execute_input":"2024-12-06T04:46:51.965981Z","iopub.status.idle":"2024-12-06T04:46:52.046636Z","shell.execute_reply.started":"2024-12-06T04:46:51.965936Z","shell.execute_reply":"2024-12-06T04:46:52.045509Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Selección de columnas relevantes","metadata":{}},{"cell_type":"code","source":"columns_to_keep = [\n    'Basic_Demos-Enroll_Season', 'Basic_Demos-Age', 'Basic_Demos-Sex', 'CGAS-Season', \n    'CGAS-CGAS_Score', 'Physical-Season', 'Physical-BMI', 'Physical-Height', \n    'Physical-Weight', 'Physical-Waist_Circumference', 'Physical-Diastolic_BP', \n    'Physical-HeartRate', 'Physical-Systolic_BP', 'Fitness_Endurance-Season', \n    'Fitness_Endurance-Max_Stage', 'Fitness_Endurance-Time_Mins', \n    'Fitness_Endurance-Time_Sec', 'FGC-Season', 'FGC-FGC_CU', 'FGC-FGC_CU_Zone', \n    'FGC-FGC_GSND', 'FGC-FGC_GSND_Zone', 'FGC-FGC_GSD', 'FGC-FGC_GSD_Zone', \n    'FGC-FGC_PU', 'FGC-FGC_PU_Zone', 'FGC-FGC_SRL', 'FGC-FGC_SRL_Zone', \n    'FGC-FGC_SRR', 'FGC-FGC_SRR_Zone', 'FGC-FGC_TL', 'FGC-FGC_TL_Zone', \n    'BIA-Season', 'BIA-BIA_Activity_Level_num', 'BIA-BIA_BMC', 'BIA-BIA_BMI', \n    'BIA-BIA_BMR', 'BIA-BIA_DEE', 'BIA-BIA_ECW', 'BIA-BIA_FFM', 'BIA-BIA_FFMI', \n    'BIA-BIA_FMI', 'BIA-BIA_Fat', 'BIA-BIA_Frame_num', 'BIA-BIA_ICW', \n    'BIA-BIA_LDM', 'BIA-BIA_LST', 'BIA-BIA_SMM', 'BIA-BIA_TBW', 'PAQ_A-Season', \n    'PAQ_A-PAQ_A_Total', 'PAQ_C-Season', 'PAQ_C-PAQ_C_Total', 'SDS-Season', \n    'SDS-SDS_Total_Raw', 'SDS-SDS_Total_T', 'PreInt_EduHx-Season', \n    'PreInt_EduHx-computerinternet_hoursday', 'sii'\n]\nfiltered_train = data_train.loc[:, columns_to_keep]\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T04:46:52.047892Z","iopub.execute_input":"2024-12-06T04:46:52.048278Z","iopub.status.idle":"2024-12-06T04:46:52.072170Z","shell.execute_reply.started":"2024-12-06T04:46:52.048238Z","shell.execute_reply":"2024-12-06T04:46:52.070321Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Limpieza y manejo de valores faltantes","metadata":{}},{"cell_type":"code","source":"filtered_train = filtered_train[filtered_train['sii'].notnull()]\nrow_missing_limit = 0.5\nfiltered_train = filtered_train[filtered_train.isnull().mean(axis=1) <= row_missing_limit]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T04:46:52.074822Z","iopub.execute_input":"2024-12-06T04:46:52.075173Z","iopub.status.idle":"2024-12-06T04:46:52.099842Z","shell.execute_reply.started":"2024-12-06T04:46:52.075141Z","shell.execute_reply":"2024-12-06T04:46:52.098557Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Separación de variables objetivo y características","metadata":{}},{"cell_type":"code","source":"# --- Separación de variables objetivo y características ---\ntarget_variable = 'sii'\ny = filtered_train[target_variable]\nfiltered_train = filtered_train.drop(columns=[target_variable])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T04:46:52.101075Z","iopub.execute_input":"2024-12-06T04:46:52.101380Z","iopub.status.idle":"2024-12-06T04:46:52.108087Z","shell.execute_reply.started":"2024-12-06T04:46:52.101350Z","shell.execute_reply":"2024-12-06T04:46:52.107025Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# --- Identificación de tipos de columnas ---\nnumerical_cols = filtered_train.select_dtypes(include=['float64', 'int64']).columns\ncategorical_cols = filtered_train.select_dtypes(include=['object']).columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T04:46:52.109297Z","iopub.execute_input":"2024-12-06T04:46:52.109628Z","iopub.status.idle":"2024-12-06T04:46:52.121902Z","shell.execute_reply.started":"2024-12-06T04:46:52.109588Z","shell.execute_reply":"2024-12-06T04:46:52.120835Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Pipeline para preprocesamiento","metadata":{}},{"cell_type":"code","source":"# --- Pipeline para preprocesamiento ---\nnumerical_transformer = Pipeline(steps=[\n    ('imputer', SimpleImputer(strategy='mean')),\n    ('scaler', StandardScaler())\n])\n\ncategorical_transformer = Pipeline(steps=[\n    ('imputer', SimpleImputer(strategy='most_frequent')),\n    ('onehot', OneHotEncoder(sparse=False, drop='first'))\n])\n\npreprocessor = ColumnTransformer(transformers=[\n    ('num', numerical_transformer, numerical_cols),\n    ('cat', categorical_transformer, categorical_cols)\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T04:46:52.123146Z","iopub.execute_input":"2024-12-06T04:46:52.123471Z","iopub.status.idle":"2024-12-06T04:46:52.134518Z","shell.execute_reply.started":"2024-12-06T04:46:52.123442Z","shell.execute_reply":"2024-12-06T04:46:52.133465Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Transformación de datos","metadata":{}},{"cell_type":"code","source":"\nX_transformed = preprocessor.fit_transform(filtered_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T04:46:52.135846Z","iopub.execute_input":"2024-12-06T04:46:52.136259Z","iopub.status.idle":"2024-12-06T04:46:52.187539Z","shell.execute_reply.started":"2024-12-06T04:46:52.136216Z","shell.execute_reply":"2024-12-06T04:46:52.186185Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Reducción de dimensionalidad con PCA","metadata":{}},{"cell_type":"code","source":"pca_model = PCA(n_components=0.8)\nX_pca = pca_model.fit_transform(X_transformed)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T04:46:52.189040Z","iopub.execute_input":"2024-12-06T04:46:52.189439Z","iopub.status.idle":"2024-12-06T04:46:52.236498Z","shell.execute_reply.started":"2024-12-06T04:46:52.189405Z","shell.execute_reply":"2024-12-06T04:46:52.234818Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# División de datos","metadata":{}},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X_pca, y, test_size=0.2, stratify=y, random_state=6)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T04:46:52.243538Z","iopub.execute_input":"2024-12-06T04:46:52.244860Z","iopub.status.idle":"2024-12-06T04:46:52.259101Z","shell.execute_reply.started":"2024-12-06T04:46:52.244808Z","shell.execute_reply":"2024-12-06T04:46:52.257866Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Modelos y búsqueda de hiperparámetros","metadata":{}},{"cell_type":"code","source":"models = {\n    'RandomForest': RandomForestClassifier(),\n    'SVM': SVC(probability=True),\n    'XGB': XGBClassifier()\n}\n\nparam_grid = {\n    'RandomForest': {'n_estimators': [100, 200], 'max_depth': [10, 20], 'class_weight': ['balanced']},\n    'SVM': {'C': [0.1, 1], 'kernel': ['linear', 'rbf']},\n    'XGB': {'n_estimators': [100], 'learning_rate': [0.1, 0.2]}\n}\n\nbest_estimators = {}\n\nfor name, model in models.items():\n    grid = GridSearchCV(model, param_grid[name], cv=5, verbose=1, n_jobs=-1)\n    grid.fit(X_train, y_train)\n    best_estimators[name] = grid.best_estimator_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T04:46:52.262711Z","iopub.execute_input":"2024-12-06T04:46:52.266035Z","iopub.status.idle":"2024-12-06T04:47:15.873825Z","shell.execute_reply.started":"2024-12-06T04:46:52.265977Z","shell.execute_reply":"2024-12-06T04:47:15.872959Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Evaluación","metadata":{}},{"cell_type":"code","source":"for name, model in best_estimators.items():\n    y_pred = model.predict(X_val)\n    print(f\"{name} Accuracy:\", accuracy_score(y_val, y_pred))\n    print(classification_report(y_val, y_pred))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T04:47:15.875563Z","iopub.execute_input":"2024-12-06T04:47:15.875996Z","iopub.status.idle":"2024-12-06T04:47:15.960261Z","shell.execute_reply.started":"2024-12-06T04:47:15.875946Z","shell.execute_reply":"2024-12-06T04:47:15.959132Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Matriz de confusión","metadata":{}},{"cell_type":"code","source":"y_pred = best_estimators['SVM'].predict(X_val)\nconf_matrix = confusion_matrix(y_val, y_pred)\n\nplt.figure(figsize=(8, 6))\nsns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues', cbar=False)\nplt.xlabel('Predicted')\nplt.ylabel('Actual')\nplt.title('Confusion Matrix - SVM')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T04:47:15.961601Z","iopub.execute_input":"2024-12-06T04:47:15.962004Z","iopub.status.idle":"2024-12-06T04:47:16.236192Z","shell.execute_reply.started":"2024-12-06T04:47:15.961963Z","shell.execute_reply":"2024-12-06T04:47:16.235098Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import roc_curve, auc, roc_auc_score\nfrom sklearn.preprocessing import label_binarize","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T04:47:16.237393Z","iopub.execute_input":"2024-12-06T04:47:16.237686Z","iopub.status.idle":"2024-12-06T04:47:16.242421Z","shell.execute_reply.started":"2024-12-06T04:47:16.237659Z","shell.execute_reply":"2024-12-06T04:47:16.241278Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# ROC Curves and AUC","metadata":{}},{"cell_type":"code","source":"y_val_bin = label_binarize(y_val, classes=np.unique(y))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T04:47:16.243689Z","iopub.execute_input":"2024-12-06T04:47:16.244258Z","iopub.status.idle":"2024-12-06T04:47:16.258625Z","shell.execute_reply.started":"2024-12-06T04:47:16.244222Z","shell.execute_reply":"2024-12-06T04:47:16.257482Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Obtenemos las probabilidades de predicción del modelo SVM","metadata":{}},{"cell_type":"code","source":"y_pred_proba = best_estimators['SVM'].predict_proba(X_val)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T04:47:16.260346Z","iopub.execute_input":"2024-12-06T04:47:16.260870Z","iopub.status.idle":"2024-12-06T04:47:16.303198Z","shell.execute_reply.started":"2024-12-06T04:47:16.260822Z","shell.execute_reply":"2024-12-06T04:47:16.302019Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Gráfico de curvas ROC","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10, 8))\n\nfor i in range(y_val_bin.shape[1]):  # Para cada clase\n    fpr, tpr, _ = roc_curve(y_val_bin[:, i], y_pred_proba[:, i])\n    roc_auc = auc(fpr, tpr)\n    plt.plot(fpr, tpr, lw=2, label=f'Class {i} (AUC = {roc_auc:.2f})')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T04:47:16.304371Z","iopub.execute_input":"2024-12-06T04:47:16.304667Z","iopub.status.idle":"2024-12-06T04:47:16.548351Z","shell.execute_reply.started":"2024-12-06T04:47:16.304639Z","shell.execute_reply":"2024-12-06T04:47:16.547273Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Línea diagonal de referencia","metadata":{}},{"cell_type":"code","source":"plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')\n\nplt.xlabel('False Positive Rate', fontsize=14)\nplt.ylabel('True Positive Rate', fontsize=14)\nplt.title('ROC Curve - SVM Model', fontsize=16)\nplt.legend(loc='lower right', fontsize=12)\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T04:47:16.549727Z","iopub.execute_input":"2024-12-06T04:47:16.550176Z","iopub.status.idle":"2024-12-06T04:47:16.836324Z","shell.execute_reply.started":"2024-12-06T04:47:16.550132Z","shell.execute_reply":"2024-12-06T04:47:16.835195Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Calculamos el AUC para cada clase","metadata":{}},{"cell_type":"code","source":"auc_scores = [roc_auc_score(y_val_bin[:, i], y_pred_proba[:, i]) for i in range(y_val_bin.shape[1])]\nprint(\"AUC Scores for each class:\")\nfor i, auc_score in enumerate(auc_scores):\n    print(f\"Class {i}: {auc_score:.2f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T04:47:16.837685Z","iopub.execute_input":"2024-12-06T04:47:16.838054Z","iopub.status.idle":"2024-12-06T04:47:16.851437Z","shell.execute_reply.started":"2024-12-06T04:47:16.838022Z","shell.execute_reply":"2024-12-06T04:47:16.850264Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Predictions for test dataset","metadata":{}},{"cell_type":"code","source":"if 'id' not in data_test.columns:\n    raise ValueError(\"El conjunto de datos de prueba no contiene la columna objetivo 'id'.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T04:47:16.852690Z","iopub.execute_input":"2024-12-06T04:47:16.853044Z","iopub.status.idle":"2024-12-06T04:47:16.858231Z","shell.execute_reply.started":"2024-12-06T04:47:16.853012Z","shell.execute_reply":"2024-12-06T04:47:16.857260Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Preprocesamos los datos de prueba","metadata":{}},{"cell_type":"code","source":"data_test_numerical = data_test[numerical_cols]\ndata_test_categorical = data_test[categorical_cols]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T04:47:16.859361Z","iopub.execute_input":"2024-12-06T04:47:16.859719Z","iopub.status.idle":"2024-12-06T04:47:16.870947Z","shell.execute_reply.started":"2024-12-06T04:47:16.859689Z","shell.execute_reply":"2024-12-06T04:47:16.869823Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Imputamos valores faltantes y transformamos\ndata_test_processed = preprocessor.transform(data_test)\n\n# Reducimos dimensionalidad usando el PCA ajustado\ndata_test_pca = pca_model.transform(data_test_processed)\n\n# Realizamos las predicciones con el mejor modelo (SVM en este caso)\ntest_predictions = best_estimators['SVM'].predict(data_test_pca)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T04:47:16.872366Z","iopub.execute_input":"2024-12-06T04:47:16.872825Z","iopub.status.idle":"2024-12-06T04:47:16.892914Z","shell.execute_reply.started":"2024-12-06T04:47:16.872775Z","shell.execute_reply":"2024-12-06T04:47:16.891797Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.DataFrame({\n    'id': data_test['id'],  # Identificador único de cada fila\n    'sii': test_predictions  # Predicciones del modelo\n})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T04:47:16.894272Z","iopub.execute_input":"2024-12-06T04:47:16.894704Z","iopub.status.idle":"2024-12-06T04:47:16.902196Z","shell.execute_reply.started":"2024-12-06T04:47:16.894662Z","shell.execute_reply":"2024-12-06T04:47:16.901219Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"output_path = 'submission.csv'\nsubmission.to_csv(output_path, index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T04:47:16.903493Z","iopub.execute_input":"2024-12-06T04:47:16.904398Z","iopub.status.idle":"2024-12-06T04:47:16.917893Z","shell.execute_reply.started":"2024-12-06T04:47:16.904350Z","shell.execute_reply":"2024-12-06T04:47:16.916897Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}