{"cells":[{"cell_type":"markdown","source":"Como no podía ser de otra manera, vamos a ver una implementación del algoritmo de Adaboost en nuestro famoso dataset de Iris :)","metadata":{"id":"LVDI1hvcEWE8","cell_id":"bfc708020ee849ada14ea79792f00ad7","deepnote_cell_type":"markdown"}},{"cell_type":"code","source":"from sklearn.ensemble import AdaBoostClassifier\nfrom sklearn import datasets\nfrom sklearn.model_selection import train_test_split\nfrom sklearn import metrics","metadata":{"id":"d_SUvBpGEWFJ","cell_id":"5ee21d994c5746898d3fd74f3183a49e","executionInfo":{"user":{"userId":"09471607480253994520","displayName":"David Francisco Bustos Usta"},"status":"ok","elapsed":1311,"user_tz":240,"timestamp":1652653924649},"deepnote_cell_type":"code"},"outputs":[],"execution_count":1},{"cell_type":"code","source":"from sklearn.ensemble import AdaBoostRegressor","metadata":{"id":"t4jondJCIiG6","cell_id":"1891986b210543509130bc32e173d6dc","executionInfo":{"user":{"userId":"09471607480253994520","displayName":"David Francisco Bustos Usta"},"status":"ok","elapsed":16,"user_tz":240,"timestamp":1652653924652},"deepnote_cell_type":"code"},"outputs":[],"execution_count":2},{"cell_type":"code","source":"#Cargamos la data!\niris = datasets.load_iris()\nX = iris.data\ny = iris.target","metadata":{"id":"P1MOgvRvEWFN","cell_id":"218d4cd090fd4b979286c5d418736dfc","executionInfo":{"user":{"userId":"09471607480253994520","displayName":"David Francisco Bustos Usta"},"status":"ok","elapsed":16,"user_tz":240,"timestamp":1652653924653},"deepnote_cell_type":"code"},"outputs":[],"execution_count":3},{"cell_type":"code","source":"X","metadata":{"id":"SEB9EI_lEba3","colab":{"base_uri":"https://localhost:8080/"},"cell_id":"0dbe895b2deb4fe28476911d06080a63","outputId":"61b16ed4-8301-4a50-cf3b-dd086fdcf91b","executionInfo":{"user":{"userId":"09471607480253994520","displayName":"David Francisco Bustos Usta"},"status":"ok","elapsed":17,"user_tz":240,"timestamp":1652653924655},"deepnote_cell_type":"code"},"outputs":[{"output_type":"execute_result","data":{"text/plain":"array([[5.1, 3.5, 1.4, 0.2],\n [4.9, 3. , 1.4, 0.2],\n [4.7, 3.2, 1.3, 0.2],\n [4.6, 3.1, 1.5, 0.2],\n [5. , 3.6, 1.4, 0.2],\n [5.4, 3.9, 1.7, 0.4],\n [4.6, 3.4, 1.4, 0.3],\n [5. , 3.4, 1.5, 0.2],\n [4.4, 2.9, 1.4, 0.2],\n [4.9, 3.1, 1.5, 0.1],\n [5.4, 3.7, 1.5, 0.2],\n [4.8, 3.4, 1.6, 0.2],\n [4.8, 3. , 1.4, 0.1],\n [4.3, 3. , 1.1, 0.1],\n [5.8, 4. , 1.2, 0.2],\n [5.7, 4.4, 1.5, 0.4],\n [5.4, 3.9, 1.3, 0.4],\n [5.1, 3.5, 1.4, 0.3],\n [5.7, 3.8, 1.7, 0.3],\n [5.1, 3.8, 1.5, 0.3],\n [5.4, 3.4, 1.7, 0.2],\n [5.1, 3.7, 1.5, 0.4],\n [4.6, 3.6, 1. , 0.2],\n [5.1, 3.3, 1.7, 0.5],\n [4.8, 3.4, 1.9, 0.2],\n [5. , 3. , 1.6, 0.2],\n [5. , 3.4, 1.6, 0.4],\n [5.2, 3.5, 1.5, 0.2],\n [5.2, 3.4, 1.4, 0.2],\n [4.7, 3.2, 1.6, 0.2],\n [4.8, 3.1, 1.6, 0.2],\n [5.4, 3.4, 1.5, 0.4],\n [5.2, 4.1, 1.5, 0.1],\n [5.5, 4.2, 1.4, 0.2],\n [4.9, 3.1, 1.5, 0.2],\n [5. , 3.2, 1.2, 0.2],\n [5.5, 3.5, 1.3, 0.2],\n [4.9, 3.6, 1.4, 0.1],\n [4.4, 3. , 1.3, 0.2],\n [5.1, 3.4, 1.5, 0.2],\n [5. , 3.5, 1.3, 0.3],\n [4.5, 2.3, 1.3, 0.3],\n [4.4, 3.2, 1.3, 0.2],\n [5. , 3.5, 1.6, 0.6],\n [5.1, 3.8, 1.9, 0.4],\n [4.8, 3. , 1.4, 0.3],\n [5.1, 3.8, 1.6, 0.2],\n [4.6, 3.2, 1.4, 0.2],\n [5.3, 3.7, 1.5, 0.2],\n [5. , 3.3, 1.4, 0.2],\n [7. , 3.2, 4.7, 1.4],\n [6.4, 3.2, 4.5, 1.5],\n [6.9, 3.1, 4.9, 1.5],\n [5.5, 2.3, 4. , 1.3],\n [6.5, 2.8, 4.6, 1.5],\n [5.7, 2.8, 4.5, 1.3],\n [6.3, 3.3, 4.7, 1.6],\n [4.9, 2.4, 3.3, 1. ],\n [6.6, 2.9, 4.6, 1.3],\n [5.2, 2.7, 3.9, 1.4],\n [5. , 2. , 3.5, 1. ],\n [5.9, 3. , 4.2, 1.5],\n [6. , 2.2, 4. , 1. ],\n [6.1, 2.9, 4.7, 1.4],\n [5.6, 2.9, 3.6, 1.3],\n [6.7, 3.1, 4.4, 1.4],\n [5.6, 3. , 4.5, 1.5],\n [5.8, 2.7, 4.1, 1. ],\n [6.2, 2.2, 4.5, 1.5],\n [5.6, 2.5, 3.9, 1.1],\n [5.9, 3.2, 4.8, 1.8],\n [6.1, 2.8, 4. , 1.3],\n [6.3, 2.5, 4.9, 1.5],\n [6.1, 2.8, 4.7, 1.2],\n [6.4, 2.9, 4.3, 1.3],\n [6.6, 3. , 4.4, 1.4],\n [6.8, 2.8, 4.8, 1.4],\n [6.7, 3. , 5. , 1.7],\n [6. , 2.9, 4.5, 1.5],\n [5.7, 2.6, 3.5, 1. ],\n [5.5, 2.4, 3.8, 1.1],\n [5.5, 2.4, 3.7, 1. ],\n [5.8, 2.7, 3.9, 1.2],\n [6. , 2.7, 5.1, 1.6],\n [5.4, 3. , 4.5, 1.5],\n [6. , 3.4, 4.5, 1.6],\n [6.7, 3.1, 4.7, 1.5],\n [6.3, 2.3, 4.4, 1.3],\n [5.6, 3. , 4.1, 1.3],\n [5.5, 2.5, 4. , 1.3],\n [5.5, 2.6, 4.4, 1.2],\n [6.1, 3. , 4.6, 1.4],\n [5.8, 2.6, 4. , 1.2],\n [5. , 2.3, 3.3, 1. ],\n [5.6, 2.7, 4.2, 1.3],\n [5.7, 3. , 4.2, 1.2],\n [5.7, 2.9, 4.2, 1.3],\n [6.2, 2.9, 4.3, 1.3],\n [5.1, 2.5, 3. , 1.1],\n [5.7, 2.8, 4.1, 1.3],\n [6.3, 3.3, 6. , 2.5],\n [5.8, 2.7, 5.1, 1.9],\n [7.1, 3. , 5.9, 2.1],\n [6.3, 2.9, 5.6, 1.8],\n [6.5, 3. , 5.8, 2.2],\n [7.6, 3. , 6.6, 2.1],\n [4.9, 2.5, 4.5, 1.7],\n [7.3, 2.9, 6.3, 1.8],\n [6.7, 2.5, 5.8, 1.8],\n [7.2, 3.6, 6.1, 2.5],\n [6.5, 3.2, 5.1, 2. ],\n [6.4, 2.7, 5.3, 1.9],\n [6.8, 3. , 5.5, 2.1],\n [5.7, 2.5, 5. , 2. ],\n [5.8, 2.8, 5.1, 2.4],\n [6.4, 3.2, 5.3, 2.3],\n [6.5, 3. , 5.5, 1.8],\n [7.7, 3.8, 6.7, 2.2],\n [7.7, 2.6, 6.9, 2.3],\n [6. , 2.2, 5. , 1.5],\n [6.9, 3.2, 5.7, 2.3],\n [5.6, 2.8, 4.9, 2. ],\n [7.7, 2.8, 6.7, 2. ],\n [6.3, 2.7, 4.9, 1.8],\n [6.7, 3.3, 5.7, 2.1],\n [7.2, 3.2, 6. , 1.8],\n [6.2, 2.8, 4.8, 1.8],\n [6.1, 3. , 4.9, 1.8],\n [6.4, 2.8, 5.6, 2.1],\n [7.2, 3. , 5.8, 1.6],\n [7.4, 2.8, 6.1, 1.9],\n [7.9, 3.8, 6.4, 2. ],\n [6.4, 2.8, 5.6, 2.2],\n [6.3, 2.8, 5.1, 1.5],\n [6.1, 2.6, 5.6, 1.4],\n [7.7, 3. , 6.1, 2.3],\n [6.3, 3.4, 5.6, 2.4],\n [6.4, 3.1, 5.5, 1.8],\n [6. , 3. , 4.8, 1.8],\n [6.9, 3.1, 5.4, 2.1],\n [6.7, 3.1, 5.6, 2.4],\n [6.9, 3.1, 5.1, 2.3],\n [5.8, 2.7, 5.1, 1.9],\n [6.8, 3.2, 5.9, 2.3],\n [6.7, 3.3, 5.7, 2.5],\n [6.7, 3. , 5.2, 2.3],\n [6.3, 2.5, 5. , 1.9],\n [6.5, 3. , 5.2, 2. ],\n [6.2, 3.4, 5.4, 2.3],\n [5.9, 3. , 5.1, 1.8]])"},"metadata":{},"execution_count":4}],"execution_count":4},{"cell_type":"code","source":"y","metadata":{"id":"dGkO2ThlEcTa","colab":{"base_uri":"https://localhost:8080/"},"cell_id":"39afea741c5f474a86abbb0241f2825e","outputId":"b724e1b3-2d2a-4418-9a19-52f6cafd5e8b","executionInfo":{"user":{"userId":"09471607480253994520","displayName":"David Francisco Bustos Usta"},"status":"ok","elapsed":287,"user_tz":240,"timestamp":1652653929722},"deepnote_cell_type":"code"},"outputs":[{"output_type":"execute_result","data":{"text/plain":"array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,\n 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,\n 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])"},"metadata":{},"execution_count":5}],"execution_count":5},{"cell_type":"code","source":"#Separación en Train y Test\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3) # 70% training and 30% test","metadata":{"id":"FeFpeDTtEWFP","cell_id":"52089237237f472b9cd40d55402e9663","executionInfo":{"user":{"userId":"09471607480253994520","displayName":"David Francisco Bustos Usta"},"status":"ok","elapsed":6,"user_tz":240,"timestamp":1652653930916},"deepnote_cell_type":"code"},"outputs":[],"execution_count":6},{"cell_type":"code","source":"# Creamos un AdaBoostClassifier\nada = AdaBoostClassifier(n_estimators=50,learning_rate=0.1)","metadata":{"id":"fJDauD8aEWFR","cell_id":"8ebef14a9fd1420084f70a740183f49d","executionInfo":{"user":{"userId":"09471607480253994520","displayName":"David Francisco Bustos Usta"},"status":"ok","elapsed":3,"user_tz":240,"timestamp":1652653931274},"deepnote_cell_type":"code"},"outputs":[],"execution_count":7},{"cell_type":"code","source":"#Fiteamos el modelo\nmodel = ada.fit(X_train, y_train)","metadata":{"id":"5VQYJc8REWFS","cell_id":"b648f9437a34486f88aa12f934ffc4b4","executionInfo":{"user":{"userId":"09471607480253994520","displayName":"David Francisco Bustos Usta"},"status":"ok","elapsed":4,"user_tz":240,"timestamp":1652653931893},"deepnote_cell_type":"code"},"outputs":[],"execution_count":8},{"cell_type":"code","source":"#Predicción!\ny_pred = model.predict(X_test)","metadata":{"id":"UIO1tDtBEWFV","cell_id":"839344ce847542e6b8b7f7d61568f9d7","executionInfo":{"user":{"userId":"09471607480253994520","displayName":"David Francisco Bustos Usta"},"status":"ok","elapsed":4,"user_tz":240,"timestamp":1652653932562},"deepnote_cell_type":"code"},"outputs":[],"execution_count":9},{"cell_type":"code","source":"y_pred","metadata":{"id":"3Vlmf1tPTkmL","colab":{"base_uri":"https://localhost:8080/"},"cell_id":"8d0488cc95dd4c178c097baedc1a1cfc","outputId":"329b0c9f-6603-4103-faef-416012dd5c02","executionInfo":{"user":{"userId":"09471607480253994520","displayName":"David Francisco Bustos Usta"},"status":"ok","elapsed":6,"user_tz":240,"timestamp":1652653932847},"deepnote_cell_type":"code"},"outputs":[{"output_type":"execute_result","data":{"text/plain":"array([2, 0, 0, 1, 2, 0, 0, 2, 1, 1, 0, 1, 2, 2, 0, 0, 1, 2, 1, 0, 0, 0,\n 0, 1, 1, 1, 1, 1, 2, 1, 1, 0, 0, 1, 2, 0, 2, 1, 1, 0, 2, 2, 2, 2,\n 2])"},"metadata":{},"execution_count":10}],"execution_count":10},{"cell_type":"code","source":"#Accuracy!\nprint(\"Accuracy:\",metrics.accuracy_score(y_test, y_pred))","metadata":{"id":"ssTrv_wnEWFX","colab":{"base_uri":"https://localhost:8080/"},"cell_id":"72fb546d612b4f1e8428a7d36caef345","outputId":"9e8723d8-3bfe-48d5-a594-17b16cfe8777","executionInfo":{"user":{"userId":"09471607480253994520","displayName":"David Francisco Bustos Usta"},"status":"ok","elapsed":5,"user_tz":240,"timestamp":1652653934249},"deepnote_cell_type":"code"},"outputs":[{"output_type":"stream","name":"stdout","text":"Accuracy: 0.9555555555555556\n"}],"execution_count":11},{"cell_type":"code","source":"from sklearn.metrics import classification_report\nprint(classification_report(y_test,y_pred))","metadata":{"id":"l2nom3vPIs5T","colab":{"base_uri":"https://localhost:8080/"},"cell_id":"6f64ffa8b9c74b12b010c550c883be24","outputId":"51930863-4f29-4973-c424-7a04bd060885","executionInfo":{"user":{"userId":"09471607480253994520","displayName":"David Francisco Bustos Usta"},"status":"ok","elapsed":5,"user_tz":240,"timestamp":1652653934820},"deepnote_cell_type":"code"},"outputs":[{"output_type":"stream","name":"stdout","text":" precision recall f1-score support\n\n 0 1.00 1.00 1.00 15\n 1 0.88 1.00 0.93 14\n 2 1.00 0.88 0.93 16\n\n accuracy 0.96 45\n macro avg 0.96 0.96 0.96 45\nweighted avg 0.96 0.96 0.96 45\n\n"}],"execution_count":12},{"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":{"toc":{"sideBar":true,"nav_menu":{},"toc_cell":false,"title_cell":"Table of Contents","toc_position":{},"skip_h1_title":false,"title_sidebar":"Contents","base_numbering":1,"number_sections":true,"toc_window_display":false,"toc_section_display":true},"colab":{"name":"Adaboost - CoderHouse (Ejemplo 1).ipynb","provenance":[],"collapsed_sections":[]},"deepnote":{},"kernelspec":{"name":"python3","language":"python","display_name":"Python 3"},"varInspector":{"cols":{"lenVar":40,"lenName":16,"lenType":16},"kernels_config":{"r":{"library":"var_list.r","varRefreshCmd":"cat(var_dic_list()) ","delete_cmd_prefix":"rm(","delete_cmd_postfix":") "},"python":{"library":"var_list.py","varRefreshCmd":"print(var_dic_list())","delete_cmd_prefix":"del ","delete_cmd_postfix":""}},"window_display":false,"types_to_exclude":["module","function","builtin_function_or_method","instance","_Feature"]},"language_info":{"name":"python","version":"3.8.5","mimetype":"text/x-python","file_extension":".py","pygments_lexer":"ipython3","codemirror_mode":{"name":"ipython","version":3},"nbconvert_exporter":"python"},"deepnote_notebook_id":"ad70077d77d24253a713d3725e98b7aa","deepnote_execution_queue":[]}}