ML 逻辑回归 Logistic Regression
逻辑回归
Logistic Regression
1 分类 Classification
首先我们来看看使用线性回归来解决分类会出现的问题。下图中,我们加入了一个训练集,产生的新的假设函数使得我们进行分类出现了错误;而且线性回归计算的结果往往会远小于0或者远大于1,这对于0,1分类变得很奇怪。可见线性回归并不适用与分类。下面介绍的逻辑回归的结果总是在[0,1],适用于分类,其实逻辑回归是一种分类算法。
2 假设函数Hypothesis Representation
逻辑回归假设函数为:
其中 是参数向量,
特征向量。该函数叫做Logistic function(也叫做Sigmoid function)。函数图像如下,范围(0,1)
对于分类问题,它可以表示给定特征,参数
属于类别1的概率,那么属于另一类的概率自然也就是
。
3 决策边界Decision Boundary
根据逻辑函数的特性可以看出,当,当
我们把 就称为决策边界(Decision Boundary)。
我们可以通过多项式组合来制定更加复杂的决策边界。
4 代价函数 Cost Function
代价函数 ,在线性回归中,我们的
,如果在逻辑回归中也使用这个函数,那么代价函数会是一个非凸函数,无法使用梯度下降去求解参数,所以我们要寻找一些函数使得代价函数为凸函数。
我们来看一下这个代价函数:
我们可以将它合并为一个公式:
5 梯度下降 Gradient Desecent
这里我们把代价函数写成这种形式是由最大似然估计得到了,当然也还有其他的形式。
梯度下降算法:
可以看到这里推导出来的公式看起来和线性回归梯度下降中推导出来的公式是一样的,但是要注意已经是sigmod函数而不是线性公式了,所以他们是两码事。
6 多分类 Multi-class classification: one-vs-all
我们可以降逻辑回归用于多分类问题,假设有K类,我们可以训练出K个分类器,每个分类器,将其中一种类别作为正类,其余的都是负类来训练,然后再预测时,类别属于概率最大的那个类。
7 正则化regularization
7.1 过拟合 overfitting
7.2 代价函数cost function
在代价函数中加入正则项,注意,这里对计算,而不是从开始。其中
是正则项参数,如果
太大,那么
会趋向于0,使得
,导致欠拟合。
7.3 正则化线性回归 Regularized linear regression
代价函数:
Gradeint descent
Repeat{
}
这里是一个比1小一点点的数。
在线性回归中,我们除了梯度下降,还有正规方程的方法,正规方法加入正则项后:
7.4 正则化逻辑回归Regularized logistic regression
代价函数:
Gradient descent
Repeat{
}
实验代码
正则化逻辑回归
- function [J, grad] = costFunctionReg(theta, X, y, lambda)
- %COSTFUNCTIONREG Compute cost and gradient for logistic regression with regularization
- % J = COSTFUNCTIONREG(theta, X, y, lambda) computes the cost of using
- % theta as the parameter for regularized logistic regression and the
- % gradient of the cost w.r.t. to the parameters.
- % Initialize some useful values
- m = length(y); % number of training examples
- % You need to return the following variables correctly
- J = ;
- grad = zeros(size(theta));
- % ====================== YOUR CODE HERE ======================
- % Instructions: Compute the cost of a particular choice of theta.
- % You should set J to the cost.
- % Compute the partial derivatives and set grad to the partial
- % derivatives of the cost w.r.t. each parameter in theta
- hx=sigmoid(X*theta);
- Jnorm=(-/m)*(y'*log(hx)+(1-y)'*log(-hx));
- theta0=theta(); %注意theta0不用正则化
- theta1=theta(:end);
- Jreg=(lambda/(*m))*sum(theta1.^);
- J=Jnorm+Jreg;
- grad0=(hx-y)'*X(:,1)./m;
- grad1=((hx-y)'*X(:,2:end)./m)'+(lambda/m).*theta1;
- grad=[grad0;grad1];
- % =============================================================
- end
sigmoid函数
- 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));
- % ====================== YOUR CODE HERE ======================
- % Instructions: Compute the sigmoid of each value of z (z can be a matrix,
- % vector or scalar).
- g=./(+exp((-).*z));
- % =============================================================
- end
特征
- function out = mapFeature(X1, X2)
- % MAPFEATURE Feature mapping function to polynomial features
- %
- % MAPFEATURE(X1, X2) maps the two input features
- % to quadratic features used in the regularization exercise.
- %
- % Returns a new feature array with more features, comprising of
- % X1, X2, X1.^, X2.^, X1*X2, X1*X2.^, etc..
- %
- % Inputs X1, X2 must be the same size
- %
- degree = ;%6次函数
- out = ones(size(X1(:,)));
- for i = :degree
- for j = :i
- out(:, end+) = (X1.^(i-j)).*(X2.^j);
- end
- end
- end
主函数
- %% Machine Learning Online Class - Exercise : Logistic Regression
- %
- % Instructions
- % ------------
- %
- % This file contains code that helps you get started on the second part
- % of the exercise which covers regularization with logistic regression.
- %
- % You will need to complete the following functions in this exericse:
- %
- % sigmoid.m
- % costFunction.m
- % predict.m
- % costFunctionReg.m
- %
- % For this exercise, you will not need to change any code in this file,
- % or any other files other than those mentioned above.
- %
- %% Initialization
- clear ; close all; clc
- %% Load Data
- % The first two columns contains the X values and the third column
- % contains the label (y).
- data = load('ex2data2.txt');
- X = data(:, [, ]); y = data(:, );
- plotData(X, y);
- % Put some labels
- hold on;
- % Labels and Legend
- xlabel('Microchip Test 1')
- ylabel('Microchip Test 2')
- % Specified in plot order
- legend('y = 1', 'y = 0')
- hold off;
- %% =========== Part : Regularized Logistic Regression ============
- % In this part, you are given a dataset with data points that are not
- % linearly separable. However, you would still like to use logistic
- % regression to classify the data points.
- %
- % To do so, you introduce more features to use -- in particular, you add
- % polynomial features to our data matrix (similar to polynomial
- % regression).
- %
- % Add Polynomial Features
- % Note that mapFeature also adds a column of ones for us, so the intercept
- % term is handled
- X = mapFeature(X(:,), X(:,));
- % Initialize fitting parameters
- initial_theta = zeros(size(X, ), );
- % Set regularization parameter lambda to
- lambda = ;
- % Compute and display initial cost and gradient for regularized logistic
- % regression
- [cost, grad] = costFunctionReg(initial_theta, X, y, lambda);
- fprintf('Cost at initial theta (zeros): %f\n', cost);
- fprintf('\nProgram paused. Press enter to continue.\n');
- pause;
- %% ============= Part : Regularization and Accuracies =============
- % Optional Exercise:
- % In this part, you will get to try different values of lambda and
- % see how regularization affects the decision coundart
- %
- % Try the following values of lambda (, , , ).
- %
- % How does the decision boundary change when you vary lambda? How does
- % the training set accuracy vary?
- %
- % Initialize fitting parameters
- initial_theta = zeros(size(X, ), );
- % Set regularization parameter lambda to (you should vary this)
- lambda = ;
- % Set Options
- options = optimset('GradObj', 'on', 'MaxIter', );
- % Optimize
- [theta, J, exit_flag] = ...
- fminunc(@(t)(costFunctionReg(t, X, y, lambda)), initial_theta, options);
- % Plot Boundary
- plotDecisionBoundary(theta, X, y);
- hold on;
- title(sprintf('lambda = %g', lambda))
- % Labels and Legend
- xlabel('Microchip Test 1')
- ylabel('Microchip Test 2')
- legend('y = 1', 'y = 0', 'Decision boundary')
- hold off;
- % Compute accuracy on our training set
- p = predict(theta, X);
- fprintf('Train Accuracy: %f\n', mean(double(p == y)) * );
实验中参数lamda的大小也十分重要,不同的lamda可能会过拟合或者欠拟合。
ML 逻辑回归 Logistic Regression的更多相关文章
- Coursera公开课笔记: 斯坦福大学机器学习第六课“逻辑回归(Logistic Regression)” 清晰讲解logistic-good!!!!!!
原文:http://52opencourse.com/125/coursera%E5%85%AC%E5%BC%80%E8%AF%BE%E7%AC%94%E8%AE%B0-%E6%96%AF%E5%9D ...
- 机器学习总结之逻辑回归Logistic Regression
机器学习总结之逻辑回归Logistic Regression 逻辑回归logistic regression,虽然名字是回归,但是实际上它是处理分类问题的算法.简单的说回归问题和分类问题如下: 回归问 ...
- 机器学习(四)--------逻辑回归(Logistic Regression)
逻辑回归(Logistic Regression) 线性回归用来预测,逻辑回归用来分类. 线性回归是拟合函数,逻辑回归是预测函数 逻辑回归就是分类. 分类问题用线性方程是不行的 线性方程拟合的是连 ...
- 机器学习入门11 - 逻辑回归 (Logistic Regression)
原文链接:https://developers.google.com/machine-learning/crash-course/logistic-regression/ 逻辑回归会生成一个介于 0 ...
- 机器学习方法(五):逻辑回归Logistic Regression,Softmax Regression
欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld. 技术交流QQ群:433250724,欢迎对算法.技术.应用感兴趣的同学加入. 前面介绍过线性回归的基本知识, ...
- 机器学习 (三) 逻辑回归 Logistic Regression
文章内容均来自斯坦福大学的Andrew Ng教授讲解的Machine Learning课程,本文是针对该课程的个人学习笔记,如有疏漏,请以原课程所讲述内容为准.感谢博主Rachel Zhang 的个人 ...
- 逻辑回归(Logistic Regression)详解,公式推导及代码实现
逻辑回归(Logistic Regression) 什么是逻辑回归: 逻辑回归(Logistic Regression)是一种基于概率的模式识别算法,虽然名字中带"回归",但实际上 ...
- 逻辑回归 Logistic Regression
逻辑回归(Logistic Regression)是广义线性回归的一种.逻辑回归是用来做分类任务的常用算法.分类任务的目标是找一个函数,把观测值匹配到相关的类和标签上.比如一个人有没有病,又因为噪声的 ...
- 【机器学习】Octave 实现逻辑回归 Logistic Regression
ex2data1.txt ex2data2.txt 本次算法的背景是,假如你是一个大学的管理者,你需要根据学生之前的成绩(两门科目)来预测该学生是否能进入该大学. 根据题意,我们不难分辨出这是一种二分 ...
随机推荐
- Linq与扩展方法
使用数据集 /// <summary> /// 库房信息类 /// </summary> public class Kfxx { /// <summary> /// ...
- Linux 并发服务器雏形总结
如下介绍一个并发回射客户端/服务器的雏形,所谓回射:就是客户端输入一条数据,服务器端读取并显示,然后服务器端再把刚读取的信息发送回客户端进行显示.示意图如下: 所谓并发服务器:就是一个服务器可以同时为 ...
- 【BZOJ1937】[Shoi2004]Mst 最小生成树 KM算法(线性规划)
[BZOJ1937][Shoi2004]Mst 最小生成树 Description Input 第一行为N.M,其中 表示顶点的数目, 表示边的数目.顶点的编号为1.2.3.…….N-1.N.接下来的 ...
- PHP 获得域控内用户的计算机登录名
一个需求: 在域控范围获得访问用户的计算机名.方法: 1.测试软件环境: XAMPP Control Panel V3.2.1 , Apache version 2.4.7 2.Apache 2.2 ...
- 小程序 less wxss 混合 Mixins picker样式优化 箭头样式的实现原理
lessc src/style/picker-arrow_.less src/style/picker-arrow_.wxss 快速入门 | Less.js 中文文档 https://less.boo ...
- VSpy之C Code Interface的使用
Spy3 要运行 CCodeInterface 功能,需要安装运行环境,建议安装 Visual Studio2003,2005,2008,2010 或更新的版本.当然也可以安装 VC express ...
- Servlet 运行原理
一:servlet定义 Servlet是一个Java应用程序,运行在服务器端,用来处理客户端请求并作出响应的程序. 二:简单servlet实例 //导入所需的包 import javax.servle ...
- mongodb文档支持的数据类型
版权声明:转载请标明来源. https://blog.csdn.net/u014285882/article/details/25510377 1. 存储类型 mongodb文档相似于json,但不是 ...
- 剑指offer 面试62题
面试62题: 题目:圆圈中最后剩下的数字 题:0,1,...,n-1这n个数字排成一个圆圈,从数字0开始,每次从这个圆圈里删除第m个数字.求出这个圆圈里剩下的最后一个数字. 解题思路:约瑟夫环问题,可 ...
- try catch 事务不会滚
在spring机制中,在配置事务后,如果采用try catch 捕获异常后,因为异常已经被捕获,所以事务不会滚,从而产生许多脏数据.解决办法: 1.在catch中抛出异常,(throw new Run ...