Loading content...
In machine learning and statistical modeling, polynomial feature expansion is a powerful technique for capturing non-linear relationships between input variables. By transforming original features into polynomial combinations, we enable linear models to learn complex curved decision boundaries and regression surfaces.
Given a 2D NumPy array X with shape (n_samples, n_features) and a positive integer degree, your task is to generate all polynomial feature combinations of the columns up to the specified degree (inclusive), and then sort the resulting features for each sample in ascending order.
Understanding Polynomial Feature Expansion:
For an input with two features [x₁, x₂] and degree 2, the polynomial features include:
The complete unsorted polynomial feature set would be: [1, x₁, x₂, x₁², x₁·x₂, x₂²]
After computing these polynomial terms for each sample, sort the values within each row from lowest to highest.
Why Sort the Features?
While polynomial features are typically kept in a specific order for interpretability, sorting them creates a canonical representation that can be useful for:
Your Task: Write a Python function that:
X and an integer degreeX = np.array([[2.0, 3.0], [3.0, 4.0], [5.0, 6.0]])
degree = 2[[1.0, 2.0, 3.0, 4.0, 6.0, 9.0], [1.0, 3.0, 4.0, 9.0, 12.0, 16.0], [1.0, 5.0, 6.0, 25.0, 30.0, 36.0]]For degree 2 with 2 features, the polynomial terms are: [1, x₁, x₂, x₁², x₁·x₂, x₂²].
Sample 1 (x₁=2, x₂=3): • Unsorted terms: [1, 2, 3, 4, 6, 9] → Sorted: [1, 2, 3, 4, 6, 9]
Sample 2 (x₁=3, x₂=4): • Unsorted terms: [1, 3, 4, 9, 12, 16] → Sorted: [1, 3, 4, 9, 12, 16]
Sample 3 (x₁=5, x₂=6): • Unsorted terms: [1, 5, 6, 25, 30, 36] → Sorted: [1, 5, 6, 25, 30, 36]
In these cases, the polynomial terms happen to be naturally ordered.
X = np.array([[1.0, 2.0], [3.0, 4.0]])
degree = 1[[1.0, 1.0, 2.0], [1.0, 3.0, 4.0]]For degree 1, only the bias term and original features are included: [1, x₁, x₂].
Sample 1 (x₁=1, x₂=2): • Terms: [1, 1, 2] → Already sorted: [1.0, 1.0, 2.0]
Sample 2 (x₁=3, x₂=4): • Terms: [1, 3, 4] → Already sorted: [1.0, 3.0, 4.0]
Note: The first '1' is the bias term, the second '1' is the feature value.
X = np.array([[2.0], [3.0], [4.0]])
degree = 3[[1.0, 2.0, 4.0, 8.0], [1.0, 3.0, 9.0, 27.0], [1.0, 4.0, 16.0, 64.0]]For a single feature with degree 3, the polynomial terms are: [1, x, x², x³].
Sample 1 (x=2): • Terms: [1, 2, 4, 8] → Powers of 2: [2⁰, 2¹, 2², 2³]
Sample 2 (x=3): • Terms: [1, 3, 9, 27] → Powers of 3: [3⁰, 3¹, 3², 3³]
Sample 3 (x=4): • Terms: [1, 4, 16, 64] → Powers of 4: [4⁰, 4¹, 4², 4³]
All values are naturally in ascending order since we're dealing with positive values greater than 1.
Constraints