Logistic Regression and Newton's Method
Data
For this exercise, suppose that a high school has a dataset representing 40 students who were admitted to college and 40 students who were not admitted. Each training example contains a student's score on two standardized exams and a label of whether the student was admitted.
Your task is to build a binary classification model that estimates college admission chances based on a student's scores on two exams. In your training data,
a. The first column of your x array represents all Test 1 scores, and the second column represents all Test 2 scores.
b. The y vector uses '1' to label a student who was admitted and '0' to label a student who was not admitted.
Plot the data
Load the data for the training examples into your program and add the
intercept term into your x matrix.
Before beginning Newton's Method, we will first plot the data using different symbols to represent the two classes. In Matlab/Octave, you can separate the positive class and the negative class using the find command:
% 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')Your plot should look like the following:
Newton's Method
在logistic regression问题中,logistic函数表达式如下:
这样做的好处是可以把输出结果压缩到0~1之间。而在logistic回归问题中的损失函数与线性回归中的损失函数不同,这里定义的为:
如果采用牛顿法来求解回归方程中的参数,则参数的迭代公式为:
其中一阶导函数和hessian矩阵表达式如下:
code
% Exercise -- Logistic Regression clear all; close all; clc x = load('ex4x.dat');
y = load('ex4y.dat'); [m, n] = size(x); % Add intercept term to x
x = [ones(m, ), x]; % Plot the training data
% Use different markers for positives and negatives
figure
pos = find(y); neg = find(y == );%find是找到的一个向量,其结果是find函数括号值为真时的值的编号
plot(x(pos, ), x(pos,), '+')
hold on
plot(x(neg, ), x(neg, ), 'o')
hold on
xlabel('Exam 1 score')
ylabel('Exam 2 score') % Initialize fitting parameters
theta = zeros(n+, ); % Define the sigmoid function 匿名函数
g = inline('1.0 ./ (1.0 + exp(-z))'); % Newton's method
MAX_ITR = ;
J = zeros(MAX_ITR, ); for i = :MAX_ITR
% Calculate the hypothesis function
z = x * theta;
h = g(z);%转换成logistic函数 % Calculate gradient and hessian.
% The formulas below are equivalent to the summation formulas
% given in the lecture videos.
grad = (/m).*x' * (h-y);%梯度的矢量表示法
H = (/m).*x' * diag(h) * diag(1-h) * x;%hessian矩阵的矢量表示法 % Calculate J (for testing convergence)
J(i) =(/m)*sum(-y.*log(h) - (-y).*log(-h));%损失函数的矢量表示法 theta = theta - H\grad;%是这样子的吗?
end
% Display theta
theta % Calculate the probability that a student with
% Score on exam and score on exam
% will not be admitted
prob = - g([, , ]*theta) %画出分界面
% Plot Newton's method result
% Only need points to define a line, so choose two endpoints
plot_x = [min(x(:,))-, max(x(:,))+];
% Calculate the decision boundary line,plot_y的计算公式见博客下面的评论。
plot_y = (-./theta()).*(theta().*plot_x +theta());
plot(plot_x, plot_y)
legend('Admitted', 'Not admitted', 'Decision Boundary')
hold off % Plot J
figure
plot(:MAX_ITR-, J, 'o--', 'MarkerFaceColor', 'r', 'MarkerSize', )
xlabel('Iteration'); ylabel('J')
% Display J
J
matlab
diag函数功能:矩阵对角元素的提取和创建对角阵
设以下X为方阵,v为向量
1、X = diag(v,k)当v是一个含有n个元素的向量时,返回一个n+abs(k)阶方阵X,向量v在矩阵X中的第k个对角线上,k=0表示主对角线,k>0表示在主对角线上方,k<0表示在主对角线下方。例1:
v=[1 2 3];
diag(v, 3)ans =
0 0 0 1 0 0
0 0 0 0 2 00 0 0 0 0 3
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
注:从主对角矩阵上方的第三个位置开始按对角线方向产生数据的
例2:
v=[1 2 3];
diag(v, -1)ans =
0 0 0 0
1 0 0 0
0 2 0 0
0 0 3 0
注:从主对角矩阵下方的第一个位置开始按对角线方向产生数据的
2、X = diag(v)
向量v在方阵X的主对角线上,类似于diag(v,k),k=0的情况。
例3:
v=[1 2 3];
diag(v)ans =
1 0 0
0 2 00 0 3
注:写成了对角矩阵的形式
3、v = diag(X,k)
返回列向量v,v由矩阵X的第k个对角线上的元素形成
例4:
v=[1 0 3;2 3 1;4 5 3];
diag(v,1)ans =
0
1注:把主对角线上方的第一个数据作为起始数据,按对角线顺序取出写成列向量形式
4、v = diag(X)返回矩阵X的主对角线上的元素,类似于diag(X,k),k=0的情况例5:
v=[1 0 0;0 3 0;0 0 3];
diag(v)ans =
1
33
或改为:
v=[1 0 3;2 3 1;4 5 3];
diag(v)ans =
1
33
注:把主对角线的数据取出写成列向量形式
5、diag(diag(X))
取出X矩阵的对角元,然后构建一个以X对角元为对角的对角矩阵。
例6:X=[1 2;3 4]
diag(diag(X))X =
1 2
3 4ans =
1 0
0 4
Logistic Regression and Newton's Method的更多相关文章
- 转载 Deep learning:四(logistic regression练习)
前言: 本节来练习下logistic regression相关内容,参考的资料为网页:http://openclassroom.stanford.edu/MainFolder/DocumentPage ...
- 斯坦福CS229机器学习课程笔记 part2:分类和逻辑回归 Classificatiion and logistic regression
Logistic Regression 逻辑回归 1.模型 逻辑回归解决的是分类问题,并且是二元分类问题(binary classification),y只有0,1两个取值.对于分类问题使用线性回归不 ...
- Regularized logistic regression
要解决的问题是,给出了具有2个特征的一堆训练数据集,从该数据的分布可以看出它们并不是非常线性可分的,因此很有必要用更高阶的特征来模拟.例如本程序中个就用到了特征值的6次方来求解. Data To be ...
- 逻辑回归模型(Logistic Regression)及Python实现
逻辑回归模型(Logistic Regression)及Python实现 http://www.cnblogs.com/sumai 1.模型 在分类问题中,比如判断邮件是否为垃圾邮件,判断肿瘤是否为阳 ...
- Logistic Regression Vs Decision Trees Vs SVM: Part I
Classification is one of the major problems that we solve while working on standard business problem ...
- Stanford机器学习---第三讲. 逻辑回归和过拟合问题的解决 logistic Regression & Regularization
原文:http://blog.csdn.net/abcjennifer/article/details/7716281 本栏目(Machine learning)包括单参数的线性回归.多参数的线性回归 ...
- logistic regression的一些问题,不平衡数据,时间序列,求解惑
Logistic Regression 1.在有时间序列的特征数据中,怎么运用LR? 不光是LR,其他的模型也是. 有很多基本的模型变形之后,变成带时序的模型.但,个人觉得,这类模型大多不靠谱. 我觉 ...
- Machine Learning - 第3周(Logistic Regression、Regularization)
Logistic regression is a method for classifying data into discrete outcomes. For example, we might u ...
- Python实践之(七)逻辑回归(Logistic Regression)
机器学习算法与Python实践之(七)逻辑回归(Logistic Regression) zouxy09@qq.com http://blog.csdn.net/zouxy09 机器学习算法与Pyth ...
随机推荐
- 链表python
无序链表.有序链表 有序列表排序通常是升序或降序,并且我们假设列表项具有已经定义的有意义的比较运算. 许多有序列表操作与无序列表的操作相同. 必须明确链表的第一项位置,一旦知道第一项. 链表实现的基本 ...
- 京东在2018年成为Intel全球最大PC零售渠道
京东宣布,根据Intel公布的数据,京东在2018年成为Intel全球最大的PC零售渠道. 近日,京东.Intel高层进行了战略会晤,在总结回顾2018年合作成果的同时,就2019年进一步深度战略合作 ...
- PHP -Casbin: 支持 ACL、RBAC、ABAC 多种模型的 PHP 权限管理框架
PHP-Casbin 是一个用 PHP 语言打造的轻量级开源访问控制框架( https://github.com/php-casbin... ),目前在 GitHub 开源.PHP-Casbin 采用 ...
- C#开发奇技淫巧二:根据dll文件加载C++或者Delphi插件
原文:C#开发奇技淫巧二:根据dll文件加载C++或者Delphi插件 这两天忙着把框架改为支持加载C++和Delphi的插件,来不及更新blog了. 原来的写的框架只支持c#插件,这个好做 ...
- 【Codeforces Round #239 (Div. 1) B】 Long Path
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] DP,设f[i]表示第一次到i这个房间的时候传送的次数. f[1] = 0,f[2] = 2 考虑第i个位置的情况. 它肯定是从i- ...
- 洛谷 P1952 火星上的加法运算_NOI导刊2009提高(3)
P1952 火星上的加法运算_NOI导刊2009提高(3) 题目描述 最近欢欢看到一本有关火星的书籍,其中她被一个加法运算所困惑,由于她的运算水平有限.她想向你求助,作为一位优秀的程序员,你当然不会拒 ...
- 洛谷 P2083 找人
P2083 找人 题目背景 无 题目描述 小明要到他的同学家玩,可他只知道他住在某一单元,却不知住在哪个房间.那个单元有N层(1,2……N),每层有M(1,2……M)个房间. 小明会从第一层的某个房间 ...
- 对GPDB查询计划的Motion结点的理解
GPDB在进行join查询时,可能会产生Motion结点 根据官方文档,总共有这几种Motion: redistribute 重分布(用hash取模的方法把join字段重分布到各个segment,相当 ...
- hdu 4707 Pet【BFS求树的深度】
Pet Time Limit: 4000/2000 MS (Java/Others) ...
- hdu(1069)——Monkey and Banana(LIS变形)
题意: 如今给你n个石块,然后它由坐标来表示(x,y,z).可是它能够有不同的方法,也就是说它的三个坐标能够轮换着来的. 石块的数量不限,可是每次都必须保持上底面的长和宽严格递减,然后问你用这些石块所 ...
intercept term into your x matrix.



