转自:吴恩达机器学习ex2对多项式拟合生成矩阵
简单看了一下,我觉得思路就是:不同未知数的幂次方总和依次循环(由小到大),主未知数(一般为X1)的幂次数由大到小,次未知数(X2)的幂次数由小到大,.........最后一个未知数(Xn)的幂次数由小到大。
function out = mapFeature(X1, X2)
% MAPFEATURE Feature mapping function to polynomial features
%
% MAPFEATURE(X1, X2) maps the two input features
% to quadratic features used in the regularization exercise.
%
% Returns a new feature array with more features, comprising of
% X1, X2, X1.^2, X2.^2, X1*X2, X1*X2.^2, etc..
%
% Inputs X1, X2 must be the same size
%
degree = 6;
out = ones(size(X1(:,1)));
for i = 1:degree
for j = 0:i
out(:, end+1) = (X1.^(i-j)).*(X2.^j);
end
end
end
eg: 有4个未知数
i = 1: degree
j = 0:i
k=0:j
w=0:k
x1.^(i-j).* X2.^(j-k) .* X3.^(k-w) * X4.^w