要解决的问题是,给出了具有2个特征的一堆训练数据集,从该数据的分布可以看出它们并不是非常线性可分的,因此很有必要用更高阶的特征来模拟。例如本程序中个就用到了特征值的6次方来求解。

Data

To begin, load the files 'ex5Logx.dat' and ex5Logy.dat' into your program. This dataset represents the training set of a logistic regression problem with two features. To avoid confusion later, we will refer to the two input features contained in 'ex5Logx.dat' as and . So in the 'ex5Logx.dat' file, the first column of numbers represents the feature , which you will plot on the horizontal axis, and the second feature represents , which you will plot on the vertical axis.

After loading the data, plot the points using different markers to distinguish between the two classifications. The commands in Matlab/Octave will be:

x = load('ex5Logx.dat');
y = load('ex5Logy.dat'); figure % Find the indices for the 2 classes
pos = find(y); neg = find(y == 0); plot(x(pos, 1), x(pos, 2), '+')
hold on
plot(x(neg, 1), x(neg, 2), 'o')

After plotting your image, it should look something like this:

Model

the hypothesis function is

 

Let's look at the parameter in the sigmoid function .

In this exercise, we will assign to be all monomials (meaning polynomial terms) of and up to the sixth power:

To clarify this notation: we have made a 28-feature vector where

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

Newton’s method

Recall that the Newton's Method update rule is

1. is your feature vector, which is a 28x1 vector in this exercise.

2. is a 28x1 vector.

3. and are 28x28 matrices.

4. and are scalars.

5. The matrix following in the Hessian formula is a 28x28 diagonal matrix with a zero in the upper left and ones on every other diagonal entry.

After convergence, use your values of theta to find the decision boundary in the classification problem. The decision boundary is defined as the line where

Code

%载入数据
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(:,)); %投影到高维特征空间 [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 % 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(i,j) = 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')

Result

Regularized logistic regression的更多相关文章

  1. machine learning(15) --Regularization:Regularized logistic regression

    Regularization:Regularized logistic regression without regularization 当features很多时会出现overfitting现象,图 ...

  2. matlab(7) Regularized logistic regression : mapFeature(将feature增多) and costFunctionReg

    Regularized logistic regression : mapFeature(将feature增多) and costFunctionReg ex2_reg.m文件中的部分内容 %% == ...

  3. matlab(6) Regularized logistic regression : plot data(画样本图)

    Regularized logistic regression :  plot data(画样本图) ex2data2.txt 0.051267,0.69956,1-0.092742,0.68494, ...

  4. 编程作业2.2:Regularized Logistic regression

    题目 在本部分的练习中,您将使用正则化的Logistic回归模型来预测一个制造工厂的微芯片是否通过质量保证(QA),在QA过程中,每个芯片都会经过各种测试来保证它可以正常运行.假设你是这个工厂的产品经 ...

  5. matlab(8) Regularized logistic regression : 不同的λ(0,1,10,100)值对regularization的影响,对应不同的decision boundary\ 预测新的值和计算模型的精度predict.m

    不同的λ(0,1,10,100)值对regularization的影响\ 预测新的值和计算模型的精度 %% ============= Part 2: Regularization and Accur ...

  6. 吴恩达机器学习笔记22-正则化逻辑回归模型(Regularized Logistic Regression)

    针对逻辑回归问题,我们在之前的课程已经学习过两种优化算法:我们首先学习了使用梯度下降法来优化代价函数

  7. Stanford机器学习---第三讲. 逻辑回归和过拟合问题的解决 logistic Regression & Regularization

    原文:http://blog.csdn.net/abcjennifer/article/details/7716281 本栏目(Machine learning)包括单参数的线性回归.多参数的线性回归 ...

  8. Machine Learning - 第3周(Logistic Regression、Regularization)

    Logistic regression is a method for classifying data into discrete outcomes. For example, we might u ...

  9. 【机器学习】Octave 实现逻辑回归 Logistic Regression

    ex2data1.txt ex2data2.txt 本次算法的背景是,假如你是一个大学的管理者,你需要根据学生之前的成绩(两门科目)来预测该学生是否能进入该大学. 根据题意,我们不难分辨出这是一种二分 ...

随机推荐

  1. MySQL修改最大连接数,没有my.ini文件,只有my-default,这怎么改呀?

    # For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.6/en/server-co ...

  2. 以下三种下载方式有什么不同?如何用python模拟下载器下载?

    问题始于一个链接https://i1.pixiv.net/img-zip-...这个链接在浏览器打开,会直接下载一个不完整的zip文件 但是,使用下载器下载却是完整文件 而当我尝试使用python下载 ...

  3. vue中Object.defineProperty用法

    function def (obj, key, val, enumerable) { Object.defineProperty(obj, key, { value: val, enumerable: ...

  4. 1、Go base64编码

    package main import ( "encoding/base64" "fmt") func main() { //标准base64编码 data:= ...

  5. python(2) 图像通道,几何变换,裁剪

    一.图像通道 1.彩色图像转灰度图 from PIL import Image import matplotlib.pyplot as plt img=Image.open('d:/ex.jpg') ...

  6. php八大设计模式之适配器模式

    将一个抽象被具体后的结果转换成另外一个需求所需的格式. 在生活中也处处有适配器的出现,比如转换头,就是让两种不同的规格合适的搭配在一起. <?php header("content-t ...

  7. 洛谷3857 [TJOI2008]彩灯

    题目描述 已知一组彩灯是由一排N个独立的灯泡构成的,并且有M个开关控制它们.从数学的角度看,这一排彩灯的任何一个彩灯只有亮与不亮两个状态,所以共有2N个样式.由于技术上的问题,Peter设计的每个开关 ...

  8. linux命令su与su-的差别

    su命令和su -命令最大的本质差别就是: su仅仅是切换了root身份.但Shell环境仍然是普通用户的Shell. 而su -连用户和Shell环境一起切换成root身份了. 仅仅有切换了Shel ...

  9. 改动npm包管理器的registry为淘宝镜像(npm.taobao.org)

    起因 安装了node.安装了npm之后,官方的源实在是 太慢了! 看了看淘宝的npm镜像, http://npm.taobao.org/  居然说让我再下载一个cnpm,要不然就每次都得install ...

  10. rman数据库恢复;关键/非重要文件、影像副本、控制文件、还原点、非归档、增量、新数据库、灾难性回复

    运行全然恢复:在 ARCHIVELOG 模式下 丢失了系统重要数据文件: 假设某个数据文件丢失或损坏.且该文件属于 SYSTEM 或 UNDO 表空间,请运行下面步骤: 1. 实例可能会也可能不会自己 ...