{"cells":[{"cell_type":"markdown","source":"# Feature Selection : Wrapper Methods\n\nEl proceso de selección de características se basa en un algoritmo de aprendizaje automático específico que intentamos encajar en un conjunto de datos determinado.\n\nSigue un enfoque de búsqueda codiciosa al evaluar todas las posibles combinaciones de características contra el criterio de evaluación. El criterio de evaluación es simplemente la medida del desempeño que depende del tipo de problema, por ejemplo, para el criterio de evaluación de regresión puede ser p-valores, R-cuadrado, R-cuadrado ajustado, de manera similar para la clasificación el criterio de evaluación puede ser accuracy, precision, recall, puntaje f1, etc. Finalmente, selecciona la combinación de características que da el resultados óptimos para el algoritmo de aprendizaje automático especificado.\n\n","metadata":{"id":"_M75CFhDyXXg","cell_id":"d5b7f6a40bbd418fbf14242fe717338f","deepnote_cell_type":"markdown"}},{"cell_type":"markdown","source":"Los metodos mas comunes son:\n1. Forward Selection\n2. Backward elimination\n3. Bi-directional elimination (stepwise)\n\nAhora analicemos los métodos con un ejemplo del conjunto de datos de precios de la vivienda de Boston disponible en sklearn. El conjunto de datos contiene 506 observaciones de 14 características diferentes. El conjunto de datos se puede importar utilizando la función load_boston() disponible en el módulo sklearn.datasets.","metadata":{"id":"tfSn3TAxyt8R","cell_id":"e00ea4a890d644febf97c553f7c88eee","deepnote_cell_type":"markdown"}},{"cell_type":"code","source":"from sklearn.datasets import load_boston\nboston = load_boston()\nprint(boston.data.shape) # dataset dimension\nprint(boston.feature_names) # nombre feature \nprint(boston.target) # target variable\nprint(boston.DESCR) # data description","metadata":{"id":"6ptXwgv9Nbvg","colab":{"base_uri":"https://localhost:8080/"},"cell_id":"d7493c525e6e49fd8d83abfdbb0dd1d4","outputId":"c7382232-66f6-427b-aa5f-e565c4e740d2","executionInfo":{"user":{"userId":"04741209928239412574","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gi4e7mWJaOA2l-1KUn-omyigRGSrm83lG6XLzS5=s64","displayName":"david francisco bustos usta"},"status":"ok","elapsed":451,"user_tz":300,"timestamp":1642854243684},"deepnote_cell_type":"code"},"outputs":[{"output_type":"stream","name":"stdout","text":"(506, 13)\n['CRIM' 'ZN' 'INDUS' 'CHAS' 'NOX' 'RM' 'AGE' 'DIS' 'RAD' 'TAX' 'PTRATIO'\n 'B' 'LSTAT']\n[24. 21.6 34.7 33.4 36.2 28.7 22.9 27.1 16.5 18.9 15. 18.9 21.7 20.4\n 18.2 19.9 23.1 17.5 20.2 18.2 13.6 19.6 15.2 14.5 15.6 13.9 16.6 14.8\n 18.4 21. 12.7 14.5 13.2 13.1 13.5 18.9 20. 21. 24.7 30.8 34.9 26.6\n 25.3 24.7 21.2 19.3 20. 16.6 14.4 19.4 19.7 20.5 25. 23.4 18.9 35.4\n 24.7 31.6 23.3 19.6 18.7 16. 22.2 25. 33. 23.5 19.4 22. 17.4 20.9\n 24.2 21.7 22.8 23.4 24.1 21.4 20. 20.8 21.2 20.3 28. 23.9 24.8 22.9\n 23.9 26.6 22.5 22.2 23.6 28.7 22.6 22. 22.9 25. 20.6 28.4 21.4 38.7\n 43.8 33.2 27.5 26.5 18.6 19.3 20.1 19.5 19.5 20.4 19.8 19.4 21.7 22.8\n 18.8 18.7 18.5 18.3 21.2 19.2 20.4 19.3 22. 20.3 20.5 17.3 18.8 21.4\n 15.7 16.2 18. 14.3 19.2 19.6 23. 18.4 15.6 18.1 17.4 17.1 13.3 17.8\n 14. 14.4 13.4 15.6 11.8 13.8 15.6 14.6 17.8 15.4 21.5 19.6 15.3 19.4\n 17. 15.6 13.1 41.3 24.3 23.3 27. 50. 50. 50. 22.7 25. 50. 23.8\n 23.8 22.3 17.4 19.1 23.1 23.6 22.6 29.4 23.2 24.6 29.9 37.2 39.8 36.2\n 37.9 32.5 26.4 29.6 50. 32. 29.8 34.9 37. 30.5 36.4 31.1 29.1 50.\n 33.3 30.3 34.6 34.9 32.9 24.1 42.3 48.5 50. 22.6 24.4 22.5 24.4 20.\n 21.7 19.3 22.4 28.1 23.7 25. 23.3 28.7 21.5 23. 26.7 21.7 27.5 30.1\n 44.8 50. 37.6 31.6 46.7 31.5 24.3 31.7 41.7 48.3 29. 24. 25.1 31.5\n 23.7 23.3 22. 20.1 22.2 23.7 17.6 18.5 24.3 20.5 24.5 26.2 24.4 24.8\n 29.6 42.8 21.9 20.9 44. 50. 36. 30.1 33.8 43.1 48.8 31. 36.5 22.8\n 30.7 50. 43.5 20.7 21.1 25.2 24.4 35.2 32.4 32. 33.2 33.1 29.1 35.1\n 45.4 35.4 46. 50. 32.2 22. 20.1 23.2 22.3 24.8 28.5 37.3 27.9 23.9\n 21.7 28.6 27.1 20.3 22.5 29. 24.8 22. 26.4 33.1 36.1 28.4 33.4 28.2\n 22.8 20.3 16.1 22.1 19.4 21.6 23.8 16.2 17.8 19.8 23.1 21. 23.8 23.1\n 20.4 18.5 25. 24.6 23. 22.2 19.3 22.6 19.8 17.1 19.4 22.2 20.7 21.1\n 19.5 18.5 20.6 19. 18.7 32.7 16.5 23.9 31.2 17.5 17.2 23.1 24.5 26.6\n 22.9 24.1 18.6 30.1 18.2 20.6 17.8 21.7 22.7 22.6 25. 19.9 20.8 16.8\n 21.9 27.5 21.9 23.1 50. 50. 50. 50. 50. 13.8 13.8 15. 13.9 13.3\n 13.1 10.2 10.4 10.9 11.3 12.3 8.8 7.2 10.5 7.4 10.2 11.5 15.1 23.2\n 9.7 13.8 12.7 13.1 12.5 8.5 5. 6.3 5.6 7.2 12.1 8.3 8.5 5.\n 11.9 27.9 17.2 27.5 15. 17.2 17.9 16.3 7. 7.2 7.5 10.4 8.8 8.4\n 16.7 14.2 20.8 13.4 11.7 8.3 10.2 10.9 11. 9.5 14.5 14.1 16.1 14.3\n 11.7 13.4 9.6 8.7 8.4 12.8 10.5 17.1 18.4 15.4 10.8 11.8 14.9 12.6\n 14.1 13. 13.4 15.2 16.1 17.8 14.9 14.1 12.7 13.5 14.9 20. 16.4 17.7\n 19.5 20.2 21.4 19.9 19. 19.1 19.1 20.1 19.9 19.6 23.2 29.8 13.8 13.3\n 16.7 12. 14.6 21.4 23. 23.7 25. 21.8 20.6 21.2 19.1 20.6 15.2 7.\n 8.1 13.6 20.1 21.8 24.5 23.1 19.7 18.3 21.2 17.5 16.8 22.4 20.6 23.9\n 22. 11.9]\n.. _boston_dataset:\n\nBoston house prices dataset\n---------------------------\n\n**Data Set Characteristics:** \n\n :Number of Instances: 506 \n\n :Number of Attributes: 13 numeric/categorical predictive. Median Value (attribute 14) is usually the target.\n\n :Attribute Information (in order):\n - CRIM per capita crime rate by town\n - ZN proportion of residential land zoned for lots over 25,000 sq.ft.\n - INDUS proportion of non-retail business acres per town\n - CHAS Charles River dummy variable (= 1 if tract bounds river; 0 otherwise)\n - NOX nitric oxides concentration (parts per 10 million)\n - RM average number of rooms per dwelling\n - AGE proportion of owner-occupied units built prior to 1940\n - DIS weighted distances to five Boston employment centres\n - RAD index of accessibility to radial highways\n - TAX full-value property-tax rate per $10,000\n - PTRATIO pupil-teacher ratio by town\n - B 1000(Bk - 0.63)^2 where Bk is the proportion of black people by town\n - LSTAT % lower status of the population\n - MEDV Median value of owner-occupied homes in $1000's\n\n :Missing Attribute Values: None\n\n :Creator: Harrison, D. and Rubinfeld, D.L.\n\nThis is a copy of UCI ML housing dataset.\nhttps://archive.ics.uci.edu/ml/machine-learning-databases/housing/\n\n\nThis dataset was taken from the StatLib library which is maintained at Carnegie Mellon University.\n\nThe Boston house-price data of Harrison, D. and Rubinfeld, D.L. 'Hedonic\nprices and the demand for clean air', J. Environ. Economics & Management,\nvol.5, 81-102, 1978. Used in Belsley, Kuh & Welsch, 'Regression diagnostics\n...', Wiley, 1980. N.B. Various transformations are used in the table on\npages 244-261 of the latter.\n\nThe Boston house-price data has been used in many machine learning papers that address regression\nproblems. \n \n.. topic:: References\n\n - Belsley, Kuh & Welsch, 'Regression diagnostics: Identifying Influential Data and Sources of Collinearity', Wiley, 1980. 244-261.\n - Quinlan,R. (1993). Combining Instance-Based and Model-Based Learning. In Proceedings on the Tenth International Conference of Machine Learning, 236-243, University of Massachusetts, Amherst. Morgan Kaufmann.\n\n"},{"output_type":"stream","name":"stderr","text":"/usr/local/lib/python3.7/dist-packages/sklearn/utils/deprecation.py:87: FutureWarning: Function load_boston is deprecated; `load_boston` is deprecated in 1.0 and will be removed in 1.2.\n\n The Boston housing prices dataset has an ethical problem. You can refer to\n the documentation of this function for further details.\n\n The scikit-learn maintainers therefore strongly discourage the use of this\n dataset unless the purpose of the code is to study and educate about\n ethical issues in data science and machine learning.\n\n In this special case, you can fetch the dataset from the original\n source::\n\n import pandas as pd\n import numpy as np\n\n\n data_url = \"http://lib.stat.cmu.edu/datasets/boston\"\n raw_df = pd.read_csv(data_url, sep=\"\\s+\", skiprows=22, header=None)\n data = np.hstack([raw_df.values[::2, :], raw_df.values[1::2, :2]])\n target = raw_df.values[1::2, 2]\n\n Alternative datasets include the California housing dataset (i.e.\n :func:`~sklearn.datasets.fetch_california_housing`) and the Ames housing\n dataset. You can load the datasets as follows::\n\n from sklearn.datasets import fetch_california_housing\n housing = fetch_california_housing()\n\n for the California housing dataset and::\n\n from sklearn.datasets import fetch_openml\n housing = fetch_openml(name=\"house_prices\", as_frame=True)\n\n for the Ames housing dataset.\n \n warnings.warn(msg, category=FutureWarning)\n"}],"execution_count":1},{"cell_type":"markdown","source":"Convirtamos estos datos sin procesar en un marco de datos que incluya la variable de destino y los datos reales junto con los nombres de las funciones.","metadata":{"id":"VZHZMmqozVXV","cell_id":"0b383a4662a3485ab34b18a5d6529f78","deepnote_cell_type":"markdown"}},{"cell_type":"code","source":"import pandas as pd\nbos = pd.DataFrame(boston.data, columns = boston.feature_names)\nbos['Price'] = boston.target\nX = bos.drop(\"Price\", 1) # feature matrix\ny = bos['Price'] # target feature\nbos.head()","metadata":{"id":"Jw7Z9pM0zYX2","colab":{"height":206,"base_uri":"https://localhost:8080/"},"cell_id":"0e566c45452846d0822bc1e589e1c5cc","outputId":"a36f70a3-412e-4133-b23b-d83d3d763694","executionInfo":{"user":{"userId":"04741209928239412574","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gi4e7mWJaOA2l-1KUn-omyigRGSrm83lG6XLzS5=s64","displayName":"david francisco bustos usta"},"status":"ok","elapsed":497,"user_tz":300,"timestamp":1642854247373},"deepnote_cell_type":"code"},"outputs":[{"output_type":"execute_result","data":{"text/html":"\n
| \n | CRIM | \nZN | \nINDUS | \nCHAS | \nNOX | \nRM | \nAGE | \nDIS | \nRAD | \nTAX | \nPTRATIO | \nB | \nLSTAT | \nPrice | \n
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | \n0.00632 | \n18.0 | \n2.31 | \n0.0 | \n0.538 | \n6.575 | \n65.2 | \n4.0900 | \n1.0 | \n296.0 | \n15.3 | \n396.90 | \n4.98 | \n24.0 | \n
| 1 | \n0.02731 | \n0.0 | \n7.07 | \n0.0 | \n0.469 | \n6.421 | \n78.9 | \n4.9671 | \n2.0 | \n242.0 | \n17.8 | \n396.90 | \n9.14 | \n21.6 | \n
| 2 | \n0.02729 | \n0.0 | \n7.07 | \n0.0 | \n0.469 | \n7.185 | \n61.1 | \n4.9671 | \n2.0 | \n242.0 | \n17.8 | \n392.83 | \n4.03 | \n34.7 | \n
| 3 | \n0.03237 | \n0.0 | \n2.18 | \n0.0 | \n0.458 | \n6.998 | \n45.8 | \n6.0622 | \n3.0 | \n222.0 | \n18.7 | \n394.63 | \n2.94 | \n33.4 | \n
| 4 | \n0.06905 | \n0.0 | \n2.18 | \n0.0 | \n0.458 | \n7.147 | \n54.2 | \n6.0622 | \n3.0 | \n222.0 | \n18.7 | \n396.90 | \n5.33 | \n36.2 | \n
| \n | CRIM | \nZN | \nINDUS | \nCHAS | \nNOX | \nRM | \nAGE | \nDIS | \nRAD | \nTAX | \nPTRATIO | \nB | \nLSTAT | \n
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | \n0.00632 | \n18.0 | \n2.31 | \n0.0 | \n0.538 | \n6.575 | \n65.2 | \n4.0900 | \n1.0 | \n296.0 | \n15.3 | \n396.90 | \n4.98 | \n
| 1 | \n0.02731 | \n0.0 | \n7.07 | \n0.0 | \n0.469 | \n6.421 | \n78.9 | \n4.9671 | \n2.0 | \n242.0 | \n17.8 | \n396.90 | \n9.14 | \n
| 2 | \n0.02729 | \n0.0 | \n7.07 | \n0.0 | \n0.469 | \n7.185 | \n61.1 | \n4.9671 | \n2.0 | \n242.0 | \n17.8 | \n392.83 | \n4.03 | \n
| 3 | \n0.03237 | \n0.0 | \n2.18 | \n0.0 | \n0.458 | \n6.998 | \n45.8 | \n6.0622 | \n3.0 | \n222.0 | \n18.7 | \n394.63 | \n2.94 | \n
| 4 | \n0.06905 | \n0.0 | \n2.18 | \n0.0 | \n0.458 | \n7.147 | \n54.2 | \n6.0622 | \n3.0 | \n222.0 | \n18.7 | \n396.90 | \n5.33 | \n
| ... | \n... | \n... | \n... | \n... | \n... | \n... | \n... | \n... | \n... | \n... | \n... | \n... | \n... | \n
| 501 | \n0.06263 | \n0.0 | \n11.93 | \n0.0 | \n0.573 | \n6.593 | \n69.1 | \n2.4786 | \n1.0 | \n273.0 | \n21.0 | \n391.99 | \n9.67 | \n
| 502 | \n0.04527 | \n0.0 | \n11.93 | \n0.0 | \n0.573 | \n6.120 | \n76.7 | \n2.2875 | \n1.0 | \n273.0 | \n21.0 | \n396.90 | \n9.08 | \n
| 503 | \n0.06076 | \n0.0 | \n11.93 | \n0.0 | \n0.573 | \n6.976 | \n91.0 | \n2.1675 | \n1.0 | \n273.0 | \n21.0 | \n396.90 | \n5.64 | \n
| 504 | \n0.10959 | \n0.0 | \n11.93 | \n0.0 | \n0.573 | \n6.794 | \n89.3 | \n2.3889 | \n1.0 | \n273.0 | \n21.0 | \n393.45 | \n6.48 | \n
| 505 | \n0.04741 | \n0.0 | \n11.93 | \n0.0 | \n0.573 | \n6.030 | \n80.8 | \n2.5050 | \n1.0 | \n273.0 | \n21.0 | \n396.90 | \n7.88 | \n
506 rows × 13 columns
\n