逻辑回归--Octave实现

本文探讨了如何使用Octave进行逻辑回归和广义逻辑回归。在逻辑回归问题中,通过历史数据训练分类模型,预测大学申请者录取的概率。在广义逻辑回归部分,针对微芯片质量检测问题,解释了欠拟合和过拟合的概念,并展示了如何通过正则化避免过拟合。文章包括成本函数和梯度的计算,以及使用fminunc()函数寻找最优解。最后,提供了sigmoid函数、costFunctionReg函数的单元测试结果和相关资源链接。
摘要由CSDN通过智能技术生成

The logistic regression cost function is convex, so gradient descent will always find the global minimum.

问题一:采用逻辑回归

Suppose that you are the administrator of a university department and you want to determine each applicant's chance of admission based on their results on two exams. You have historical data from previous applicants that you can use as a training set for logistic regression. For each training example, you have the applicant's scores on two exams and the admissions decision.
Your task is to build a classi cation model that estimates an applicant's probability of admission based the scores from those two exams. This outline and the framework code in ex2.m will guide you through the exercise.

1. 最主要的步骤是计算cost fucnction和 gradient

function [J, grad] = costFunction(theta, X, y)
% 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));

Jtmp=0;
h= zeros(m,1);

%step1:compute hx
hx = X*theta;

%step2:compute h(hx)
h = sigmoid(hx);

%step3:compute cost function's sum part
for i=1:m,
    Jtmp=Jtmp+(-y(i)*log(h(i))-(1-y(i))*log(1-h(i)));
end;
J=(1/m)*Jtmp;

%step4:compute gradient's sum part    
sum1 =zeros(size(X,2),1);%#features row
for i=1:m
    sum1 = sum1+(h(i)-y(i)).*X(i,:)';
end;
    
grad= (1/m)*sum1;

2. 其中调用的sigmoid(S型函数)如下:

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

% You need to return the following variables correctly 
g = zeros(size(z));
for i =1:size(z,1)
    for j =1:size(z,2)
        g(i,j)=1/(1+e^(-z(i,j)));
    end;
end;

3.调用octave的内建函数fminunc();来获得最优的theta和最小的cost。

<

评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值