逻辑回归模型实例(by Fminunc and Gradient descent) ...


本实例整理自斯坦福机器学习课程课后练习ex2

一、Binary classification

本例中是用机器学习通过已知的输入(两次考试成绩)和输出(是否被录取)来建立一个模型;然后根据输入的成绩来判断一个学生是否能被录取。

1.可视化(visualizing)


data = load('ex2data1.txt');
X = data(:,1:2);
y = data(:,3);

%% step 1.Visualizing
pos = find(y==1);
neg = find(y==0);
plot(X(pos,1),X(pos,2),'k+','LineWidth',2,'MarkerSize',7);
hold on;
plot(X(neg,1),X(neg,2),'ko','MarkerSize',7,'MarkerFaceColor', 'y');
xlabel('Exam 1 score');
ylabel('Exam 2 score');
legend('Admitted', 'Not admitted');
hold off;

这里写图片描述

2.Hypothesis function and Cost function

假设函数的形式如下:

hθ(x)=g(θTx),g(z)=11+ez;hθ(x)=11+eθTx

然后先完成函数 g(z)在matlab中的定义:
%sigmoid.m

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

代价函数的形式如下:

J(θ)=1m[i=1my(i)loghθ(x(i))+(1y(i))log(1hθ(x(i)))]

此时,我们建立一个计算J(θ)的函数,名为costFunction(),其中返回值就是代价值。

% costFunction.m

function J= costFunction( X,y,theta )

m = length(y);

h_theta = sigmoid(X * theta);

J = (1/m)*(sum(-y .* log(h_theta) - ( 1 - y) .* log(1-h_theta)));

end

3.确立优化参数的方法(fit parameter)

在确定好假设函数和代价之后,我们就需要选择一种方法求解我们的参数θ,即Minimize J(θ);使得在这种情况下,函数J(θ)取得最小值(minimum)或者局部最小值(local minimum).

目前我们知道的方法有梯度下降(Gradient descent)算法和进阶优化算法(Advanced algorithm)。现在分别就进阶优化算和梯度下降算法来求解。


Advanced Optimization:

我们将使用Octave/Matlab中内置的一个函数fminunc来进行求解。此时我们将不需要手动来设置α的值,只需写出cost function 以及gradient即可,所以我们在costFunction.m中加上对每个参数的求导计算公式。

function [J,grad] = costFunction( X,y,theta )
m = length(y);
grad = zeros(size(theta));
h_theta = sigmoid(X * theta);

J = (1/m)*(sum(-y .* log(h_theta) - ( 1 - y) .* log(1-h_theta)));

for i = 1:size(theta)
grad(i) = (1/m) * sum((h_theta - y ) .* X(:,i));
end

end

下面是利用fminuc进行求解:

% Set options for fminunc
options = optimset('GradObj', 'on', 'MaxIter', 400);

% Run fminunc to obtain the optimal theta
% This function will return theta and the cost
[theta, cost] = ...
fminunc(@(t)(costFunction(t, X, y)), initial theta, options);

下面是关于fminuc的使用介绍:

In this code snippet, we first defined the options to be used with fminunc.
Specifically, we set the GradObj option to on, which tells fminunc that our
function returns both the cost and the gradient. This allows fminunc to
use the gradient when minimizing the function. Furthermore, we set the
MaxIter option to 400, so that fminunc will run for at most 400 steps before
it terminates.

To specify the actual function we are minimizing, we use a “short-hand”
for specifying functions with the @(t) ( costFunction(X, y,t) ) . This
creates a function, with argument t, which calls your costFunction. This
allows us to wrap the costFunction for use with fminunc.

If you have completed the costFunction correctly, fminunc will converge on the right optimization parameters and return the final values of the cost and θ. Notice that by using fminunc, you did not have to write any loops yourself, or set a learning rate like you did for gradient descent. This is all done by fminunc: you only needed to provide a function calculating the cost and the gradient.

%% 以下是计算结果

cost J = 0.203506
theta -24.932770
theta 0.204406
theta 0.199616

画出Decision boundary

plotDecisionBoundary(theta,X,y);

这里写图片描述

预测:

chance = sigmoid([1,63,63]*theta);
fprintf('chance = %f\n',chance);

%% 预测结果
chance = 0.627294

Matlab源码下载



梯度下降算法:

θj:=θjα1mi=1m(hθ(x(i))y(i))x(i)j

此时,我们建立一个梯度下降的函数,名为gradientDescen(),其中theta为最终得到的参数值,J_history为每次梯度下降后的代价值。
%gradientDescent.m

function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)

m = length(y); % number of training examples
J_history = zeros(num_iters, 1);
temp_matrix = zeros(size(X,2),1);

for iter = 1:num_iters
     prediction = sigmoid(X*theta) - y; 
    for i = 1:size(X,2);
        temp_matrix(i) = sum(prediction .* X(:,i));
    end
    theta = theta - alpha * (1/m) * temp_matrix;

    % Save the cost J in every iteration    
    J_history(iter) = costFunction(X, y, theta);
    %fprintf('j=%f\n',J_history);

end
end
% test.m 

data = load('ex2data1.txt');
X = data(:,1:2);
y = data(:,3);

%% step 1.Visualizing
plotData(X,y);
xlabel('Exam 1 score');
ylabel('Exam 2 score');
legend('Admitted', 'Not admitted');
%% step 2. Hypothesis and Cost function
 % sigmoid.m  costFunction.m
 m = length(y);
 X = [ones(m,1),X];
theta =-5*ones(size(X,2),1)+1E-10;
%theta =zeros(size(X,2),1)-1E-6;

 %% step 3. Fit parameter
 [theta, J_history] = gradientDescent(X, y, theta, 0.0013, 100)
 figure;
 plot(J_history,'-r','LineWidth',2);
 axis([87 100 0.385 0.42]);
 hold on;
figure;
plotDecisionBoundary(theta,X,y);

这里写图片描述

我们可以看到,代价值接近0.38,但与之前的0.2相比还是略微的偏大。所以自然而然其最终的拟合程度肯定不如之前的高,而事实也是如此。

这里写图片描述

Matlab源码下载

转载于:https://www.cnblogs.com/tolic/p/7142208.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于三个特征的逻辑回归预测模型 Matlab 代码的示例: ```Matlab % 加载数据 data = load('data.txt'); X = data(:, [1, 2, 3]); % 特征 y = data(:, 4); % 标签 % 绘制数据散点图 figure; hold on; 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); xlabel('Feature 1'); ylabel('Feature 2'); legend('Positive', 'Negative'); hold off; % 初始化参数 [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); % 绘制决策边界 plotDecisionBoundary(theta, X, y); xlabel('Feature 1'); ylabel('Feature 2'); legend('Positive', 'Negative', 'Decision Boundary'); % 预测新数据 prob = sigmoid([1, 2, 3] * theta); fprintf('For features [1, 2, 3], probability of positive class: %f\n', prob); % 定义代价函数和梯度函数 function [J, grad] = costFunction(theta, X, y) m = length(y); J = (-1/m) * sum(y .* log(sigmoid(X * theta)) + (1-y) .* log(1 - sigmoid(X * theta))); grad = (1/m) * (X' * (sigmoid(X * theta) - y)); end % 定义 sigmoid 函数 function g = sigmoid(z) g = 1 ./ (1 + exp(-z)); end % 绘制决策边界函数 function plotDecisionBoundary(theta, X, y) plotData(X(:,2:3), y); 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) 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'; contour(u, v, z, [0, 0], 'LineWidth', 2) end end % 特征映射函数 function out = mapFeature(X1, X2) 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 ``` 在这个示例中,我们加载了一个包含三个特征和一个二元标签的数据集,并使用逻辑回归算法训练模型。我们使用 fminunc 函数最小化代价函数,该函数在每次迭代中计算梯度并更新模型参数。在训练模型后,我们使用绘制决策边界的函数显示模型的预测结果。最后,我们使用训练好的模型对新的特征值进行预测。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值