v Create Interaction Features - Machine Learning

Create Interaction Features

Preliminaries

# Load libraries
from sklearn.preprocessing import PolynomialFeatures
import numpy as np

Create Feature Matrix

# Create feature matrix
X = np.array([[2, 3], 
              [2, 3], 
              [2, 3]])

Add Interaction Features

# Create PolynomialFeatures object with interaction_only set to True
interaction = PolynomialFeatures(degree=2, interaction_only=True, include_bias=False)

# Transform feature matrix
interaction.fit_transform(X)
array([[ 2.,  3.,  6.],
       [ 2.,  3.,  6.],
       [ 2.,  3.,  6.]])