- 特征映射(两个特征映射成六次多项式)
X = mapFeature(X(:,1), X(:,2));
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
- 假设函数
g = 1./(1+exp(-z)); % 计算时z用 X*theta 替换 % g是m*1向量,用./对每一项进行计算
- 代价函数和偏导
% 无正则项
J =(1/m)*((-y)'*log(sigmoid(X*theta))-(1-y)'*log(1-sigmoid(X*theta)));
grad = ((1/m)*(sigmoid(X*theta)-y)'*X)'; % 和theta维度一样,m*1
% 引入正则项
theta_cp =theta;
theta_cp(1) = 0;
% 计算正则项不包括theta(1)
J = (1/m)*((-y)'*log(sigmoid(X*theta))-(1-y)'*log(1-sigmoid(X*theta)))+(lambda/(2*m))*(theta_cp'*theta_cp);
grad = ((1/m)*(sigmoid(X*theta)-y)'*X)' + (lambda/m)*theta_cp;
无正则项:
引入正则项:
- 使用fminunc得到theta
data = load('ex2data1.txt'); X = data(:, [1, 2]); y = data(:, 3); [m, n] = size(X); X = [ones(m, 1) X]; initial_theta = zeros(n + 1, 1); options = optimset('GradObj', 'on', 'MaxIter', 400); % 无正则项 [theta, cost] = ... fminunc(@(t)(costFunction(t, X, y)), initial_theta, options); % 有正则项 lambda = 1; [theta, J, exit_flag] = ... fminunc(@(t)(costFunctionReg(t, X, y, lambda)), initial_theta, options);
- 预测(阈值0.5)
pos = find(sigmoid(X*theta) >= 0.5); neg = find(sigmoid(X*theta) < 0.5); % p是m*1维向量 p(pos,1) = 1; p(neg,1) = 0; % 判断准确度 fprintf('Train Accuracy: %f\n', mean(double(p == y)) * 100);
- 绘图
- 绘样本图
% 寻找正负样本 pos = find(y==1); neg = find(y == 0); % 正负样本分别绘制 plot(X(pos, 1), X(pos, 2), 'k+','LineWidth', 2, 'MarkerSize', 7); plot(X(neg, 1), X(neg, 2), 'ko', 'MarkerFaceColor', 'y','MarkerSize', 7); hold on; xlabel('Exam 1 score') ylabel('Exam 2 score') legend('Admitted', 'Not admitted') hold off;
- 绘边界图
plotData(X(:,2:3), y); % 绘制散点 hold on if size(X, 2) <= 3 % 如果特征数小于等于3,即边界面为直线,只需要两点确定一直线 plot_x = [min(X(:,2))-2, max(X(:,2))+2]; % 按特征x(2),选择最大、最小两点 plot_y = (-1./theta(3)).*(theta(2).*plot_x + theta(1)); % 根据theta(1)+theta(2)x(1)+theta(3)x(2)=0,计算x(3) plot(plot_x, plot_y) % 绘制曲线 legend('Admitted', 'Not admitted', 'Decision Boundary') axis([30, 100, 30, 100]) else % 如果特征数大于3,即边界面为曲线 u = linspace(-1, 1.5, 50); v = linspace(-1, 1.5, 50); z = zeros(length(u), length(v)); for i = 1:length(u) for j = 1:length(v) z(i,j) = mapFeature(u(i), v(j))*theta; end end z = z'; contour(u, v, z, [0, 0], 'LineWidth', 2) % z=0 刚好是分界面 end hold off
- 绘样本图