Generate sorted polynomial features up to a given degree for a dataset. Given a 2D NumPy array of shape (n_samples, n_features) and a degree d, produce all polynomial feature combinations up to degree d, sorted in graded lexicographic order.
import numpy as np
from itertools import combinations_with_replacement
def generate_polynomial_features(X, degree):
n_samples, n_features = X.shape
feature_indices = range(n_features)
output_features = []
for d in range(degree + 1):
for combo in combinations_with_replacement(feature_indices, d):
feature_col = np.ones(n_samples)
for idx in combo:
feature_col *= X[:, idx]
output_features.append(feature_col)
return np.column_stack(output_features) if output_features else np.ones((n_samples, 1))d, generate all combinations with replacement of feature indices.