0

I am trying to create interaction variables for a logistic regression model. I have 70+ features of which I only want to perform preprocessing on 6 of those features. Does anyone know how to take the numpy array from fit_transform and join these interactions back to may original dataframe? Also, is there an elegant way to label the interactions so I know what I am looking at? I’d imagine I’d take the numpy array and convert to dataframe via pd.DateFrame, but I am a little lost after that. Thank you in advance. I found the question below, but I was still somewhat confused for my particular use case.

How to use sklearn fit_transform with pandas and return dataframe instead of numpy array?

My code so far is as follows...

# Subset of dataframe to create interaction variables from 
df_interactions = df[['x1','x2','x3','x4','x5','x6']]

from sklearn.preprocessing import PolynomialFeatures
poly = PolynomialFeatures(interaction_only=True)
df_interactions_T = poly.fit_transform(degrees=2, df_interactions)
bbal20
  • 113
  • 4
  • 11
  • Just set it to the columns: `df[['x1','x2','x3','x4','x5','x6']] = poly.fit_transform(degrees=2, df[['x1','x2','x3','x4','x5','x6']])` – Ben Pap Jan 14 '20 at 22:52

1 Answers1

0

Short Answer

Your columns are like in the following format:

[1,
 'x1',
 'x2',
 'x3',
 'x4',
 'x5',
 'x6',
 'x1 * x2',
 'x1 * x3',
 'x1 * x4',
 'x1 * x5',
 'x1 * x6',
 'x2 * x3',
 'x2 * x4',
 'x2 * x5',
 'x2 * x6',
 'x3 * x4',
 'x3 * x5',
 'x3 * x6',
 'x4 * x5',
 'x4 * x6',
 'x5 * x6']

if you assign these values to the gen_col_names variable, and conver to DataFrame, you can see what is going on.

pd.DataFrame(df_interactions_T,columns=gen_col_names)

Long Answer

Let's visit the source code and see what is going on: https://github.com/scikit-learn/scikit-learn/blob/b194674c4/sklearn/preprocessing/_data.py#L1516

The source code for the combinations is the following:

from itertools import chain, combinations
from itertools import combinations_with_replacement as combinations_w_r

def _combinations(n_features, degree, interaction_only, include_bias):
    comb = (combinations if interaction_only else combinations_w_r)
    start = int(not include_bias)
    return chain.from_iterable(comb(range(n_features), i)
                                   for i in range(start, degree + 1))

Create the data:

import numpy as np
import pandas as pd
np.random.seed(0)
cols = ['x1','x2','x3','x4','x5','x6']
df = pd.DataFrame()

for col in cols:
    df[col] = np.random.randint(1,10,100)

df_interactions = df[['x1','x2','x3','x4','x5','x6']]

from sklearn.preprocessing import PolynomialFeatures
poly = PolynomialFeatures(interaction_only=True,degree=2)
df_interactions_T = poly.fit_transform(df_interactions)

Your parameters are like the following:

n_features = 6
degree = 2
interaction_only = True
include_bias = True
combs = list(_combinations(n_features=6, degree=2, interaction_only=True, include_bias=True))
combs
[(),
 (0,),
 (1,),
 (2,),
 (3,),
 (4,),
 (5,),
 (0, 1),
 (0, 2),
 (0, 3),
 (0, 4),
 (0, 5),
 (1, 2),
 (1, 3),
 (1, 4),
 (1, 5),
 (2, 3),
 (2, 4),
 (2, 5),
 (3, 4),
 (3, 5),
 (4, 5)]

You can use this information to generate column names:

gen_col_names = []
for i in combs:
    if i == ():
        gen_col_names.append(1)
    if len(i) == 1:
        gen_col_names.append(cols[i[0]])
    if len(i) == 2:
        gen_col_names.append(cols[i[0]] + ' * ' + cols[i[1]])

gen_col_names
[1,
 'x1',
 'x2',
 'x3',
 'x4',
 'x5',
 'x6',
 'x1 * x2',
 'x1 * x3',
 'x1 * x4',
 'x1 * x5',
 'x1 * x6',
 'x2 * x3',
 'x2 * x4',
 'x2 * x5',
 'x2 * x6',
 'x3 * x4',
 'x3 * x5',
 'x3 * x6',
 'x4 * x5',
 'x4 * x6',
 'x5 * x6']
1__
  • 1,511
  • 2
  • 9
  • 21