这是一个分类学习问题,已知80名学生两次考试的成绩和他们是否被大学录取。要求预测学生能否被大学录取。解决方法是用批梯度上升的方法求使得极大似然函数最大的theta。使用MATLAB编程,代码如下:
%ex4 %x score y goto college %batch theta=[0;0;0]; counter=1000; J=zeros(counter,1) alpha=0.0001; for num=1:counter for j_num=1:m J(num)=J(num)+(1/(1+exp(-1*x(j_num,:)*theta))-y(j_num))^2 end J(num)=J(num)/m sum=0; for i=1:m sum=sum+(y(i,1)-1/(1+exp(-x(i,:)*theta)))*x(i,:)' end theta=theta+alpha*(1/m)*sum end figure plot(1:counter,J,'-') %result:theta==[-0.0044;0.0386;0.0190]
编程中遇到的主要问题是,J(theta)的表达式写错了,还是按线性方程写的,导致调参数的时候得出了诡异结论。