How to use x2fx (or alternatives) for polynomial feature creation to the 3rd degree?

8 views (last 30 days)
I have a matrix with 4 columns, which are explaining variables for something I am trying to find a solution for through machine learning.
To get more explaining power, I want to create polynomial features to the 3rd degree for the ML model. Let's say my column names are {A, B, C, D} and my matrix of explaining variables is:
matrix = magic(4)
matrix =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
I know of the function x2fx, however, that only helps me to get the 2nd degree of polynomial features:
polyData = x2fx(matrix, 'quadratic')
polyData =
1 16 2 3 13 32 48 208 6 26 39 256 4 9 169
1 5 11 10 8 55 50 40 110 88 80 25 121 100 64
1 9 7 6 12 63 54 108 42 84 72 81 49 36 144
1 4 14 15 1 56 60 4 210 14 15 16 196 225 1
In this case the column names would be {1, A, B, C, D, AB, AC, AD, BC, BD, CD, A^2, B^2, C^2, D^2}, as that is how x2fx works.
I want to achieve all polynomial features to the 3rd degree, e.g. until {..., A^3, B^3, C^3, D^3}. In python this is possible through the sklearn toolbox and PolynomialFeatures.
Through research on the internet I couldn't find a solution for this, only a MATLAB Answers post asking for the same thing (without any answers) and a 10yrs old blog post, where this was proposed for a matrix with 3 variables:
matrix3 = magic(3)
matrix3 =
8 1 6
3 5 7
4 9 2
m = fullfact([4 4 4]) - 1;
m(sum(m, 2) > 3, :) = []
m = % This is the matrix that x2fx will use for a custom calculation
0 0 0
1 0 0
2 0 0
3 0 0
0 1 0
1 1 0
2 1 0
0 2 0
1 2 0
0 3 0
0 0 1
1 0 1
2 0 1
0 1 1
1 1 1
0 2 1
0 0 2
1 0 2
0 1 2
0 0 3
polyData3 = x2fx(matrix3, m);
My problem is that for a matrix with 4 variables, this solution is going to be very messy for purposes of
a) other people understanding what I did in the code (I work in a group), and
b) renaming the columns in no clear and easy way (compared to just using the default x2fx)
Is there an easier way to do that or a function that I have not yet heard of? Thank you!

Answers (1)

Aditya
Aditya on 4 Jun 2024
For generating polynomial features up to the 3rd degree (or any specified degree) in MATLAB, especially when x2fx does not directly support higher degrees beyond quadratic terms, you can create a custom function to automate this process. While MATLAB does not have an out-of-the-box function equivalent to PolynomialFeatures from scikit-learn, the process can be implemented manually with a bit of coding.
Given your requirements and the complexity involved in manually specifying interactions for a 4-variable matrix to the 3rd degree, a more generalized approach would be beneficial. Here's a way to generate polynomial features up to the 3rd degree, including interaction terms, for any given matrix:
Step 1: Define the Function
You can define a function that takes in your original matrix and the degree of polynomial features you want to generate. This function will iterate through each combination of features up to the specified degree, including the interaction terms.
function polyFeatures = generatePolyFeatures(X, degree)
% X is the input matrix of size m x n
% degree is the polynomial degree to generate features for
% Initialize polyFeatures with the bias term
polyFeatures = ones(size(X,1), 1);
% Iterate through degrees
for d = 1:degree
% Generate all combinations for the current degree
combos = nchoosek(1:size(X,2), d);
for i = 1:size(combos, 1)
% For each combination, calculate the product (for interaction terms)
comboFeatures = X(:, combos(i, 1));
for j = 2:size(combos, 2)
comboFeatures = comboFeatures .* X(:, combos(i, j));
end
% For each combination, also generate individual powers
for k = 1:d
polyFeatures = [polyFeatures, comboFeatures.^k];
end
end
end
end
Step 2: Use the Function
You can then use this function to generate polynomial features for your matrix up to the 3rd degree:
matrix = magic(4);
polyFeatures = generatePolyFeatures(matrix, 3);

Categories

Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!