对于线性不可分的非线性数据,我们怎么应用LR模型来进行预测和分类呢?
lr通过增加大量非线性特征,使得获得非线性切割能力。
数据可视化
显然这些数据是线性不可分的。那么我们能做的就是对每个数据点做特征映射,形成高维特征,这里采用多项式核,将2维特征(扩展为28维)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
X = mapFeature(X(:,1), X(:,2)); 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 |