Machine Learning---Logistic回归

本章节主要讲解Logistic回归的原理及其数学推导,Logistic有3种不同的表达形式,现在我就一一展开这几种不同的形式,以及它在分类中的效果。并比较这三种形式。

下面分别写出这三种形式的损失函数:

下面分别写出这三种损失函数的梯度形式:

其中第一种形式和第三种形式是等价的,推导如下:

Steepest descent

前面章节已经讲过最速下降法的更新公式,如下:

下面将给出代码这样容易理解:

main.m

<span style="font-family:Times New Roman;">[D,b] = load_data();
%%% run exp and log convex logistic regression %%%
x0 = randn(3,1);    % initial point
alpha = 10^-2;        % step length
x = grad_descent_exp_logistic(D,b,x0,alpha);
% Run log convex logistic regression
alpha = 10^-1;        % step length
y = grad_descent_log_logistic(D,b,x0,alpha);
%%% plot everything, pts and lines %%%
plot_all(D',b,x,y);</span>

load_data().m

<span style="font-family:Times New Roman;"> function [A,b] = load_data()
        data = load('exp_vs_log_data.mat');
        data = data.data;
        A = data(:,1:3);
        A = A';
        b = data(:,4);
    end</span>

grad_descent_exp_logistic.m

<span style="font-family:Times New Roman;">function x = grad_descent_exp_logistic(D,b,x0,alpha)
        % Initializations
        x = x0;
        iter = 1;
        max_its = 3000;
        grad = 1;
        m=22;
        while  norm(grad) > 10^-6 && iter < max_its

            % compute gradient
              sum=0;
            for i=1:22
                z=b(i)*(D(:,i)'*x);
                tmp1=exp(-z);
                tmp2=-b(i)*D(:,i)';
                sum=sum+tmp1*tmp2';
            end
            grad=(1/22)*sum;         % your code goes here!
            x = x - alpha*grad;

            % update iteration count
            iter = iter + 1;
        end
    end</span>

grad_descent_log_logistic.m

<span style="font-family:Times New Roman;">function x = grad_descent_log_logistic(D,b,x0,alpha)
        % Initializations
        x = x0;
        iter = 1;
        max_its = 3000;
        grad = 1;
        m=22;
        while  norm(grad) > 10^-6 && iter < max_its
            sum=0;
            for i=1:22
                z=b(i)*(D(:,i)'*x);
                tmp1=exp(-z)/sigmoid(z);
                tmp2=-b(i)*D(:,i)';
                sum=sum+tmp1*tmp2';
            end
            grad=(1/22)*sum;
            x = x - alpha*grad;
            % update iteration count
            iter = iter + 1;
        end
    end</span>

plot_all.m

<span style="font-family:Times New Roman;">function plot_all(A,b,x,y)

        % plot points
        ind = find(b == 1);
        scatter(A(ind,2),A(ind,3),'Linewidth',2,'Markeredgecolor','b','markerFacecolor','none');
        hold on
        ind = find(b == -1);
        scatter(A(ind,2),A(ind,3),'Linewidth',2,'Markeredgecolor','r','markerFacecolor','none');
        hold on

        % plot separators
        s =[min(A(:,2)):.01:max(A(:,2))];
        plot (s,(-x(1)-x(2)*s)/x(3),'m','linewidth',2);
        hold on

        plot (s,(-y(1)-y(2)*s)/y(3),'k','linewidth',2);
        hold on

        set(gcf,'color','w');
        axis([ (min(A(:,2)) - 0.1) (max(A(:,2)) + 0.1) (min(A(:,3)) - 0.1) (max(A(:,3)) + 0.1)])
        box off

        % graph info labels
        xlabel('a_1','Fontsize',14)
        ylabel('a_2  ','Fontsize',14)
        set(get(gca,'YLabel'),'Rotation',0)

    end</span>

结果图

其中黑线为第二种损失函数,彩色线为第一种损失函数。

资源----------------代码和数据集见资源

中科院大学雁西湖校区

时间: 2024-08-06 11:58:07

Machine Learning---Logistic回归的相关文章

机器学习---逻辑回归(二)(Machine Learning Logistic Regression II)

在<机器学习---逻辑回归(一)(Machine Learning Logistic Regression I)>一文中,我们讨论了如何用逻辑回归解决二分类问题以及逻辑回归算法的本质.现在来看一下多分类的情况. 现实中相对于二分类问题,我们更常遇到的是多分类问题.多分类问题如何求解呢?有两种方式.一种是方式是修改原有模型,另一种方式是将多分类问题拆分成一个个二分类问题解决. 先来看一下第一种方式:修改原有模型.即:把二分类逻辑回归模型变为多分类逻辑回归模型. (二分类逻辑回归称为binary

Machine Learning — 逻辑回归

现实生活中有很多分类问题,比如正常邮件/垃圾邮件,良性肿瘤/恶性肿瘤,识别手写字等等,这些可以用逻辑回归算法来解决. 一.二分类问题 所谓二分类问题,即结果只有两类,Yes or No,这样结果{0,1}集合来表示y的取值范围. 前面说到过,线性回归的模型是 h(x)=θ0+θ1x1+θ2x2+...,这种回归模型的取值是在整个自然数空间的,对于0,1问题,就要想办法把模型取值压缩到0~1之间,这里我们就引入一个sigmoid函数:g(z)=1/(1+e-z) 所以hθ(x)=g(θTx),它代

Machine Learning Algorithms Study Notes(4)—无监督学习(unsupervised learning)

1    Unsupervised Learning 1.1    k-means clustering algorithm 1.1.1    算法思想 1.1.2    k-means的不足之处 1.1.3    如何选择K值 1.1.4    Spark MLlib 实现 k-means 算法 1.2    Mixture of Gaussians and the EM algorithm 1.3    The EM Algorithm 1.4    Principal Components

机器学习经典算法详解及Python实现---Logistic回归(LR)分类器

(一)认识Logistic回归(LR)分类器 首先,Logistic回归虽然名字里带"回归",但是它实际上是一种分类方法,主要用于两分类问题,利用Logistic函数(或称为Sigmoid函数),自变量取值范围为(-INF, INF),自变量的取值范围为(0,1),函数形式为: 由于sigmoid函数的定义域是(-INF, +INF),而值域为(0, 1).因此最基本的LR分类器适合于对两分类(类0,类1)目标进行分类.Sigmoid 函数是个很漂亮的"S"形,如下

Machine Learning Algorithms Study Notes(1)--Introduction

Machine Learning Algorithms Study Notes 高雪松 @雪松Cedro Microsoft MVP 目 录 1    Introduction    1 1.1    What is Machine Learning    1 1.2    学习心得和笔记的框架    1 2    Supervised Learning    3 2.1    Perceptron Learning Algorithm (PLA)    3 2.1.1    PLA -- "知

Logistic回归Cost函数和J(θ)的推导----Andrew Ng【machine learning】公开课

最近翻Peter Harrington的<机器学习实战>,看到Logistic回归那一章有点小的疑问. 作者在简单介绍Logistic回归的原理后,立即给出了梯度上升算法的code:从算法到代码跳跃的幅度有点大,作者本人也说了,这里略去了一个简单的数学推导. 那么其实这个过程在Andrew Ng的机器学习公开课里也有讲到.现在回忆起来,大二看Andrew的视频的时候心里是有这么一个疙瘩(Andrew也是跳过了一步推导) 那么这里就来讲一下作者略去了怎样的数学推导,以及,怎么推导. 在此之前,先

Andrew Ng Machine Learning - Week 3:Logistic Regression &amp; Regularization

此文是斯坦福大学,机器学习界 superstar - Andrew Ng 所开设的 Coursera 课程:Machine Learning 的课程笔记.力求简洁,仅代表本人观点,不足之处希望大家探讨. 课程网址:https://www.coursera.org/learn/machine-learning/home/welcome Week 1: Introduction 笔记:http://blog.csdn.net/ironyoung/article/details/46845233 We

转载 Deep learning:六(regularized logistic回归练习)

前言: 在上一讲Deep learning:五(regularized线性回归练习)中已经介绍了regularization项在线性回归问题中的应用,这节主要是练习regularization项在logistic回归中的应用,并使用牛顿法来求解模型的参数.参考的网页资料为:http://openclassroom.stanford.edu/MainFolder/DocumentPage.php?course=DeepLearning&doc=exercises/ex5/ex5.html.要解决的

Logistic Regression &amp; Regularization ----- Stanford Machine Learning(by Andrew NG)Course Notes

coursera上面Andrew NG的Machine learning课程地址为:https://www.coursera.org/course/ml 我曾经使用Logistic Regression方法进行ctr的预测工作,因为当时主要使用的是成型的工具,对该算法本身并没有什么比较深入的认识,不过可以客观的感受到Logistic Regression的商用价值. Logistic Regression Model A. objective function       其中z的定义域是(-I

CheeseZH: Stanford University: Machine Learning Ex2:Logistic Regression

1. Sigmoid Function In Logisttic Regression, the hypothesis is defined as: where function g is the sigmoid function. The sigmoid function is defined as: 2.Cost function and gradient The cost function in logistic regression is: the gradient of the cos