Regularized logistic regression

要解决的问题是,给出了具有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),1),x(find(y),2),‘o‘,‘MarkerFaceColor‘,‘b‘)
hold on;
plot(x(find(y==0),1),x(find(y==0),2),‘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(:,1), x(:,2));  %投影到高维特征空间

[m, n] = size(x);

% Initialize fitting parameters
theta = zeros(n, 1);

% Define the sigmoid function
g = inline(‘1.0 ./ (1.0 + exp(-z))‘); 

% setup for Newton‘s method
MAX_ITR = 15;
J = zeros(MAX_ITR, 1);

% Lambda is the regularization parameter
lambda = 1;%lambda=0,1,10,修改这个地方,运行3次可以得到3种结果。

% Newton‘s Method
for i = 1:MAX_ITR
    % Calculate the hypothesis function
    z = x * theta;
    h = g(z);

    % Calculate J (for testing convergence) -- 损失函数
    J(i) =(1/m)*sum(-y.*log(h) - (1-y).*log(1-h))+ ...
    (lambda/(2*m))*norm(theta([2:end]))^2;

    % Calculate gradient and hessian.
    G = (lambda/m).*theta; G(1) = 0; % extra term for gradient
    L = (lambda/m).*eye(n); L(1) = 0;% extra term for Hessian
    grad = ((1/m).*x‘ * (h-y)) + G;
    H = ((1/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, 1.5, 200);
v = linspace(-1, 1.5, 200);

z = zeros(length(u), length(v));
% Evaluate z = theta*x over the grid
for i = 1:length(u)
    for j = 1: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 = 0
% Notice you need to specify the range [0, 0]
contour(u, v, z, [0, 0], ‘LineWidth‘, 2)%在z上画出为0值时的界面,因为为0时刚好概率为0.5,符合要求
legend(‘y = 1‘, ‘y = 0‘, ‘Decision boundary‘)
title(sprintf(‘\\lambda = %g‘, lambda), ‘FontSize‘, 14)

hold off

% Uncomment to plot J
% figure
% plot(0:MAX_ITR-1, J, ‘o--‘, ‘MarkerFaceColor‘, ‘r‘, ‘MarkerSize‘, 8)
% xlabel(‘Iteration‘); ylabel(‘J‘)

Result

时间: 2024-10-21 07:38:52

Regularized logistic regression的相关文章

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

不同的λ(0,1,10,100)值对regularization的影响\ 预测新的值和计算模型的精度 %% ============= Part 2: Regularization and Accuracies =============% Optional Exercise:% In this part, you will get to try different values of lambda and % see how regularization affects the decisio

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

原文地址:http://blog.csdn.net/abcjennifer/article/details/7716281 本栏目(Machine learning)包括单参数的线性回归.多参数的线性回归.Octave Tutorial.Logistic Regression.Regularization.神经网络.机器学习系统设计.SVM(Support Vector Machines 支持向量机).聚类.降维.异常检测.大规模机器学习等章节.所有内容均来自Standford公开课machin

Logistic Regression & Regularization ----- Stanford Machine Learning(by Andrew NG)Course Notes

coursera上面Andrew NG的Machine learning课程地址为:https://www.coursera.org/course/ml 我曾经使用Logistic Regression方法进行ctr的预测工作,因为当时主要使用的是成型的工具,对该算法本身并没有什么比较深入的认识,不过可以客观的感受到Logistic Regression的商用价值. Logistic Regression Model A. objective function       其中z的定义域是(-I

CheeseZH: Stanford University: Machine Learning Ex2:Logistic Regression

1. Sigmoid Function In Logisttic Regression, the hypothesis is defined as: where function g is the sigmoid function. The sigmoid function is defined as: 2.Cost function and gradient The cost function in logistic regression is: the gradient of the cos

斯坦福大学机器学习公开课:Programming Exercise 2: Logistic Regression

斯坦福大学机器学习公开课:Programming Exercise 2: Logistic Regression---Matlab实现 1 Logistic Regression In this part of the exercise, I will build a logistic regression model to predict whether a student gets admitted into a university. You want to determine each

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

34.62365962451697,78.0246928153624,0 30.28671076822607,43.89499752400101,0 35.84740876993872,72.90219802708364,0 60.18259938620976,86.30855209546826,1 79.0327360507101,75.3443764369103,1 45.08327747668339,56.3163717815305,0 61.10666453684766,96.51142

使用Logistic Regression Algorithm进行多分类数字识别的Octave仿真

所需解决的问题是,训练一个Logistic Regression系统,使之能够识别手写体数字1-10,每张图片为20px*20px的灰度图.训练样例的输入X是5000行400列的一个矩阵,每一行存储一张图片(20^2=400),共5000个训练样例,而y则为手写体所表示的数字1-10. 利用Logistic Regression进行多分类应用,其基础是将问题本身化解为z个二分类问题,其中z为类别的个数.第一步,将向量m*1维y扩展为矩阵m*z维矩阵Y,向量n+1维向量theta扩展为矩阵z*(n

Spark MLlib Logistic Regression逻辑回归算法

1.1 逻辑回归算法 1.1.1 基础理论 logistic回归本质上是线性回归,只是在特征到结果的映射中加入了一层函数映射,即先把特征线性求和,然后使用函数g(z)将最为假设函数来预测.g(z)可以将连续值映射到0和1上. 它与线性回归的不同点在于:为了将线性回归输出的很大范围的数,例如从负无穷到正无穷,压缩到0和1之间,这样的输出值表达为"可能性"才能说服广大民众.当然了,把大值压缩到这个范围还有个很好的好处,就是可以消除特别冒尖的变量的影响. Logistic函数(或称为Sigm

CheeseZH: Stanford University: Machine Learning Ex3: Multiclass Logistic Regression and Neural Network Prediction

Handwritten digits recognition (0-9) Multi-class Logistic Regression 1. Vectorizing Logistic Regression (1) Vectorizing the cost function (2) Vectorizing the gradient (3) Vectorizing the regularized cost function (4) Vectorizing the regularized gradi