模式识别与机器学习(4)

http://www.cnblogs.com/tornadomeet/archive/2013/03/16/2963919.html


%%http://blog.csdn.net/qunxingvip/article/details/51531044
%%讲了推理,以及主要收获为,原来是对损失函数求导。不过公式不是很对,因为
clear
x=load('./ex4Data/ex4x.dat');
y=load('./ex4Data/ex4y.dat');
[m,n]=size(x);
x=[ones(m,1),x];
figure
pos=find(y);neg=find(y==0);
plot(x(pos,2),x(pos,3),'+');
hold on
plot(x(neg,2),x(neg,3),'o');
hold on
xlabel('Exam 1 score');
ylabel('Exam 2 score');
%上面是画数据点,下面代码是logistics回归
theta=zeros(n+1,1);  %回归系数
%直接令logistic回归的值为0.5,则可以得到e的指数为0,即:
%theta(1)*1+theta(2)*plot_x+theta(3)*plot_y=0,解出plot_y即可。
%g=inline('1.0./(1.0+exp(-z))');



%MAX_ITR=1500;%迭代次数
%alpha=0.01; %学习率
% for num_iterations=1:MAX_ITR   %懂了上标是什么意思的,原来是指维度,即对于一个变量来说指取了
%     grad=(1/(m))*x'*(g(x*theta)-y);%第几个实例,一般来说,一张图像是一个变量,图像的维度表示数据
%     theta=theta-alpha*grad;     %换句话说,譬如这里的年龄是个变量,取了几个值,代表着维度。
%    %矩阵相乘就是求和了
% end                            

MAX_ITR = 7;
J = zeros(MAX_ITR, 1);



for i = 1:MAX_ITR
    % Calculate the hypothesis function
    z = x * theta;
    %h = g(z);%转换成logistic函数
    h=1.0./(1.0+exp(-z));
    grad = (1/m).*x' * (h-y);%梯度的矢量表示法   G(i) = sum(dif.* x(:, i), 1)/m;  注意这是点乘。运算是对应的
    H = (1/m).*x' * diag(h) * diag(1-h) * x;%hessian矩阵的矢量表示法
    J(i) =(1/m)*sum(-y.*log(h) - (1-y).*log(1-h));%损失函数的矢量表示法
    
    for ii=1:(n+1)
        for jj=1:(n+1)
            H1(ii,jj) = sum((h.*(ones(m,1)-h)).* x(:, ii).*x(:,jj), 1)/m;%多个负号,按理是要负号的啊
        end
    end  %这个挺好,这样对应公式还是挺快的 。
    
    theta = theta - inv(H1)*grad;
end


%画图
plot_x = [min(x(:,2))-2,  max(x(:,2))+2];
plot_y = (-1./theta(3)).*(theta(2).*plot_x +theta(1));
plot(plot_x, plot_y)
legend('Admitted', 'Not admitted', 'Decision Boundary')
hold off 













另一个.m

%%http://blog.csdn.net/qunxingvip/article/details/51531044
%%讲了推理,以及主要收获为,原来是对损失函数求导。不过公式不是很对,因为
clear
x=load('./ex4Data/ex4x.dat');
y=load('./ex4Data/ex4y.dat');
[m,n]=size(x);
x=[ones(m,1),x];
figure
pos=find(y);neg=find(y==0);
plot(x(pos,2),x(pos,3),'+');
hold on
plot(x(neg,2),x(neg,3),'o');
hold on
xlabel('Exam 1 score');
ylabel('Exam 2 score');
%上面是画数据点,下面代码是logistics回归
theta=zeros(n+1,1);  %回归系数
%直接令logistic回归的值为0.5,则可以得到e的指数为0,即:
%theta(1)*1+theta(2)*plot_x+theta(3)*plot_y=0,解出plot_y即可。
%g=inline('1.0./(1.0+exp(-z))');
    z=x*theta;
    h=1.0./(1.0+exp(-z));
%MAX_ITR=1500;%迭代次数
%alpha=0.00001; %学习率
 alpha=[0.130082343771923,5.00882567970842,8.78832968558437;
     5.00882567970842,201.254613548375,335.714823876853;
     8.78832968558437,335.714823876853,601.801497555077]; %说明整体是对的,但是学习率的变化影响了。
for num_iterations=1:1000   
%     z=x*theta;
%     h=1.0./(1.0+exp(-z));
   
    grad=(1/(m))*x'*(h-y);
        for ii=1:(n+1)
            for jj=1:(n+1)
                H1(ii,jj) = sum((h.*(ones(m,1)-h)).* x(:, ii).*x(:,jj), 1)/m;%多个负号,按理是要负号的啊
            end
        end  %这个挺好,这样对应公式还是挺快的 。
    
    %theta=theta-H1\grad;   
    theta=theta-alpha\grad;
   %矩阵相乘就是求和了
end                            



%画图
plot_x = [min(x(:,2))-2,  max(x(:,2))+2];
plot_y = (-1./theta(3)).*(theta(2).*plot_x +theta(1));
plot(plot_x, plot_y)
legend('Admitted', 'Not admitted', 'Decision Boundary')
hold off 






猜你喜欢

转载自blog.csdn.net/snailyww/article/details/52317737