{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# \ud83d\udcdd Exercise 01\n", "\n", "The aim of this exercise is to highlight caveats to have in mind when using\n", "feature selection. You have to be extremely careful regarding the set of data\n", "on which you will compute the statistic that helps your feature selection\n", "algorithm to decide which feature to select.\n", "\n", "On purpose, we will make you program the wrong way of doing feature selection\n", "to gain insights.\n", "\n", "First, you will create a completely random dataset using NumPy. Using the\n", "function `np.random.randn`, generate a matrix `data` containing 100 samples\n", "and 100,000 features. Then, using the function `np.random.randint`, generate a\n", "vector `target` with 100 samples containing either 0 or 1.\n", "\n", "This type of dimensionality is typical in bioinformatics when dealing with\n", "RNA-seq. However, we will use completely randomized features such that we\n", "don't have a link between the data and the target. Thus, the generalization\n", "performance of any machine-learning model should not perform better than the\n", "chance-level." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "# Write your code here." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, create a logistic regression model and use cross-validation to check the\n", "score of such a model. It will allow use to confirm that our model cannot\n", "predict anything meaningful from random data." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Write your code here." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, we will ask you to program the **wrong** pattern to select feature.\n", "Select the feature by using the entire dataset. We will choose ten features\n", "with the highest ANOVA F-score computed on the full dataset. Subsequently,\n", "subsample the dataset `data` by selecting the features' subset. Finally, train\n", "and test a logistic regression model.\n", "\n", "You should get some surprising results." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from sklearn.feature_selection import SelectKBest, f_classif\n", "\n", "# Write your code here." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, we will make you program the **right** way to do the feature selection.\n", "First, split the dataset into a training and testing set. Then, fit the\n", "feature selector on the training set. Then, transform both the training and\n", "testing sets before you train and test the logistic regression." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from sklearn.model_selection import train_test_split\n", "\n", "# Write your code here." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "However, the previous case is not perfect. For instance, if we were asking to\n", "perform cross-validation, the manual `fit`/`transform` of the datasets will\n", "make our life hard. Indeed, the solution here is to use a scikit-learn\n", "pipeline in which the feature selection will be a pre processing stage before\n", "to train the model.\n", "\n", "Thus, start by creating a pipeline with the feature selector and the logistic\n", "regression. Then, use cross-validation to get an estimate of the uncertainty\n", "of your model generalization performance." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from sklearn.pipeline import make_pipeline\n", "\n", "# Write your code here." ] } ], "metadata": { "jupytext": { "main_language": "python" }, "kernelspec": { "display_name": "Python 3", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }