{"cells":[{"cell_type":"code","source":"# librerias\nfrom numpy import mean\nfrom numpy import std\nfrom pandas import read_csv\nfrom sklearn.model_selection import LeaveOneOut\nfrom sklearn.model_selection import StratifiedKFold\nfrom sklearn.model_selection import cross_val_score\nfrom sklearn.ensemble import RandomForestClassifier","metadata":{"id":"4wZI6qHSWbo9","cell_id":"94c1b3785dc54b428bce57ffff71e057","executionInfo":{"user":{"userId":"09471607480253994520","displayName":"David Francisco Bustos Usta"},"status":"ok","elapsed":2218,"user_tz":240,"timestamp":1652614617495},"deepnote_cell_type":"code"},"outputs":[],"execution_count":2},{"cell_type":"markdown","source":"**Descripcion de datos**\nhttps://raw.githubusercontent.com/jbrownlee/Datasets/master/sonar.names\n\n**Enlace con datos**\nhttps://raw.githubusercontent.com/jbrownlee/Datasets/master/sonar.csv\n\nEl archivo \"sonar.mines\" contiene 111 patrones obtenidos al hacer rebotar señales de sonar en un cilindro de metal en varios ángulos y bajo diversas condiciones. El archivo \"sonar.rocks\" contiene 97 patrones obtenidos de rocas en condiciones similares. La señal del sonar transmitida es un chirrido de frecuencia modulada, aumentando en frecuencia. El conjunto de datos contiene señales obtenidas desde una variedad de ángulos de aspecto diferentes, que abarcan 90 grados para el cilindro y 180 grados para la roca.\n\nCada patrón es un conjunto de 60 números en el rango de 0,0 a 1,0. Cada número representa la energía dentro de una banda de frecuencia particular, integrada durante un cierto período de tiempo. La apertura de integración para frecuencias más altas ocurre más tarde, ya que estas frecuencias se transmiten más tarde durante el chirrido.\n\nLa etiqueta asociada a cada registro contiene la letra \"R\" si el objeto es una roca y \"M\" si es una mina (cilindro de metal). Los números en las etiquetas están en orden creciente de ángulo de aspecto, pero no codifican el ángulo directamente.","metadata":{"id":"c-uqzdzEWsyS","cell_id":"9e450f6e356341febbfd6ad47ef161d5","deepnote_cell_type":"markdown"}},{"cell_type":"code","source":"# datos\nurl = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/sonar.csv'\ndataframe = read_csv(url, header=None)\ndata = dataframe.values\ndataframe.head()","metadata":{"id":"HJveRtrNWfyH","colab":{"height":235,"base_uri":"https://localhost:8080/"},"cell_id":"7553e690416747d7ad17c0771af5a393","outputId":"fe18027d-9ca3-446b-e30d-925e98575666","executionInfo":{"user":{"userId":"09471607480253994520","displayName":"David Francisco Bustos Usta"},"status":"ok","elapsed":382,"user_tz":240,"timestamp":1652614618542},"deepnote_cell_type":"code"},"outputs":[{"output_type":"execute_result","data":{"text/plain":" 0 1 2 3 4 5 6 7 8 \\\n0 0.0200 0.0371 0.0428 0.0207 0.0954 0.0986 0.1539 0.1601 0.3109 \n1 0.0453 0.0523 0.0843 0.0689 0.1183 0.2583 0.2156 0.3481 0.3337 \n2 0.0262 0.0582 0.1099 0.1083 0.0974 0.2280 0.2431 0.3771 0.5598 \n3 0.0100 0.0171 0.0623 0.0205 0.0205 0.0368 0.1098 0.1276 0.0598 \n4 0.0762 0.0666 0.0481 0.0394 0.0590 0.0649 0.1209 0.2467 0.3564 \n\n 9 ... 51 52 53 54 55 56 57 \\\n0 0.2111 ... 0.0027 0.0065 0.0159 0.0072 0.0167 0.0180 0.0084 \n1 0.2872 ... 0.0084 0.0089 0.0048 0.0094 0.0191 0.0140 0.0049 \n2 0.6194 ... 0.0232 0.0166 0.0095 0.0180 0.0244 0.0316 0.0164 \n3 0.1264 ... 0.0121 0.0036 0.0150 0.0085 0.0073 0.0050 0.0044 \n4 0.4459 ... 0.0031 0.0054 0.0105 0.0110 0.0015 0.0072 0.0048 \n\n 58 59 60 \n0 0.0090 0.0032 R \n1 0.0052 0.0044 R \n2 0.0095 0.0078 R \n3 0.0040 0.0117 R \n4 0.0107 0.0094 R \n\n[5 rows x 61 columns]","text/html":"\n
\n
\n
\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
0123456789...51525354555657585960
00.02000.03710.04280.02070.09540.09860.15390.16010.31090.2111...0.00270.00650.01590.00720.01670.01800.00840.00900.0032R
10.04530.05230.08430.06890.11830.25830.21560.34810.33370.2872...0.00840.00890.00480.00940.01910.01400.00490.00520.0044R
20.02620.05820.10990.10830.09740.22800.24310.37710.55980.6194...0.02320.01660.00950.01800.02440.03160.01640.00950.0078R
30.01000.01710.06230.02050.02050.03680.10980.12760.05980.1264...0.01210.00360.01500.00850.00730.00500.00440.00400.0117R
40.07620.06660.04810.03940.05900.06490.12090.24670.35640.4459...0.00310.00540.01050.01100.00150.00720.00480.01070.0094R
\n

5 rows × 61 columns

\n
\n \n \n \n\n \n
\n
\n "},"metadata":{},"execution_count":3}],"execution_count":3},{"cell_type":"code","source":"dataframe.shape","metadata":{"id":"_3qcVkZ4XLdT","colab":{"base_uri":"https://localhost:8080/"},"cell_id":"f598973276ac480185a10e2d523df2ef","outputId":"e1f5079a-dd09-4701-968d-3dc54a8aa804","executionInfo":{"user":{"userId":"09471607480253994520","displayName":"David Francisco Bustos Usta"},"status":"ok","elapsed":212,"user_tz":240,"timestamp":1652614620827},"deepnote_cell_type":"code"},"outputs":[{"output_type":"execute_result","data":{"text/plain":"(208, 61)"},"metadata":{},"execution_count":4}],"execution_count":4},{"cell_type":"code","source":"dataframe.isnull().sum()","metadata":{"id":"Aoc0O4lXXUui","colab":{"base_uri":"https://localhost:8080/"},"cell_id":"7400746a894648649d5360d384ae0ebe","outputId":"8ce72725-7c9f-4bbc-c2d0-d87ec98131cf","executionInfo":{"user":{"userId":"09471607480253994520","displayName":"David Francisco Bustos Usta"},"status":"ok","elapsed":6,"user_tz":240,"timestamp":1652614621553},"deepnote_cell_type":"code"},"outputs":[{"output_type":"execute_result","data":{"text/plain":"0 0\n1 0\n2 0\n3 0\n4 0\n ..\n56 0\n57 0\n58 0\n59 0\n60 0\nLength: 61, dtype: int64"},"metadata":{},"execution_count":5}],"execution_count":5},{"cell_type":"code","source":"sum(dataframe.isna().sum())\n# no hay nulos","metadata":{"id":"X3s35hdfXZTe","colab":{"base_uri":"https://localhost:8080/"},"cell_id":"9f53ff026bc64b599378c05209da1c06","outputId":"29579d0b-5f9b-4c9b-f15a-319b4645e2a5","executionInfo":{"user":{"userId":"09471607480253994520","displayName":"David Francisco Bustos Usta"},"status":"ok","elapsed":327,"user_tz":240,"timestamp":1652614622889},"deepnote_cell_type":"code"},"outputs":[{"output_type":"execute_result","data":{"text/plain":"0"},"metadata":{},"execution_count":6}],"execution_count":6},{"cell_type":"code","source":"# Separar en X y y\nX, y = data[:, :-1], data[:, -1]\nprint(X.shape, y.shape)","metadata":{"id":"rqoMoCdqWnU2","colab":{"base_uri":"https://localhost:8080/"},"cell_id":"e03f6757bdd542f381d28b7d8c0d51c4","outputId":"c4effe08-a3e5-4589-aee0-6e907fa47baa","executionInfo":{"user":{"userId":"09471607480253994520","displayName":"David Francisco Bustos Usta"},"status":"ok","elapsed":4,"user_tz":240,"timestamp":1652614623967},"deepnote_cell_type":"code"},"outputs":[{"output_type":"stream","name":"stdout","text":"(208, 60) (208,)\n"}],"execution_count":7},{"cell_type":"code","source":"# crear el procedimiento loocv \nskf = StratifiedKFold(n_splits=5, random_state=42, shuffle=True)\nskf.get_n_splits(X, y)","metadata":{"id":"mWhTZ2BxXjdZ","colab":{"base_uri":"https://localhost:8080/"},"cell_id":"4fac9eff3dfa410fa576a7711c957567","outputId":"1e4b487f-b107-4a02-895e-2caa5ec2683c","executionInfo":{"user":{"userId":"09471607480253994520","displayName":"David Francisco Bustos Usta"},"status":"ok","elapsed":322,"user_tz":240,"timestamp":1652614687595},"deepnote_cell_type":"code"},"outputs":[{"output_type":"execute_result","data":{"text/plain":"5"},"metadata":{},"execution_count":9}],"execution_count":9},{"cell_type":"code","source":"train, test = list(skf.split(X, y))[0]\ntrain","metadata":{"id":"dWUvDDJnhecQ","colab":{"base_uri":"https://localhost:8080/"},"cell_id":"fc5b17dc888b45d59f00bd9e083e9fa1","outputId":"2e1dcb5f-56cb-4b71-991b-90c66a925a14","executionInfo":{"user":{"userId":"09471607480253994520","displayName":"David Francisco Bustos Usta"},"status":"ok","elapsed":251,"user_tz":240,"timestamp":1652615064136},"deepnote_cell_type":"code"},"outputs":[{"output_type":"execute_result","data":{"text/plain":"array([ 0, 1, 2, 4, 5, 6, 7, 10, 11, 12, 13, 14, 16,\n 18, 19, 20, 21, 22, 24, 25, 27, 29, 30, 31, 32, 33,\n 35, 36, 37, 39, 40, 42, 43, 45, 46, 47, 49, 52, 54,\n 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68,\n 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,\n 83, 84, 86, 87, 88, 89, 90, 91, 92, 93, 95, 96, 99,\n 100, 101, 102, 103, 105, 106, 107, 109, 110, 111, 112, 114, 115,\n 116, 117, 118, 119, 120, 121, 123, 124, 125, 126, 127, 128, 129,\n 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142,\n 143, 144, 146, 148, 149, 150, 151, 153, 154, 155, 156, 158, 160,\n 161, 162, 164, 165, 166, 167, 168, 170, 171, 172, 173, 175, 177,\n 179, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193,\n 194, 196, 197, 199, 200, 201, 203, 204, 205, 207])"},"metadata":{},"execution_count":15}],"execution_count":15},{"cell_type":"code","source":"test","metadata":{"id":"yCA58SzAiLKN","colab":{"base_uri":"https://localhost:8080/"},"cell_id":"e13512dd249043c2838471617260b45a","outputId":"1f4f43d6-cb70-40de-c95f-9dec49574cf4","executionInfo":{"user":{"userId":"09471607480253994520","displayName":"David Francisco Bustos Usta"},"status":"ok","elapsed":251,"user_tz":240,"timestamp":1652615069061},"deepnote_cell_type":"code"},"outputs":[{"output_type":"execute_result","data":{"text/plain":"array([ 3, 8, 9, 15, 17, 23, 26, 28, 34, 38, 41, 44, 48,\n 50, 51, 53, 55, 82, 85, 94, 97, 98, 104, 108, 113, 122,\n 145, 147, 152, 157, 159, 163, 169, 174, 176, 178, 180, 181, 195,\n 198, 202, 206])"},"metadata":{},"execution_count":16}],"execution_count":16},{"cell_type":"code","source":"X[test]","metadata":{"id":"Iv0FnvzUjYdm","colab":{"base_uri":"https://localhost:8080/"},"cell_id":"1d164d7df1294aadb16aaf5de00b38c1","outputId":"27aed50b-5c8c-42d3-9e94-607bc06b9d44","executionInfo":{"user":{"userId":"09471607480253994520","displayName":"David Francisco Bustos Usta"},"status":"ok","elapsed":6,"user_tz":240,"timestamp":1652615391799},"deepnote_cell_type":"code"},"outputs":[{"output_type":"execute_result","data":{"text/plain":"(42, 60)"},"metadata":{},"execution_count":34}],"execution_count":34},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\n# Modelo vacio\nmodel = RandomForestClassifier(random_state=1,n_estimators=30, criterion=\"gini\",max_depth=4)\n# Lista para guardar accuracy\nlist_ac=[]\nfor i in range(0,skf.get_n_splits(X, y)):\n # Calculo de indices\n train, test = list(skf.split(X, y))[i]\n # Separar train y test\n X_train_n, y_train_n, X_test_n, y_test_n= X[train], y[train], X[test],y[test]\n # Ajustar el modelo\n model.fit(X_train_n, y_train_n)\n # Predicciones\n pred= model.predict(X_test_n)\n # Calculo del error\n accuracy= accuracy_score(y_true=y_test_n, y_pred= pred)\n print('Iteracion: ',i+1, ' Accuracy:', accuracy)\n list_ac.append(accuracy)\n# Promedio de accuracy\nimport numpy as np\nprint('Accuracy promedio:', np.mean(list_ac))","metadata":{"id":"207rYldLiM9K","colab":{"base_uri":"https://localhost:8080/"},"cell_id":"4c62f3a3315c4cf7b333327197fff9a6","outputId":"718d3100-536b-4a6f-80a4-d4595fcc0760","executionInfo":{"user":{"userId":"09471607480253994520","displayName":"David Francisco Bustos Usta"},"status":"ok","elapsed":992,"user_tz":240,"timestamp":1652615682308},"deepnote_cell_type":"code"},"outputs":[{"output_type":"stream","name":"stdout","text":"Iteracion: 1 Accuracy: 0.7619047619047619\nIteracion: 2 Accuracy: 0.8095238095238095\nIteracion: 3 Accuracy: 0.8095238095238095\nIteracion: 4 Accuracy: 0.8292682926829268\nIteracion: 5 Accuracy: 0.8048780487804879\nAccuracy promedio: 0.8030197444831592\n"}],"execution_count":38},{"cell_type":"markdown","source":"\nCreated in deepnote.com \nCreated in Deepnote","metadata":{"created_in_deepnote_cell":true,"deepnote_cell_type":"markdown"}}],"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"Ejemplo 2- Stratified- K fold (clasificacion).ipynb","provenance":[],"authorship_tag":"ABX9TyMnP5vLNvpWNbN0FAwkS4xB","collapsed_sections":[]},"deepnote":{},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"},"deepnote_notebook_id":"fa28bd6283684b25a63d2e81a8978c3e","deepnote_execution_queue":[]}}