六、regularized logisitic regssion练习(转载)
转载链接:http://www.cnblogs.com/tornadomeet/archive/2013/03/17/2964858.html
在上一讲Deep learning:五(regularized线性回归练习)中已经介绍了regularization项在线性回归问题中的应用,这节主要是练习regularization项在logistic回归中的应用,并使用牛顿法来求解模型的参数。参考的网页资料为:http://openclassroom.stanford.edu/MainFolder/DocumentPage.php?course=DeepLearning&doc=exercises/ex5/ex5.html。要解决的问题是,给出了具有2个特征的一堆训练数据集,从该数据的分布可以看出它们并不是非常线性可分的,因此很有必要用更高阶的特征来模拟。例如本程序中个就用到了特征值的6次方来求解。
实验基础:
contour:
该函数是绘制轮廓线的,比如程序中的contour(u, v, z, [0, 0], 'LineWidth', 2),指的是在二维平面U-V中绘制曲面z的轮廓,z的值为0,轮廓线宽为2。注意此时的z对应的范围应该与U和V所表达的范围相同。因为contour函数是用来等高线,而本实验中只需画一条等高线,所以第4个参数里面的值都是一样的,这里为[0,0],0指的是函数值z在0和0之间的等高线(很明显,只能是一条)。
在logistic回归中,其表达式为:

在此问题中,将特征x映射到一个28维的空间中,其x向量映射后为:

此时加入了规则项后的系统的损失函数为:

对应的牛顿法参数更新方程为:

其中:

公式中的一些宏观说明(直接截的原网页):

实验结果:
原训练数据点的分布情况:

当lambda=0时所求得的分界曲面:

当lambda=1时所求得的分界曲面:

当lambda=10时所求得的分界曲面:

实验程序代码:
%载入数据
clc,clear,close all;
x = load('ex5Logx.dat');
y = load('ex5Logy.dat'); %画出数据的分布图
plot(x(find(y),),x(find(y),),'o','MarkerFaceColor','b')
hold on;
plot(x(find(y==),),x(find(y==),),'r+')
legend('y=1','y=0') % Add polynomial features to x by
% calling the feature mapping function
% provided in separate m-file
x = map_feature(x(:,), x(:,)); %map_feature函数是什么? [m, n] = size(x); % Initialize fitting parameters
theta = zeros(n, ); % Define the sigmoid function
g = inline('1.0 ./ (1.0 + exp(-z))'); % setup for Newton's method
MAX_ITR = ;
J = zeros(MAX_ITR, ); % Lambda is the regularization parameter
lambda = ;%lambda=,,,修改这个地方,运行3次可以得到3种结果。 % Newton's Method
for i = :MAX_ITR
% Calculate the hypothesis function
z = x * theta;
h = g(z); % Calculate J (for testing convergence)
J(i) =(/m)*sum(-y.*log(h) - (-y).*log(-h))+ ...
(lambda/(*m))*norm(theta([:end]))^; % Calculate gradient and hessian.
G = (lambda/m).*theta; G() = ; % extra term for gradient
L = (lambda/m).*eye(n); L() = ;% extra term for Hessian
grad = ((/m).*x' * (h-y)) + G;
H = ((/m).*x' * diag(h) * diag(1-h) * x) + L; % Here is the actual update
theta = theta - H\grad; end
% Show J to determine if algorithm has converged
J
% display the norm of our parameters
norm_theta = norm(theta) % Plot the results
% We will evaluate theta*x over a
% grid of features and plot the contour
% where theta*x equals zero % Here is the grid range
u = linspace(-, 1.5, );
v = linspace(-, 1.5, ); z = zeros(length(u), length(v));
% Evaluate z = theta*x over the grid
for i = :length(u)
for j = :length(v)
z(j,i) = map_feature(u(i), v(j))*theta;%这里绘制的并不是损失函数与迭代次数之间的曲线,而是线性变换后的值
end
end
z = z'; % important to transpose z before calling contour % Plot z =
% Notice you need to specify the range [, ]
contour(u, v, z, [, ], 'LineWidth', )%在z上画出为0值时的界面,因为为0时刚好概率为0.,符合要求
legend('y = 1', 'y = 0', 'Decision boundary')
title(sprintf('\\lambda = %g', lambda), 'FontSize', ) hold off % Uncomment to plot J
% figure
% plot(:MAX_ITR-, J, 'o--', 'MarkerFaceColor', 'r', 'MarkerSize', )
% xlabel('Iteration'); ylabel('J')
疑问: 上文中的map_feature是什么函数?
六、regularized logisitic regssion练习(转载)的更多相关文章
- 转载 Deep learning:六(regularized logistic回归练习)
前言: 在上一讲Deep learning:五(regularized线性回归练习)中已经介绍了regularization项在线性回归问题中的应用,这节主要是练习regularization项在lo ...
- 四、Logisitic Regssion练习(转载)
转载:http://www.cnblogs.com/tornadomeet/archive/2013/03/16/2963919.html 牛顿法:http://blog.csdn.net/xp215 ...
- 五、regularized线性回归练习(转载)
转载链接:http://www.cnblogs.com/tornadomeet/archive/2013/03/17/2964515.html 前言: 本节主要是练习regularization项的使 ...
- 【windows核心编程】 第六章 线程基础
Windows核心编程 第六章 线程基础 欢迎转载 转载请注明出处:http://www.cnblogs.com/cuish/p/3145214.html 1. 线程的组成 ① 一个是线程的内核 ...
- 从ReadImage到ML- 一个不错的博客
实在对不起原作者,为了不把文章淹没在 转载的海洋里.... 原文链接: http://www.cnblogs.com/tornadomeet/archive/2012/09/26/270404 ...
- [UFLDL] Basic Concept
博客内容取材于:http://www.cnblogs.com/tornadomeet/archive/2012/06/24/2560261.html 参考资料: UFLDL wiki UFLDL St ...
- [UFLDL] Linear Regression & Classification
博客内容取材于:http://www.cnblogs.com/tornadomeet/archive/2012/06/24/2560261.html Deep learning:六(regulariz ...
- libcurl 设置代理,通过Fiddler可以进行抓包
转载:https://blog.csdn.net/jaryguo/article/details/53021923 转载:https://www.cnblogs.com/miantest/p/7289 ...
- sql server编写脚本求解第1天1分钱之后每天两倍持续一个月的等比数列问题
一.问题 问题1 场景:如果你未来的丈母娘要求你,第1天给她1分钱,第2天给2分钱,第3天给4分钱,以此类推,每天给前一天的2倍,给1个月(按30天)算就行.问:第30天给多少钱,总共给多少钱? 问题 ...
随机推荐
- [poj3046][Ant counting数蚂蚁]
题目链接 http://noi.openjudge.cn/ch0206/9289/ 描述 Bessie was poking around the ant hill one day watching ...
- os模块(二十)
os模块是与操作系统交互的一个接口 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录:相 ...
- JasperReport 中踩过的坑
Mac Book Pro 10.13.6Jaspersoft Studio community version 6.6.9JDK 8 安装 Jaspersoft Studio Jasper Rep ...
- iptables限制同一IP连接数,防防CC/DDOS攻击
启动sftp本机的iptables防火墙功能,限制每个ip连接22端口(sftp连接端口即是ssh端口)最大为50个,当超过50后的连接数的流量就会被DROP掉! 同时iptables需要开放5000 ...
- ElasticSearch 例子
ElasticSearch是一个接近实时的搜索平台,它利用Lucese进行文档索引. 本文会写个可以运行的简单例子,方便大家上手,日后深入了解. 需要引入maven依赖 <dependency& ...
- JavaEE学习总结(十四)— 人工智能微博
一.数据库与表 人工智能微博(blog) note(id,bt,nr);微博信息(编号,标题,内容) 列表添加 数据库脚本 /* Navicat MySQL Data Transfer Source ...
- 设计模式---对象创建模式之抽象工厂模式(Abstract Factory)
一:概念 抽象工厂模式是所有形态的工厂模式中最为抽象和最具一般性的.抽象工厂模式可以向客户端提供一个接口,使得客户端在不必指定产品的具体类型的情况下,能够创建多个产品族的产品对象 二:动机 在软件系统 ...
- Cannot send, channel has already failed: tcp://127.0.0.1:8161
解决方案一 我觉得你可能需要把服务完全停掉了 然后重启一下. http://localhost:8161(管理端口) tcp://127.0.0.1:61616(服务端口)
- HDU - 3564 Another LIS(LIS+线段树)
http://acm.hdu.edu.cn/showproblem.php?pid=3564 题意 给出1~n的插入顺序,要求每次插入之后的LIS 分析 首先用线段树还原出最终序列.因为插入的顺序是按 ...
- bzoj千题计划300:bzoj4823: [Cqoi2017]老C的方块
http://www.lydsy.com/JudgeOnline/problem.php?id=4823 讨厌的形状就是四联通图 且左右各连一个方块 那么破坏所有满足条件的四联通就好了 按上图方式染色 ...