使用多变量多项式的特征映射

Tho*_*oth 8 mapping matlab machine-learning octave linear-regression

考虑一下我们有一个数据矩阵 数据点 我们有兴趣将这些数据点映射到更高维度的特征空间.我们可以通过使用d次多项式来做到这一点.因此对于一系列 数据指向新的数据矩阵

我研究了一个相关的脚本(Andrew Ng.在线课程),它将二维数据点转换为更高的特征空间.但是,我无法想出一种在任意高维样本中推广的方法,.这是代码:

d = 6;
m = size(D,1); 
new = ones(m);
for k = 1:d
    for l = 0:k
        new(:, end+1) = (x1.^(k-l)).*(x2.^l);
    end
end
Run Code Online (Sandbox Code Playgroud)

我们可以矢量化这段代码吗?还给出了数据矩阵 你能否就如何使用d维多项式将任意维度的数据点转换为更高维数的方法提出建议?

PS:d维数据点的推广将非常有用.

Mat*_*unn 8

该解决方案可以处理k变量并生成度d多项式的所有项,其中kd是非负整数.大多数代码长度是由于dk变量中生成度多项式的所有项的组合复杂性.

这需要一个n_obs通过k数据矩阵X,其中n_obs是观测值的数量和k是变量的数目.

辅助功能

此函数生成所有可能的行,使得每个条目都是非负整数,并且行总和为正整数:

the row [0, 1, 3, 0, 1]  corresponds to (x1^0)*(x1^1)*(x2^3)*(x4^0)*(x5^1)
Run Code Online (Sandbox Code Playgroud)

该功能(几乎可以肯定地写得更有效)是:

function result = mg_sums(n_numbers, d)
if(n_numbers<=1)
    result = d;
else
    result = zeros(0, n_numbers);    
    for(i = d:-1:0)
        rc = mg_sums(n_numbers - 1, d - i);
        result = [result; i * ones(size(rc,1), 1), rc];
    end    
end
Run Code Online (Sandbox Code Playgroud)

初始化代码

n_obs  = 1000;  % number observations
n_vars = 3;     % number of variables
max_degree  = 4;     % order of polynomial

X = rand(n_obs, n_vars);  % generate random, strictly positive data

stacked = zeros(0, n_vars); %this will collect all the coefficients...    
for(d = 1:max_degree)          % for degree 1 polynomial to degree 'order'
    stacked = [stacked; mg_sums(n_vars, d)];
end
Run Code Online (Sandbox Code Playgroud)

最后一步:方法1

newX = zeros(size(X,1), size(stacked,1));
for(i = 1:size(stacked,1))
    accumulator = ones(n_obs, 1);
    for(j = 1:n_vars)
        accumulator = accumulator .* X(:,j).^stacked(i,j);
    end
    newX(:,i) = accumulator;
end
Run Code Online (Sandbox Code Playgroud)

使用任一方法1或方法2.

最后一步:方法2(要求数据矩阵X中的所有数据都是严格正数的(问题是如果你有0个元素,那么-inf当你调用矩阵代数例程时它不能正常传播.)

newX = real(exp(log(X) * stacked'));  % multiplying log of data matrix by the    
                                % matrix of all possible exponent combinations
                                % effectively raises terms to powers and multiplies them!
Run Code Online (Sandbox Code Playgroud)

示例运行

X = [2, 3, 5];
max_degree = 3;
Run Code Online (Sandbox Code Playgroud)

堆叠矩阵和它代表的多项式项是:

 1     0     0        x1           2
 0     1     0        x2           3 
 0     0     1        x3           5
 2     0     0        x1.^2        4
 1     1     0        x1.*x2       6
 1     0     1        x1.*x3       10
 0     2     0        x2.^2        9
 0     1     1        x2.*x3       15
 0     0     2        x3.^2        25
 3     0     0        x1.^3        8
 2     1     0        x1.^2.*x2    12
 2     0     1        x1.^2.*x3    20
 1     2     0        x1.*x2.^2    18
 1     1     1        x1.*x2.*x3   30
 1     0     2        x1.*x3.^2    50
 0     3     0        x2.^3        27
 0     2     1        x2.^2.*x3    45
 0     1     2        x2.*x3.^2    75
 0     0     3        x3.^3        125
Run Code Online (Sandbox Code Playgroud)

如果数据矩阵X[2, 3, 5]正确生成的:

newX = [2, 3, 5, 4, 6, 10, 9, 15, 25, 8, 12, 20, 18, 30, 50, 27, 45, 75, 125];
Run Code Online (Sandbox Code Playgroud)

当第一列是x1,第二点是x2,第三是x3,第四是x1.^2,5日是x1.*x2等...