吴恩达机器学习之路---logistic regression

2023-11-11

logistic regression

一、Logistic 回归(利用matlib实现:基础版)

1、logistic regression数学基础

1.1 此示例为二元分类,二元分类的最终预测结果h为{0, 1},为获得此效果,使用sigmoid函数/logistic函数:

g ( z ) = 1 / 1 + e x p ( − z ) g(z) = 1 / 1 + exp(-z) g(z)=1/1+exp(z)

效果图如下:
在这里插入图片描述
1.2 计算代价函数和代价函数对θ(j) (j = 0, …,n,n为特征数量)的偏导数。
在这里插入图片描述
在这里插入图片描述

2. 代码分析

2.1 对数据进行预处理,获得特征矩阵X,和答案向量y;

data = load('ex2data1.txt');
[m, n] = size(data);
X = data(:,1:2);
X = [ones(m,1), X];
y = data(:,3);

2.2 将数据分为正样本,负样本,并进行可视化, plotData(X, y).m;

function plotData(X, y)
figure; hold on;
pos = find(y);
neg = find(y == 0);
plot(X(pos,1), X(pos, 2), 'k+','LineWidth', 2, 'MarkerSize', 7);
plot(X(neg,1), X(neg, 2), 'ko', 'MarkerFaceColor', 'y','MarkerSize', 7);

end

2.3 初始化训练参数theta

initial_theta = zeros(n,1);

2.4 计算初始化的cost 和 grad,定义函数costFunction(initial_theta, X, y)

function [J, grad] = costFunction(theta, X, y)
m = length(y); % number of training examples
J = 0;
grad = zeros(size(theta));

h = sigmoid(X * theta);
J = -sum(y .* log(h) + (1-y) .* log(1-h))/m;

error = h - y;
grad = (X' * error)/m;

end

2.5 使用fminunc得到优化的theta和对应的cost:fminunc()函数会自动计算出合适的theta,并求出对应的cost,只需输入初始化theta,并且无需编写theta = theta - gradient,只需在fminunc@的函数中写出J 和 gradient

% 设置fminunc的选项
options = optimoptions(@fminunc, 'Algorithm', 'Quasi-Newton', 'GradObj', 'on', 'MaxIter',400);

% 运行fminunc获得最优的theta
% 这个方程将会返回theta和cost
[theta, cost] = fminunc(@(t)costFunction(t,X,y), initial_theta, options);

2.6 画出决策边界;定义函数plotDecisionBoundary(theta,X,y);

function plotDecisionBoundary(theta, X, y)

plotData(X(:,2:3), y);
hold on

if size(X, 2) <= 3
    plot_x = [min(X(:,2))-2,  max(X(:,2))+2];

    plot_y = (-1./theta(3)).*(theta(2).*plot_x + theta(1));

    plot(plot_x, plot_y)
    
    legend('Admitted', 'Not admitted', 'Decision Boundary')
    axis([30, 100, 30, 100])
else

    u = linspace(-1, 1.5, 50);
    v = linspace(-1, 1.5, 50);

    z = zeros(length(u), length(v));

    for i = 1:length(u)
        for j = 1:length(v)
            z(i,j) = mapFeature(u(i), v(j))*theta;
        end
    end
    z = z'; % important to transpose z before calling contour

    contour(u, v, z, [0, 0], 'LineWidth', 2)
end
hold off

end

2.7 输入新样本进行预测

prob = sigmoid([1 45 85] * theta);

2.8 计算训练集的准确度;定义函数predict(theta, X);

function p = predict(theta, X)

m = size(X, 1); % Number of training examples
p = zeros(m, 1);
s = sigmoid(X * theta);
p = s >= 0.5;

end

整体代码如下:

clear all; close all; clc
% 获取数据并构建特征值矩阵X, 结果向量y
data = load('ex2data1.txt');
[m, n] = size(data);
X = data(:,1:2);
X = [ones(m,1), X];
y = data(:,3);

% 数据可视化
plotData(X,y);

%定义轴标签
xlabel('Exam 1 score')
ylabel('Exam 2 score')

%显示图例
legend('Admitted', 'Not admitted')

%初始化训练参数
initial_theta = zeros(n,1);

%计算并打印初始cost和gradient
[cost, grad] = costFunction(initial_theta,X,y);
fprintf('Cost at initial theta(zeros): %f\n', cost);
disp('Gradient at initial theta:'); disp(grad);

% 设置fminunc的选项
options = optimoptions(@fminunc, 'Algorithm', 'Quasi-Newton', 'GradObj', 'on', 'MaxIter',400);

% 运行fminunc获得最优的theta
% 这个方程将会返回theta和cost
[theta, cost] = fminunc(@(t)costFunction(t,X,y), initial_theta, options);

% 显示theta
fprintf('Cost of theta found by fminunc: %f\n', cost);
disp('theta:'); disp(theta);

% 数据可视化
plotDecisionBoundary(theta,X,y);

%定义轴标签
xlabel('Exam 1 score')
ylabel('Exam 2 score')

%显示图例
legend('Admitted', 'Not admitted')
hold off;

%预测一个学生第一次考试为45,第二次考试为85,通过的可能性。
prob = sigmoid([1 45 85] * theta);
fprintf('For a student with scores 45 and 85, we predict an admission probability of %f\n\n', prob);
%计算训练集的准确度
p = predict(theta, X);
fprintf('Train Accuracy: %f\n', mean(double(p == y)) * 100);

结果图如下:
在这里插入图片描述

二、Logistic 回归(利用matlib实现:进阶版)

1.基本思想

使用fitglm函数获得logistic regression模型来预测通过的可能性。

2. 代码分析

2.0 本地函数:plotMdlData函数

plotMdlData用于绘制数据集的正例和负例,以便更好地与ex2中的图进行比较。

function [] = plotMdlData(data)
% Reproduce the plots from ex2 with positive and negative results for an input table
% Extract variable names from 3 column table
varNames = data.Properties.VariableNames;
% Plot the data with + for true and 0 for false examples
inds = data.(varNames{3}) == 1;
plot(data.(varNames{1})(inds), data.(varNames{2})(inds), 'k+','LineWidth', 2, 'MarkerSize', 7); 
inds = data.(varNames{3}) == 0;
plot(data.(varNames{1})(inds), data.(varNames{2})(inds), 'ko', 'MarkerFaceColor', 'y','MarkerSize', 7);
end

2.1 将数据加载为一个表,并预览数据

clear:
data = readtable('ex2data1.txt');
data.Properties.VariableNames = {'Exam1', 'Exam2', 'Admitted'};
data.Admitted = logical(data.Admitted)
% 显示数据表总体信息
summary(data)

结果如下:
在这里插入图片描述
2.2 使用fitglm训练模型
Logistic回归模型属于一类较大的线性模型,在MATLAB中被称为广义线性模型。 为了训练广义线性模型,我们使用fitglm函数。 运行本节中的代码以在exam data 上训练逻辑回归模型。 结果是GeneralizedLinearModel变量,其中包含有关模型的所有信息。 请注意,要从fitglm获得logistic回归模型,我们将Distribution参数设置为二项式,如以下代码所示:

logMdl = fitglm(data,'Distribution','binomial')

结果如下:

在这里插入图片描述
注意上面输出中显示的模型形式。 这是对以下公式的简写:
l o g i t ( A d m i t t e d ) = 1 ∗ θ 0 + E x a m 1 ∗ θ 1 + E x a m 2 ∗ θ 2 logit(Admitted) = 1 * θ0 + Exam1 * θ1 + Exam2 * θ2 logit(Admitted)=1θ0+Exam1θ1+Exam2θ2

由于是 sigmoid(x) 的反函数,因此该模型等效于由用ex2中的Admitted概率组成的logistic回归模型:
在这里插入图片描述
其中包括两个考试成绩和一个偏差项。 与fitlm在ex1脚本中训练的线性回归模型一样,fitglm自动添加偏差项。

2.3 预测训练准确率和通过的可能性

theta = logMdl.Coefficients.Estimate

在这里插入图片描述

% Predict the probability for a student with scores of 45 and 85
prob = predict(logMdl,[45 85]);
fprintf('For a student with scores 45 and 85, we predict an admission probability of %f\n\n', prob);
% Compute the training accuracy
Admitted = predict(logMdl,data) > 0.5;
fprintf('Train Accuracy: %f\n', mean(double(Admitted == data.Admitted)) * 100);

2.4 可视化决策边界

figure; hold on;
% Plot the positive and negative examples
plotMdlData(data);

% Plot the decision boundary
xvals = [min(data.Exam1), max(data.Exam1)];
yvals = -(theta(1)+theta(2)*xvals)/theta(3);
plot(xvals,yvals); hold off;
ylim([min(data.Exam2),max(data.Exam2)]);

% Labels and Legend
xlabel('Exam 1 score')
ylabel('Exam 2 score')
legend('Admitted','Not admitted','Decision Boundary')
hold off;

在这里插入图片描述

三、正则化regularized logistic 回归

1.基本思想:

1.1 更好地拟合数据的一种方法是从每个数据点创建更多特征。
在这里插入图片描述
1.2 修改代价函数和梯度为:
在这里插入图片描述
在这里插入图片描述

2.代码分析:

2.1 增加多项式特征:定义mapFeature(X1, X2)函数
X = mapFeature(X(:,1),X(:,2));

function out = mapFeature(X1, X2)
degree = 6;
out = ones(size(X1(:,1)));
for i = 1:degree
    for j = 0:i
        out(:, end+1) = (X1.^(i-j)).*(X2.^j);
    end
end

end

2.2 计算正则化logistic回归的cost和grad:定义costFunctionReg(theta, X, y)

function [J, grad] = costFunctionReg(theta, X, y, lambda)
m = length(y); % number of training examples

J = 0;
grad = zeros(size(theta));

h = sigmoid(X * theta);
reg = (lambda / (2*m)) * sum(theta(2:end) .^ 2);
J = -1/m * sum(y .* log(h) + (1-y) .* log(1-h)) + reg;

error = h - y;
tmp = theta;
tmp(1) = 0;
grad = (1/m) * (X' * error) + lambda/m*tmp;

完整代码:

clear all; close all; clc
% 初始化数据,特征值矩阵,结果向量
data = load('ex2data2.txt');
X = data(:,1:2);
y = data(:,3);

%可视化数据
plotData(X, y)
xlabel('Microchip test 1')
ylabel('Microchip test 2')
legend('y = 1', 'y = 0')

%增加多项式特征
X = mapFeature(X(:,1),X(:,2));
[m, n] = size(X);

%初始化theta,lambda
initial_theta = zeros(n,1);
lambda = 1;

%计算和显示初始化的theta的cost和grad
[cost, grad] = costFunctionReg(initial_theta, X, y, lambda)
fprintf('cost at initial theta (zeros): %f\n', cost);
disp('grad:'); disp(grad);

%运行fminunc获得最优theta,无需指定学习率和循环
%返回theta和cost
options = optimoptions(@fminunc, 'Algorithm', 'Quasi-Newton', 'GradObj', 'on', 'MAXITER', 1000);

[theta, J, exit_flag] = fminunc(@(t)costFunctionReg(t,X,y,lambda), initial_theta, options);

%显示theta和J
fprintf('cost at  theta found by fminuc: %f\n',J);
disp('theta: '); disp(theta);

%可视化决策边界
plotDecisionBoundary(theta ,X, y)
xlabel('Microchip test 1')
ylabel('Microchip test 2')

legend('y = 1', 'y = 0', 'Decision boundary')
hold off;

% 计算训练集的准确率
p = predict(theta, X);

fprintf('Train Accuracy: %f\n', mean(double(p == y)) * 100);

结果图如下:
在这里插入图片描述

四、正则化regularized logistic 回归(进阶版)

1、基本思想:

我们使用fitclinear函数训练一个包括正则化的逻辑回归模型。 由于fitclinear函数通常用于高维数据(即具有很多变量,例如我们的多项式特征模型),因此在表变量中存储数据的意义不大,因此取而代之的是采用数字矩阵形式的训练数据。

2、代码分析:

2.1 加载数据

clear;
X = load('ex2data2.txt');
y = X(:,3);
X(:,3) = [];
% Create the polynomial feature matrix up to power 6
powers = [nchoosek(0:6,2); fliplr(nchoosek(0:6,2));1 1;2 2;3 3]';
powers(:,sum(powers)>6) = [];
Xpoly = (X(:,1).^powers(1,:)).*(X(:,2).^powers(2,:));

2.2 训练模型

我们使用fitclinear训练模型,并将正则化类型设置为ridge(这是ex2中使用的正则化类型),并使用lambda给出强度。 结果是一个ClassificationLinear模型变量,其中包含有关模型的所有信息。 在模型变量的Bias和Beta属性中可以找到模型系数。 使用控件选择一个值,然后从以下两个部分的结果中检查对训练准确性和决策边界的影响:

% Choose lambda and train the model
lambda = 0.001;
logMdl = fitclinear(Xpoly,y,'Lambda',lambda,'Learner','logistic','Regularization','ridge')
logMdl.Bias
logMdl.Beta

2.3 使用正则化模型预测类并绘制决策边界

将预测函数用于使用CategoryLinear模型变量以与GeneralizedLinearModel变量相同的方式对数据进行分类。 但是,预测函数将返回类标签,而不是概率得分。 如果需要,我们可以通过请求预测的第二个输出来获得概率分数。

% Obtain the class labels and compute the training accuracy
Pass = predict(logMdl,Xpoly);
fprintf('Train Accuracy: %f\n', mean(Pass == y) * 100);
% Plot the positve and negative examples
figure; hold on;
plotMdlData(array2table([X y],'VariableNames',{'Test1','Test2','Pass'})); 

% Plot the decision boundary
xvals = linspace(min(X(:,1)), max(X(:,1)));
yvals = linspace(min(X(:,2)), max(X(:,2)));
[Xgrid, Ygrid] = meshgrid(xvals,yvals);
Xpolygrid = (Xgrid(:).^powers(1,:)).*(Ygrid(:).^powers(2,:));
[~,Score] = predict(logMdl,Xpolygrid); % Obtain the probability scores
contour(Xgrid,Ygrid,reshape(Score(:,2),size(Xgrid)),[0.5,0.5]); hold off;
% Labels and legend
xlabel('Test 1 score')
ylabel('Test 2 score')
legend('Pass', 'Fail','Decision Boundary')

以上为matlib实现logistic regression,在下面这边文档中将使用Python实现该分类算法。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

吴恩达机器学习之路---logistic regression 的相关文章

  • Explicit 关键字简介

    Explicit 关键字简介 explicit关键字用来修饰类的构造函数 表明构造函数是显示的 相对的是implicit关键字 首先这个关键字只能用在类内部的构造函数声明上 而不能用在类外部的函数定义上 它的作用是不能进行隐式转换 clas

随机推荐

  • ntp服务器不稳定,NTP服务器的管理如何保持如此准确?

    我们中的许多人对我们的计算机和其他设备偶尔出现问题 保留了准确的时间设置 但是与NTP服务器的快速同步使得一切顺利 但是如果我们自己的设备丢失准确性 NTP服务器如何保持这样准确 今天的问题 照片由LEOL30 Flickr 提供 问题 S
  • 信息化和数字化的本质区别有哪些?

    信息化和数字化的本质区别在于信息化以流程为中心 而数字化以业务为中心 数字化转型与传统信息化的主要区别在于 一方面传统信息化主要涵义是 流程 的信息化 而数字化的主要含义是构建 业务数字化 数字资产化 资产服务化 服务业务化 闭环 通过数字
  • Qt断开信号与槽

    关于信号与槽的研究 DisConnect bool QObject disconnect const QObject sender const char signal const QObject receiver const char me
  • git的安装配置与操作

    一 git 安装与配置 1 git 安装 windows 搜索 git for windows linux yum install y git 查看安装完成 git version git help 2 git 配置 A 配置用户信息 每一
  • android使用隐藏api的方法(使用被@hide的api)

    前言 一开始需要说明的是 Google之所以要将一些API隐藏 指加上 hide标记的public类 方法或常量 是有原因的 其中很大的原因就是Android系统本身还在不断的进化发展中 从1 0 1 1到现在即将问世的Android 2
  • 一个web请求在springboot经历了什么

    写了一个MailServiceController接口 在入口处打上断电 debug启动 可以看到 tomcat embed core 9 0 36 jar 1 run 748 Thread java lang 2 run 61 TaskT
  • Python编程之理解对象

    1 python中的函数和类均是对象 体现在以下几方面 1 都可以赋值给一个变量 2 可以添加到集合对象中 3 可以作为参数传递给函数 4 可以当作函数的返回值 如果一个函数没有return语句 则默认返回None 2 type class
  • 什么是用户token(令牌)-- 转

    在目前的互联网或者计算机网络技术中 经常会听到token或者 令牌 这个词 那有没有想过 token或者说令牌到底是什么东西 有什么作用 为什么token的中文翻译是 令牌 其实这个问题也困扰了我很长的时间 长久以来我都是从token的形式
  • 混合开发监听安卓手机物理返回键

    混合开发监听安卓手机物理返回键 在用h5做混合开发过程中由于有个考试考试页面 中途不能退出 退出要添加确认操作 所以需要监听手机的返回操作 不让用户直接通过返回键返回 目前了解到混合开发中有两种方式监听 方式一 监听popstate 用到的
  • 指针式仪表识别读数 Python(已开源数据集)

    目录 一 前言 二 使用方法 1 安装相关的库 2 运行 三 方法说明 MeterDetection类说明 类参数 主函数 self ImgCutCircle 截取表盘区域 滤除背景 self ContoursFilter 对轮廓进行筛选
  • linux下的shell 快捷键

    linux下的shell 快捷键 2011 05 24 14 06 51 转载 标签 杂谈 分类 linux Ctrl p重复上一次命令 Ctrl a跳到第一个字符前 Ctrl x同上但再按一次会从新回到原位置 Ctrl b前移一个字符不删
  • 深度探索C++对象模型(20)——函数语义学(4)——多继承第二基类对虚函数支持的影响、虚继承下的虚函数

    1 多继承第二基类对虚函数支持的影响 子类继承了几个父类 子类就有几个虚函数表 this指针调整的目的就是让对象指针正确的指向对象首地址 从而能正确的调用对象的成员函数或者说正确确定数据成员的存储位置 多重继承下 有几种情况 第二个或者后续
  • ubuntu设置共享文件夹与linux进行文件共享

    1 打开虚拟机设置 选项 共享文件夹 添加一个文件夹路径 这个路径是windows下 的路径 比如说E盘 可以在E盘里面新建一个 share文件夹 然后确定 2 在Linux目录下 cd mnt hgfs E share 就可以看到里面的文
  • NoSQL的概念

    NoSQL概述 发展历程 1 单机MySQL的年代 网站发展之初 网站的访问量基本不会太大 单个数据库完全足够 那个时候基本都是静态网页HTML服务器没有压力 数据量如果太大 一个机器放不下了 B Tree 索引也放不下了 访问量太大 一个
  • yum安装软件时报错libmysqlclient.so.18()(64bit)

    环境 CentOS 7 4 使用阿里yum的网络源 问题 使用yum安装软件时报错 2 postfix 2 10 1 6 el7 x86 64 has missing requires of libmysqlclient so 18 64b
  • PyQt5中为QTextEdit的某些字符单独设置大小和颜色

    QTextEdit支持富文本 因此您可以将css样式与html一起用于QTextEdit中的文本 可以使用不同的样式附加不同的富文本 为方便起见 只需创建一些格式化文本 并将相应的文本传递给python string的format方法来创建
  • [Linux安装软件详解系列]01 安装MySQL8.0

    目录 1 检查有没有安装MySQL 2 安装MySQL8 0 1 下载 rpm文件 2 上传rpm文件到服务器 3 安装rpm文件 4 查看安装好的包 5 安装MySQL 5 启动MySQL 3 本地登录 1 查看默认密码 2 本地登录My
  • 这注定是一场独一无二的旅行——周年纪念日 [form 2022 to 2023]

    啊哈 竟然已经一周年了 还记得自己写了两三篇博客以后 就停写了很久 很久 总是因为各种各样的事情拖沓 直到有一天CSDN轻轻敲醒了我沉睡的心灵 忽然意识到自己好久没写了 也让我去想自己的初衷 为何而写 Why 一直觉得内容创作是一个很酷的事
  • 华为OD机试 Python 查找人名

    描述 有一串由逗号分隔的人名 每个人名可能由一个或多个单词组成 请你设计一个方法 根据指定的前缀串 找出与前缀匹配的人名 前缀串的构造是由人名中每个单词的第一个字母组合而成 输入 一串用逗号分隔的人名 一个前缀串 输出 匹配前缀串的所有人名
  • 吴恩达机器学习之路---logistic regression

    logistic regression 一 Logistic 回归 利用matlib实现 基础版 1 logistic regression数学基础 1 1 此示例为二元分类 二元分类的最终预测结果h为 0 1 为获得此效果 使用sigmo