吴恩达机器学习第二道编程题logistic逻辑回归

主程序logistic.m:

A=load('tumor_size.txt');%肿瘤尺寸
B=load('tumor_type.txt');%肿瘤性质,0良性1恶性
C=[ones(length(B),1),A];
%画出数据集
pos=find(B==1);neg=find(B==0);
plot(A(pos,1),A(pos,2),'k+','Linewidth',2,'MarkerSize',7);hold on;
plot(A(neg,1),A(neg,2),'ro','MarkerFaceColor','y','MarkerSize',7);
m=length(B);%样本个数
theta=ones(3,1);%假设函数参数初始化
alfa=0.01 ;%设置学习率
Jvec=zeros(1000,1);%用来存放收敛过程中的代价函数值,用于判断学习率是否合适 
h_theta=cal_h(theta,C,m);
Jvec(1,1)=log_cal_j(h_theta,B);%计算第一个代价函数值
theta(1,1)=theta(1,1)-alfa/m*sum((h_theta-B).*C(:,1));%更新theta
theta(2,1)=theta(2,1)-alfa/m*sum((h_theta-B).*C(:,2));
theta(3,1)=theta(3,1)-alfa/m*sum((h_theta-B).*C(:,3));
h_theta=cal_h(theta,C,m);
Jvec(2,1)=log_cal_j(h_theta,B);%计算第二个代价函数值
ii=2;
while Jvec(ii-1,1)-Jvec(ii,1)>=0.0000001%设置参数更新的终止条件:如果先后两个代价
    ii=ii+1;                            %函数值的减小幅度小于0.0000001,则终止
    theta(1,1)=theta(1,1)-alfa/m*sum((h_theta-B).*C(:,1));
    theta(2,1)=theta(2,1)-alfa/m*sum((h_theta-B).*C(:,2));
    theta(3,1)=theta(3,1)-alfa/m*sum((h_theta-B).*C(:,3));
    h_theta=cal_h(theta,C,m);
    Jvec(ii,1)=log_cal_j(h_theta,B);
end
hold on;
x=[1:10];
y=(ones(length(x),1)*theta(1,1)+theta(2,1)*x')/(-theta(3,1));
plot(x,y','y');%画出决策边界
figure;
plot(Jvec);%axis([0 10 0 60000]);

计算假设函数值cal_h.m

function h=cal_h(theta,X,m)%计算假设函数值
h=zeros(m,1);
for i=1:m
z=theta'*X(i,:)';
h(i,1)=1/(1+exp(-z));
end
end

计算代价函数log_cal_j.m

function j=log_cal_j(h_theta,y)%计算代价函数值
m=length(y);
h1=log(h_theta);h2=log(ones(m,1)-h_theta);
j=sum(y.*h1+(ones(m,1)-y).*h2)/(-m);
end

数据集tumor_size.txt

4 3
2 6
7 8
5 4
9 12
6 5
3 10
2 7
4 5
5 6

还有tumor_type.txt

0
0
1
0
1
1
1
0
0
1

运行结果:
数据集和决策边界:
在这里插入图片描述
代价函数:
在这里插入图片描述

欢迎学习吴恩达机器学习的朋友一起交流进步!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Programming Exercise 1: Linear Regression Machine Learning Introduction In this exercise, you will implement linear regression and get to see it work on data. Before starting on this programming exercise, we strongly recom- mend watching the video lectures and completing the review questions for the associated topics. To get started with the exercise, you will need to download the starter code and unzip its contents to the directory where you wish to complete the exercise. If needed, use the cd command in Octave/MATLAB to change to this directory before starting this exercise. You can also find instructions for installing Octave/MATLAB in the “En- vironment Setup Instructions” of the course website. Files included in this exercise ex1.m - Octave/MATLAB script that steps you through the exercise ex1 multi.m - Octave/MATLAB script for the later parts of the exercise ex1data1.txt - Dataset for linear regression with one variable ex1data2.txt - Dataset for linear regression with multiple variables submit.m - Submission script that sends your solutions to our servers [?] warmUpExercise.m - Simple example function in Octave/MATLAB [?] plotData.m - Function to display the dataset [?] computeCost.m - Function to compute the cost of linear regression [?] gradientDescent.m - Function to run gradient descent [†] computeCostMulti.m - Cost function for multiple variables [†] gradientDescentMulti.m - Gradient descent for multiple variables [†] featureNormalize.m - Function to normalize features [†] normalEqn.m - Function to compute the normal equations ? indicates files you will need to complete † indicates optional exercises
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值