{ "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%2F45_feature_interactions.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/45_feature_interactions.ipynb)\n", "\n", "# 🤖⚡ scikit-learn tip #45 ([video](https://www.youtube.com/watch?v=unP3rCfzROk&list=PL5-da3qGB5ID7YYAqireYEew2mWVvgmj6&index=45))\n", "\n", "Want to include \"feature interactions\" in your model? Use PolynomialFeatures!\n", "\n", "See example 👇\n", "\n", "P.S. This is impractical if you have lots of features, and unnecessary if you're using a tree-based model." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "X = pd.DataFrame({'A':[1, 2, 3], 'B':[4, 4, 4], 'C':[0, 10, 100]})" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from sklearn.preprocessing import PolynomialFeatures\n", "poly = PolynomialFeatures(include_bias=False, interaction_only=True)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | A | \n", "B | \n", "C | \n", "
---|---|---|---|
0 | \n", "1 | \n", "4 | \n", "0 | \n", "
1 | \n", "2 | \n", "4 | \n", "10 | \n", "
2 | \n", "3 | \n", "4 | \n", "100 | \n", "