{"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":"# Deep Learning Prediction\n\nI want to predict with deep learning aproach.","metadata":{}},{"cell_type":"markdown","source":"# 0. Load libraries","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelEncoder, StandardScaler\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout\nfrom tensorflow.keras.optimizers import Adam","metadata":{"execution":{"iopub.status.busy":"2024-10-28T06:07:45.551918Z","iopub.execute_input":"2024-10-28T06:07:45.553096Z","iopub.status.idle":"2024-10-28T06:07:45.559351Z","shell.execute_reply.started":"2024-10-28T06:07:45.553042Z","shell.execute_reply":"2024-10-28T06:07:45.558094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 1. Load data.","metadata":{}},{"cell_type":"code","source":"# Cargar datos desde un archivo CSV (cambia 'tu_archivo.csv' por la ruta de tu archivo)\ntrain = pd.read_csv('/kaggle/input/child-mind-institute-problematic-internet-use/train.csv')\ntrain","metadata":{"execution":{"iopub.status.busy":"2024-10-28T06:07:45.563228Z","iopub.execute_input":"2024-10-28T06:07:45.564387Z","iopub.status.idle":"2024-10-28T06:07:45.661088Z","shell.execute_reply.started":"2024-10-28T06:07:45.564301Z","shell.execute_reply":"2024-10-28T06:07:45.659992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/child-mind-institute-problematic-internet-use/test.csv')\ntest","metadata":{"execution":{"iopub.status.busy":"2024-10-28T06:07:45.663510Z","iopub.execute_input":"2024-10-28T06:07:45.663976Z","iopub.status.idle":"2024-10-28T06:07:45.714068Z","shell.execute_reply.started":"2024-10-28T06:07:45.663912Z","shell.execute_reply":"2024-10-28T06:07:45.713090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2. Preprosesing.","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/child-mind-institute-problematic-internet-use/train.csv')\n# Preprocesamiento\n# Eliminar la columna 'id' ya que no aporta valor al modelo\ntrain = train.drop(['id'], axis=1)\n\ncolumns_to_drop = [\n    'PCIAT-Season', 'PCIAT-PCIAT_01', 'PCIAT-PCIAT_02', 'PCIAT-PCIAT_03',\n    'PCIAT-PCIAT_04', 'PCIAT-PCIAT_05', 'PCIAT-PCIAT_06', 'PCIAT-PCIAT_07',\n    'PCIAT-PCIAT_08', 'PCIAT-PCIAT_09', 'PCIAT-PCIAT_10', 'PCIAT-PCIAT_11',\n    'PCIAT-PCIAT_12', 'PCIAT-PCIAT_13', 'PCIAT-PCIAT_14', 'PCIAT-PCIAT_15',\n    'PCIAT-PCIAT_16', 'PCIAT-PCIAT_17', 'PCIAT-PCIAT_18', 'PCIAT-PCIAT_19',\n    'PCIAT-PCIAT_20', 'PCIAT-PCIAT_Total',\n    \n#     'Basic_Demos-Enroll_Season','CGAS-Season','Physical-Season', 'Fitness_Endurance-Season', 'FGC-Season', 'FGC-FGC_CU_Zone',\n#     'FGC-FGC_GSND_Zone', 'FGC-FGC_GSD_Zone', 'FGC-FGC_PU_Zone', 'FGC-FGC_SRL_Zone',\n#     'FGC-FGC_SRR_Zone', 'FGC-FGC_TL_Zone', 'BIA-Season', 'BIA-BIA_Activity_Level_num'\n]\n\ntrain.drop(columns=columns_to_drop, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-10-28T06:07:45.715654Z","iopub.execute_input":"2024-10-28T06:07:45.716758Z","iopub.status.idle":"2024-10-28T06:07:45.776029Z","shell.execute_reply.started":"2024-10-28T06:07:45.716701Z","shell.execute_reply":"2024-10-28T06:07:45.775031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/child-mind-institute-problematic-internet-use/test.csv')\ntest = test.drop(['id'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-10-28T06:07:45.779062Z","iopub.execute_input":"2024-10-28T06:07:45.779535Z","iopub.status.idle":"2024-10-28T06:07:45.791292Z","shell.execute_reply.started":"2024-10-28T06:07:45.779481Z","shell.execute_reply":"2024-10-28T06:07:45.790342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convertir las columnas categóricas a números usando LabelEncoder\nfor column in train.select_dtypes(include=['object']).columns:\n    train[column] = LabelEncoder().fit_transform(train[column].astype(str))\n    \n# Convertir las columnas categóricas a números usando LabelEncoder\nfor column in test.select_dtypes(include=['object']).columns:\n    test[column] = LabelEncoder().fit_transform(test[column].astype(str))","metadata":{"execution":{"iopub.status.busy":"2024-10-28T06:07:45.792659Z","iopub.execute_input":"2024-10-28T06:07:45.793031Z","iopub.status.idle":"2024-10-28T06:07:45.830484Z","shell.execute_reply.started":"2024-10-28T06:07:45.792977Z","shell.execute_reply":"2024-10-28T06:07:45.829183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Lidiar con valores nulos, rellenar con la media (puedes adaptar esto según sea necesario)\ntrain = train.fillna(train.mean())\n\n# Lidiar con valores nulos, rellenar con la media (puedes adaptar esto según sea necesario)\ntest = test.fillna(train.mean())","metadata":{"execution":{"iopub.status.busy":"2024-10-28T06:07:45.831993Z","iopub.execute_input":"2024-10-28T06:07:45.832485Z","iopub.status.idle":"2024-10-28T06:07:45.889034Z","shell.execute_reply.started":"2024-10-28T06:07:45.832432Z","shell.execute_reply":"2024-10-28T06:07:45.888157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3. Deep learning project.","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nimport random\nimport os\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense\n\n# Fijar las semillas para reproducibilidad\nseed = 42\nnp.random.seed(seed)\ntf.random.set_seed(seed)\nrandom.seed(seed)\nos.environ['PYTHONHASHSEED'] = str(seed)\nos.environ['TF_DETERMINISTIC_OPS'] = '1'","metadata":{"execution":{"iopub.status.busy":"2024-10-28T06:07:45.890325Z","iopub.execute_input":"2024-10-28T06:07:45.890690Z","iopub.status.idle":"2024-10-28T06:07:45.905385Z","shell.execute_reply.started":"2024-10-28T06:07:45.890647Z","shell.execute_reply":"2024-10-28T06:07:45.904201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Separar características (X) y la variable objetivo (y)\nX = train.drop('sii', axis=1)\ny = train['sii']","metadata":{"execution":{"iopub.status.busy":"2024-10-28T06:07:45.906687Z","iopub.execute_input":"2024-10-28T06:07:45.907034Z","iopub.status.idle":"2024-10-28T06:07:45.921766Z","shell.execute_reply.started":"2024-10-28T06:07:45.906995Z","shell.execute_reply":"2024-10-28T06:07:45.920834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Estandarizar los datos\nscaler = StandardScaler()\nX_scaled = scaler.fit_transform(X)","metadata":{"execution":{"iopub.status.busy":"2024-10-28T06:07:45.922978Z","iopub.execute_input":"2024-10-28T06:07:45.923342Z","iopub.status.idle":"2024-10-28T06:07:45.943089Z","shell.execute_reply.started":"2024-10-28T06:07:45.923288Z","shell.execute_reply":"2024-10-28T06:07:45.941959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Dividir datos en entrenamiento y prueba\nX_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-10-28T06:07:45.947231Z","iopub.execute_input":"2024-10-28T06:07:45.947628Z","iopub.status.idle":"2024-10-28T06:07:45.955614Z","shell.execute_reply.started":"2024-10-28T06:07:45.947587Z","shell.execute_reply":"2024-10-28T06:07:45.954372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Construir el modelo de red neuronal\nmodel = Sequential([\n    Dense(64, activation='relu', input_shape=(X_train.shape[1],)),  # Asegúrate de usar la dimensión correcta\n    Dense(64, activation='relu'),\n    Dense(4, activation='softmax')  # Capa de salida con 4 neuronas (0, 1, 2, 3)\n])","metadata":{"execution":{"iopub.status.busy":"2024-10-28T06:07:45.956908Z","iopub.execute_input":"2024-10-28T06:07:45.957356Z","iopub.status.idle":"2024-10-28T06:07:46.006222Z","shell.execute_reply.started":"2024-10-28T06:07:45.957303Z","shell.execute_reply":"2024-10-28T06:07:46.005093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compilar el modelo\nmodel.compile(optimizer=Adam(learning_rate=0.001), loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n# model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-10-28T06:07:46.007979Z","iopub.execute_input":"2024-10-28T06:07:46.008483Z","iopub.status.idle":"2024-10-28T06:07:46.024346Z","shell.execute_reply.started":"2024-10-28T06:07:46.008426Z","shell.execute_reply":"2024-10-28T06:07:46.023519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Entrenar el modelo (con 50 epochs, puedes ajustar esto para que tome más tiempo)\nhistory = model.fit(X_train, y_train, epochs=500, batch_size=32, validation_data=(X_test, y_test))","metadata":{"execution":{"iopub.status.busy":"2024-10-28T06:07:46.025524Z","iopub.execute_input":"2024-10-28T06:07:46.025855Z","iopub.status.idle":"2024-10-28T06:11:17.535167Z","shell.execute_reply.started":"2024-10-28T06:07:46.025818Z","shell.execute_reply":"2024-10-28T06:11:17.533970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluar el modelo\nloss, accuracy = model.evaluate(X_test, y_test)\nprint(f\"Precisión en los datos de prueba: {accuracy*100:.2f}%\")","metadata":{"execution":{"iopub.status.busy":"2024-10-28T06:11:17.537133Z","iopub.execute_input":"2024-10-28T06:11:17.537483Z","iopub.status.idle":"2024-10-28T06:11:17.683109Z","shell.execute_reply.started":"2024-10-28T06:11:17.537444Z","shell.execute_reply":"2024-10-28T06:11:17.682057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Realizar predicciones\npredictions = model.predict(test) # por ende debe manipularse con la misma data","metadata":{"execution":{"iopub.status.busy":"2024-10-28T06:11:17.684352Z","iopub.execute_input":"2024-10-28T06:11:17.684692Z","iopub.status.idle":"2024-10-28T06:11:17.804557Z","shell.execute_reply.started":"2024-10-28T06:11:17.684647Z","shell.execute_reply":"2024-10-28T06:11:17.803702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_classes = np.argmax(predictions, axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-10-28T06:11:17.806265Z","iopub.execute_input":"2024-10-28T06:11:17.806620Z","iopub.status.idle":"2024-10-28T06:11:17.811692Z","shell.execute_reply.started":"2024-10-28T06:11:17.806579Z","shell.execute_reply":"2024-10-28T06:11:17.810583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 4. Prediction","metadata":{}},{"cell_type":"code","source":"predictions","metadata":{"execution":{"iopub.status.busy":"2024-10-28T06:11:17.813241Z","iopub.execute_input":"2024-10-28T06:11:17.813771Z","iopub.status.idle":"2024-10-28T06:11:17.826386Z","shell.execute_reply.started":"2024-10-28T06:11:17.813722Z","shell.execute_reply":"2024-10-28T06:11:17.825376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_classes","metadata":{"execution":{"iopub.status.busy":"2024-10-28T06:11:17.827651Z","iopub.execute_input":"2024-10-28T06:11:17.828425Z","iopub.status.idle":"2024-10-28T06:11:17.838490Z","shell.execute_reply.started":"2024-10-28T06:11:17.828387Z","shell.execute_reply":"2024-10-28T06:11:17.837527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/child-mind-institute-problematic-internet-use/test.csv')\n\npredictions = predicted_classes.flatten().astype(int)  # Convierte el array 2D en 1D\n\npredictions_df = pd.DataFrame({\n    'id': test['id'],  \n    'sii': predictions \n})\n\npredictions_df","metadata":{"execution":{"iopub.status.busy":"2024-10-28T06:11:17.839903Z","iopub.execute_input":"2024-10-28T06:11:17.840473Z","iopub.status.idle":"2024-10-28T06:11:17.864224Z","shell.execute_reply.started":"2024-10-28T06:11:17.840421Z","shell.execute_reply":"2024-10-28T06:11:17.863202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 5. Submission","metadata":{}},{"cell_type":"code","source":"predictions_df.to_csv('/kaggle/working/submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-10-28T06:11:17.865610Z","iopub.execute_input":"2024-10-28T06:11:17.866062Z","iopub.status.idle":"2024-10-28T06:11:17.872829Z","shell.execute_reply.started":"2024-10-28T06:11:17.866009Z","shell.execute_reply":"2024-10-28T06:11:17.871715Z"},"trusted":true},"execution_count":null,"outputs":[]}]}