Logistic Regression 学习

本文介绍了一种实现逻辑回归的梯度上升法,并提供了详细的MATLAB代码示例。该方法通过迭代更新参数来最大化对数似然函数,适用于二分类问题。文章还包含了测试代码及数据文件的获取方式。

记录使用,参考ng代码,相关参考:http://blog.csdn.net/yangliuy/article/details/18504921

logistic_grad_ascent.m代码

% logistic_grad_ascent.m
function [theta,ll] = logistic_grad_ascent(X,y,alpha,max_iters)
% rows of X are training samples
% rows of Y are corresponding 0/1 values

% output ll: vector of log-likelihood values at each iteration
% ouptut theta: parameters

[m,n] = size(X);

X = [ones(size(X,1),1), X]; % append col of ones for intercept term

theta = zeros(n+1, 1);  % initialize theta
for k = 1:max_iters
  hx = sigmoid(X*theta);
  theta = theta + alpha * X' * (y-hx); 
  ll(k) = sum( y .* log(hx) + (1 - y) .* log(1 - hx) );
end
end

function a = sigmoid(z)
a = 1.0 ./ (1.0 + exp(-z));
end

logistic_grad_ascent_test.m

function logistic_grad_ascent_test

load q1y.dat
load q1x.dat
alpha = 0.0001;
max_iters = 500;
[th,ll] = logistic_grad_ascent(q1x,q1y,alpha,max_iters);
plot(ll)

end

q1x.dat:http://cs229.stanford.edu/materials/q1x.dat

q1y.dat:http://cs229.stanford.edu/materials/q1y.dat

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值