matlab(3) Logistic Regression: 求cost 和gradient \ 求sigmoid的值

sigmoid.m文件

function g = sigmoid(z)
%SIGMOID Compute sigmoid functoon
% J = SIGMOID(z) computes the sigmoid of z.

g = zeros(size(z));  初始化g ,z可以是一个数,一个向量或者一个矩阵

% ====================== YOUR CODE HERE ======================
% Instructions: Compute the sigmoid of each value of z (z can be a matrix, vector or scalar)
Ones = ones(size(z));
g = Ones./(Ones + exp((-1).*z));  计算,g(z)的值域在[0,1]之间,符合概率的分布.

                                                 当z=0时,g=0.5; 当z<0时,g<0.5;当z>0时,g>0.5; 

                                                 当z->-∞时,g->0; 当z->+∞时,g->1

                                                 z可以是一个数,一个向量或者是一个矩阵

% =============================================================

end

 

costFunction.m

function [J, grad] = costFunction(theta, X, y)
%COSTFUNCTION Compute cost and gradient for logistic regression
% J = COSTFUNCTION(theta, X, y) computes the cost of using theta as the
% parameter for logistic regression and the gradient of the cost
% w.r.t. to the parameters.

% Initialize some useful values
m = length(y); % number of training examples

% You need to return the following variables correctly
J = 0;
grad = zeros(size(theta));  %grad的维数与theta的一至

% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta.
% You should set J to the cost.
% Compute the partial derivatives and set grad to the partial
% derivatives of the cost w.r.t. each parameter in theta
%
% Note: grad should have the same dimensions as theta
%

                                                                                                           J(θ)的表达式      

                                                                                                              grad的表达式

J = 1/m*(-1*y'*log(sigmoid(X*theta)) - (ones(1,m)-y')*log(ones(m,1)-sigmoid(X*theta)));    %logM是对矩阵的每个元素都是求log, exp(M)同样是表示对矩阵的每                                                                                                                                        个元素求e的底

                                                                                                                                      调用的函数参见上述函数sigmoid.m

grad = 1/m * (X' * (sigmoid(X*theta) - y));,

% =============================================================

end

 

 

%% ============ Part 2: Compute Cost and Gradient ============
% In this part of the exercise, you will implement the cost and gradient
% for logistic regression. You neeed to complete the code in
% costFunction.m

% Setup the data matrix appropriately, and add ones for the intercept term
[m, n] = size(X);  %求x矩阵的维数

% Add intercept term to x and X_test
X = [ones(m, 1) X];  %X矩阵左侧加一列1,用来匹配常数量

% Initialize fitting parameters
initial_theta = zeros(n + 1, 1);  

% Compute and display initial cost and gradient
[cost, grad] = costFunction(initial_theta, X, y);   %参见上述文件costFunction.m

fprintf('Cost at initial theta (zeros): %f\n', cost);
fprintf('Gradient at initial theta (zeros): \n');
fprintf(' %f \n', grad);

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

转载于:https://www.cnblogs.com/yan2015/p/4826635.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值