Exercise: Logistic Regression and Newton's Method
Exercise: Logistic Regression and Newton's Method
题目地址:
Exercise: Logistic Regression
题目概要:某个高中有80名学生,其中40名得到了大学的录取,40名没有被录取。x中包含80名学生两门标准考试的成绩,y中包含学生是否被录取(1代表录取、0代表未录取)。
过程:
1、加载试验数据,并为x输入添加一个偏置项。
x=load('ex4x.dat'); y=load('ex4y.dat'); x=[ones(length(y),1) x];
2、绘制数据分布
% find returns the indices of the % rows meeting the specified condition pos = find(y == 1); neg = find(y == 0);% Assume the features are in the 2nd and 3rd % columns of x plot(x(pos, 2), x(pos,3), '+'); hold on plot(x(neg, 2), x(neg, 3), 'o')
3、Newton's method
先回忆一下logistic regression的假设:
因为matlab中没有sigmoid函数,因此我们用niline定义一个:
g = inline('1.0 ./ (1.0 + exp(-z))'); % Usage: To find the value of the sigmoid % evaluated at 2, call g(2)
再来看看我们定义的cost function J(θ):
我们希望使用Newton's method来求出cost function J(θ)的最小值。回忆一下Newton's method中θ的迭代规则为:
在logistic regression中,梯度和Hessian的求法分别为:
需要注意的是,上述公式中的写法为向量形式。
其中,为n+1 * 1的向量,为n+1 * n+1的矩阵。
和为标量。
实现
按照上述Newton's method所述的方法逐步实现。代码如下
function [theta, J ] = newton( x,y ) %NEWTON Summary of this function goes here % Detailed explanation goes here m = length(y); theta = zeros(3,1); g = inline('1.0 ./ (1.0 + exp(-z))'); pos = find(y == 1); neg = find(y == 0); J = zeros(10, 1); for num_iterations = 1:10%计算实际输出h_theta_x = g(x * theta);%将y=0和y=1的情况分开计算后再相加,计算J函数pos_J_theta = -1 * log(h_theta_x(pos));neg_J_theta = -1 *log((1- h_theta_x(neg)));J(num_iterations) = sum([pos_J_theta;neg_J_theta])/m;%计算J导数及Hessian矩阵delta_J = sum(repmat((h_theta_x - y),1,3).*x);H = x'*(repmat((h_theta_x.*(1-h_theta_x)),1,3).*x);%更新θtheta = theta - inv(H)*delta_J'; end % now plot J % technically, the first J starts at the zero-eth iteration % but Matlab/Octave doesn't have a zero index figure; plot(0:9, J(1:10), '-') xlabel('Number of iterations') ylabel('Cost J') end
PS:在练习的附录答案中给出的直接计算J函数的代码很优雅:
J(i) =(1/m)*sum(-y.*log(h) - (1-y).*log(1-h));
在matlab中调用:
[theta J] = newton(x,y);
可以得到输出:
θ为:
J函数输出图像为(令迭代次数为10的情况下):
可以看到,其实在迭代至第四次时,就已经收敛了。
我们可以输出一下admitted和unadmitted的分界线,代码如下:
plot(x(pos, 2), x(pos,3), '+'); hold on plot(x(neg, 2), x(neg, 3), 'o') ylabel('exam 2 scores') xlabel('exam 1 scores') plot(x(:,2), (theta(1) - x(:,2)*theta(2))/theta(3), '-'); legend('admitted', 'unadmitted','decision boundary');
效果如图:
Exercise: Logistic Regression and Newton's Method相关推荐
- Logistic regression Newton’s method
perceptron learning algorithm: Newton's method: Hessian: When Newton's method is applied to maximize ...
- 如何通过牛顿方法解决Logistic回归问题 By 机器之心2017年8月09日 16:30 本文介绍了牛顿方法(Newton's Method),以及如何用它来解决 logistic 回归。log
如何通过牛顿方法解决Logistic回归问题 By 机器之心2017年8月09日 16:30 本文介绍了牛顿方法(Newton's Method),以及如何用它来解决 logistic 回归.logi ...
- Programming Exercise 2: Logistic Regression
题目要求及材料下载链接:Coursera下载地址 没用的信息略过,有用的翻译一下. 本练习中包含的文件: ex2.m - Octave/MATLAB脚本 ex2_reg.m - Octave/MATL ...
- UFLDL教程:Exercise:Softmax Regression
Softmax分类函数的Python实现 Deep Learning and Unsupervised Feature Learning Tutorial Solutions 逻辑回归假设函数 在线性 ...
- python pandas库实现逻辑回归拟牛顿法求参数_python 牛顿法实现逻辑回归(Logistic Regression)...
本文采用的训练方法是牛顿法(Newton Method). 代码 import numpy as np class LogisticRegression(object): ""&q ...
- 牛顿法python代码_python 牛顿法实现逻辑回归(Logistic Regression)
本文采用的训练方法是牛顿法(Newton Method). 代码 import numpy as np class LogisticRegression(object): ""&q ...
- 逻辑回归模型(Logistic Regression)及Python实现
逻辑回归模型(Logistic Regression)及Python实现 http://www.cnblogs.com/sumai 1.模型 在分类问题中,比如判断邮件是否为垃圾邮件,判断肿瘤是否为阳 ...
- 吴恩达机器学习ex2 Logistic Regression (python)
Programming Exercise 2: Logistic Regression Machine Learning 目录 Introduction 1 Logistic regression 1 ...
- 基于逻辑回归(Logistic Regression)的糖尿病视网膜病变(Diabetic Retinopathy)检测
基于逻辑回归的糖尿病视网膜病变检测 说明 数据集 探索性数据分析 方法 结果 代码 说明 这是我学机器学习的一个项目, 基于逻辑回归(Logistic Regression)的糖尿病视网膜病变(Dia ...
最新文章
- 一起谈.NET技术,VS2010测试功能之旅:编码的UI测试(3)-操作对象的识别原理...
- java基础-static
- hashMap与hashTable区别
- require与include+php,PHP中include与require有什么区别
- 机器学习文献中的英文(part1)
- FPGA中计数器设计探索
- 逐渐摆脱高通和联发科!华为手机将采用更多麒麟芯片
- python修改文件名_【Python沙龙】批量修改文件名称
- 如何在python中安装包_如何给python安装包
- 造成服务器不稳定的原因有哪些
- 四级英语图表作文真题计算机,英语四级作文真题
- 天才黑客 Flanker 疑因拒绝做黑客攻击业务,被拼多多强行辞退,错失上亿股票...
- (二十七)张量表示定理 —— Cauchy 基本表示定理
- 喜报丨酷雷曼成功挂牌北京股权交易中心科技创新板
- 轻松获取Excel安装路径
- mysql 伪表查询语句_MySql系列05:MySql中DQL查询操作
- px em pt单位区别
- 多多吐槽,心情会变好
- JavaScript 有关获取当前/上个月/12个月前/七/7天/一周之前/下周一的日期格式 持续更新...
- 论文投稿指南——收藏|如何选择SCI期刊(含选刊必备神器)
热门文章
- java 中while编译之后_从APK反编译的Java-while循环什么也不做
- html5 游戏前景怎么样,独家 HTML5游戏目前究竟怎么样?看完这篇文章,你或许会清晰很多...
- 滚动条禁止_Axure 教程:不可见滚动条的页面滚动效果
- mysql的单行注释_MySQL基础--会这些就够了
- 84. Leetcode 70. 爬楼梯 (动态规划-基础题)
- Leetcode 213. 打家劫舍 II (每日一题 20211009)
- Leetcode 剑指 Offer 53 - I. 在排序数组中查找数字 I (每日一题 20210928)
- 258. Add Digits
- MCMC笔记:MCMC的不足
- 【linux】11_存储管理文件链接