机器学习逻辑回归练习及代码在Octave中实现

1.加载数据生成数据并画出对应的点

cpp数据=加载(‘ex2data1.txt’); X =数据(:,[1,2]); y =数据(:,3); fprintf中([’++绘图数据表示(Y = 1)实现表示(Y = 0)的例子\ n’]); plotData(X,y);例和O’。。坚持; xlabel

(“考试1分数”)ylabel(“考试2分数”)图例(“已录入”,“未录入”)暂缓执行; fprintf(“ \ n程序已暂停。按Enter继续。\ n’);停顿; `

![在这里插入图片描述(https://img-blog.csdnimg.cn/20200716231957476.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG_M_ZF4M2O1C3F1M2M0C1F2M3D0C3F0M0C0F3C1M0C0F0C3F1C4M2D0矩阵以及初始化梯度下降和代价函数

[m, n] = size(X);
X = [ones(m, 1) X]; %生成设计矩阵
initial_theta = zeros(n + 1, 1);  %初始化theta矩阵
% 计算代价函数和梯度下降
[cost, grad] = costFunction(initial_theta, X, y);
% 下面是测试代码以及正确答案
fprintf('Cost at initial theta (zeros): %f\n', cost);
fprintf('Expected cost (approx): 0.693\n');
fprintf('Gradient at initial theta (zeros): \n');
fprintf(' %f \n', grad);
fprintf('Expected gradients (approx):\n -0.1000\n -12.0092\n -11.2628\n');

% Compute and display cost and gradient with non-zero theta
test_theta = [-24; 0.2; 0.2];
[cost, grad] = costFunction(test_theta, X, y);

fprintf('\nCost at test theta: %f\n', cost);
fprintf('Expected cost (approx): 0.218\n');
fprintf('Gradient at test theta: \n');
fprintf(' %f \n', grad);
fprintf('Expected gradients (approx):\n 0.043\n 2.566\n 2.647\n');

fprintf('\nProgram paused. Press enter to continue.\n');
pause;

这里给出costFunction的代码
首先给出映射函数,将数据映射到0-1的函数g(z)
在这里插入图片描述
sigmoid函数

function g = sigmoid(z)
g = zeros(size(z));
g = 1./(1+exp(-z));
end

这里注意exp的用法,之前用e^(-z)不起作用,还是exp()靠谱。还有要1./(),否则只返回一个值。

先给出逻辑回归计算J(θ)的函数和对J(θ)的求导函数
在这里插入图片描述
在这里插入图片描述
只要根据函数一步步实现即可

function [J, grad] = costFunction(theta, X, y)
m = length(y); % 训练样本数
J = 0;
grad = zeros(size(theta)); %返回theta向量结果
value = 0;
% 计算代价函数
for i=1:m
  value = value + y(i)*log(sigmoid(X(i,:)*theta)) + (1-y(i))*log(1-sigmoid(X(i,:)*theta));
endfor
J = (-1)/m*value;
% 对代价函数求导
theta_len = size(theta);
for j=1:theta_len
  value = 0;
  for i=1:m
    value = value + (sigmoid(X(i,:)*theta) - y(i))*X(i,j);
  endfor
  grad(j) = 1/m*value;
endfor
end

这里需要注意的是公式里的hθ(x)和线性回归是不一样的,它实质是g(z),z才是线性回归里的hθ(x),z=X*θ,其中X是设计矩阵。

3.使用优化函数,无需自己计算最优的theta

options = optimset('GradObj', 'on', 'MaxIter', 400);

[theta, cost] = ...
	fminunc(@(t)(costFunction(t, X, y)), initial_theta, options);
fprintf('Cost at theta found by fminunc: %f\n', cost);
fprintf('Expected cost (approx): 0.203\n');
fprintf('theta: \n');
fprintf(' %f \n', theta);
fprintf('Expected theta (approx):\n');
fprintf(' -25.161\n 0.206\n 0.201\n');
% 画出决策边界
plotDecisionBoundary(theta, X, y);
hold on;
xlabel('Exam 1 score')
ylabel('Exam 2 score')
legend('Admitted', 'Not admitted')
hold off;
fprintf('\nProgram paused. Press enter to continue.\n');
pause;

option是选项,其实是一个数据结构,记录了一些参数。
将梯度目标参数设置为开启(on)
迭代次数设置为400
调用优化函数fminunc,参数是代价函数,想起来代价函数返回了代价值J和求导值grad;初始值Initial_theta;选项设置,最后得到最优的theta和代价值。
下面给出画决策边界的代码

function plotDecisionBoundary(theta, X, y)
plotData(X(:,2:3), y);
hold on
if size(X, 2) <= 3
    plot_x = [min(X(:,2))-2,  max(X(:,2))+2];
    plot_y = (-1./theta(3)).*(theta(2).*plot_x + theta(1));
    plot(plot_x, plot_y)
    legend('Admitted', 'Not admitted', 'Decision Boundary')
    axis([30, 100, 30, 100])
else
    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'; % important to transpose z before calling contour
    contour(u, v, z, [0, 0], 'LineWidth', 2)
end
hold off
end

在这里插入图片描述
涉及特征过多的数据时
计算costFunction需要加入一些参数,即正则化

function [J, grad] = costFunctionReg(theta, X, y, lambda)
m = length(y); % number of training examples
J = 0;
grad = zeros(size(theta));
value1 = 0;
value2 = 0;
theta_len = size(theta);
for i=1:m
  value1 = value1 + y(i)*log(sigmoid(X(i,:)*theta))+(1-y(i))*log(1-sigmoid(X(i,:)*theta));
endfor
for j=2:theta_len
  value2 = value2 + theta(j)^2;
endfor
J = (-1)/m*value1 + lambda/(2*m)*value2;
value = 0;
for i=1:m
  value = value + (sigmoid(X(i,:)*theta)-y(i))*X(i,1);
endfor
grad(1) = 1/m*value;

for j=2:theta_len
  value = 0;
  for i=1:m
    value = value +  (乙状结肠(X (我,:)* THETA )- Ý (我))* X (我, Ĵ ); 
  endfor grad ( j )= 1 / m *+ lambda / m * theta ( j ); endfor 结束```
    



这里计算J和对J求导的时候特别需要注意一点,就是针对θj这个,两个函数都是从θ1开始,不要把θ0算进去。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值