{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "[![Open in Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/justmarkham/scikit-learn-tips/master?filepath=notebooks%2F34_feature_selection.ipynb)\n", "\n", "[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/justmarkham/scikit-learn-tips/blob/master/notebooks/34_feature_selection.ipynb)\n", "\n", "# 🤖⚡ scikit-learn tip #34 ([video](https://www.youtube.com/watch?v=BMBVwV8iarc&list=PL5-da3qGB5ID7YYAqireYEew2mWVvgmj6&index=34))\n", "\n", "It's simple to add feature selection to a Pipeline:\n", "\n", "1. Use SelectPercentile to keep the highest scoring features\n", "2. Add feature selection after preprocessing but before model building\n", "\n", "See example 👇\n", "\n", "P.S. Make sure to tune the percentile value!" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "df = pd.read_csv('http://bit.ly/kaggletrain')" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "X = df['Name']\n", "y = df['Survived']" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from sklearn.feature_extraction.text import CountVectorizer\n", "from sklearn.linear_model import LogisticRegression\n", "from sklearn.pipeline import make_pipeline\n", "from sklearn.model_selection import cross_val_score" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Pipeline without feature selection" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "vect = CountVectorizer()\n", "clf = LogisticRegression()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.7957190383528967" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pipe = make_pipeline(vect, clf)\n", "cross_val_score(pipe, X, y, scoring='accuracy').mean()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Pipeline with feature selection" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "from sklearn.feature_selection import SelectPercentile, chi2" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# keep 50% of features with the best chi-squared scores\n", "selection = SelectPercentile(chi2, percentile=50)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.8147824995292197" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pipe = make_pipeline(vect, selection, clf)\n", "cross_val_score(pipe, X, y, scoring='accuracy').mean()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Want more tips? [View all tips on GitHub](https://github.com/justmarkham/scikit-learn-tips) or [Sign up to receive 2 tips by email every week](https://scikit-learn.tips) 💌\n", "\n", "© 2020 [Data School](https://www.dataschool.io). All rights reserved." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.4" } }, "nbformat": 4, "nbformat_minor": 4 }