当前位置: 首页 > news >正文

【Machine Learning】8.逻辑回归及其在分类问题的应用

逻辑回归及其在分类问题的应用

  • 1. 导入
  • 2.Sigmoid函数
  • 3.逻辑回归
  • 4.分类问题示例及决策边界
    • 4.1 导入
    • 4.2 数据集
    • 4.3 模型应用
      • Refresher on logistic regression and decision boundary
      • Plotting decision boundary

在分类问题之前,我们要知道sigmoid函数,以及逻辑回归是sigmoid函数在线性回归上的应用

1. 导入

import numpy as np
%matplotlib widget
import matplotlib.pyplot as plt
from plt_one_addpt_onclick import plt_one_addpt_onclick
from lab_utils_common import draw_vthresh
plt.style.use('./deeplearning.mplstyle')

2.Sigmoid函数

在分类时,我们希望分类模型的预测值介于0和1之间,因为我们的输出变量 y y y是0或1,而sigmoid函数可以把输出值压缩到0-1
g ( z ) = 1 1 + e − z (1) g(z) = \frac{1}{1+e^{-z}}\tag{1} g(z)=1+ez1(1)

在逻辑回归的情况下,z(sigmoid函数的输入)是线性回归模型的输出。

  • 在单个示例中, z z z是标量。
  • 在多个示例的情况下, z z z可以是由 m m m值组成的向量,每个示例对应一个值。
  • sigmoid函数的实现应涵盖这两种潜在的输入格式。

numpy自带exp函数可以把array中的数取 e z e^z ez,其中z为array中的数

# Input is an array. 
input_array = np.array([1,2,3])
exp_array = np.exp(input_array)

print("Input to exp:", input_array)
print("Output of exp:", exp_array)

# Input is a single number
input_val = 1  
exp_val = np.exp(input_val)

print("Input to exp:", input_val)
print("Output of exp:", exp_val)


Input to exp: [1 2 3]
Output of exp: [ 2.718  7.389 20.086]
Input to exp: 1
Output of exp: 2.718281828459045

手撕sigmoid(很简单)

def sigmoid(z):
    """
    Compute the sigmoid of z

    Args:
        z (ndarray): A scalar, numpy array of any size.

    Returns:
        g (ndarray): sigmoid(z), with the same shape as z
         
    """

    g = 1/(1+np.exp(-z))
   
    return g

对比一下处理前后的数据

# Generate an array of evenly spaced values between -10 and 10
z_tmp = np.arange(-10,11)

# Use the function implemented above to get the sigmoid values
y = sigmoid(z_tmp)

# Code for pretty printing the two arrays next to each other
np.set_printoptions(precision=3) 
print("Input (z), Output (sigmoid(z))")
print(np.c_[z_tmp, y])


Input (z), Output (sigmoid(z))
[[-1.000e+01  4.540e-05]
 [-9.000e+00  1.234e-04]
 [-8.000e+00  3.354e-04]
 [-7.000e+00  9.111e-04]
 [-6.000e+00  2.473e-03]
 [-5.000e+00  6.693e-03]
 [-4.000e+00  1.799e-02]
 [-3.000e+00  4.743e-02]
 [-2.000e+00  1.192e-01]
 [-1.000e+00  2.689e-01]
 [ 0.000e+00  5.000e-01]
 [ 1.000e+00  7.311e-01]
 [ 2.000e+00  8.808e-01]
 [ 3.000e+00  9.526e-01]
 [ 4.000e+00  9.820e-01]
 [ 5.000e+00  9.933e-01]
 [ 6.000e+00  9.975e-01]
 [ 7.000e+00  9.991e-01]
 [ 8.000e+00  9.997e-01]
 [ 9.000e+00  9.999e-01]
 [ 1.000e+01  1.000e+00]]

至于np.c_,可以看看numpy.c_

array将在升级到至少二维后沿其最后一个轴堆叠

np.c_[np.array([1,2,3]), np.array([4,5,6])]
array([[1, 4],
       [2, 5],
       [3, 6]])


np.c_[np.array([[1,2,3]]), 0, 0, np.array([[4,5,6]])]
array([[1, 2, 3, ..., 4, 5, 6]])

绘制z与sigmoid(z)的图像

# Plot z vs sigmoid(z)
fig,ax = plt.subplots(1,1,figsize=(5,3))
ax.plot(z_tmp, y, c="b")

ax.set_title("Sigmoid function")
ax.set_ylabel('sigmoid(z)')
ax.set_xlabel('z')
draw_vthresh(ax,0)

在这里插入图片描述

3.逻辑回归

如果w和x是向量:
f w , b ( x ( i ) ) = g ( w ⋅ x ( i ) + b ) (2) f_{\mathbf{w},b}(\mathbf{x}^{(i)}) = g(\mathbf{w} \cdot \mathbf{x}^{(i)} + b ) \tag{2} fw,b(x(i))=g(wx(i)+b)(2)

sigmoid依然是:

g ( z ) = 1 1 + e − z (3) g(z) = \frac{1}{1+e^{-z}}\tag{3} g(z)=1+ez1(3)
只是z的算法有变化:
在这里插入图片描述
此时就是逻辑回归模型,是将sigmoid运用到线性回归上的模型

4.分类问题示例及决策边界

我们要通过逻辑回归来获取分类问题的决策边界

4.1 导入

import numpy as np
%matplotlib widget
import matplotlib.pyplot as plt
from lab_utils_common import plot_data, sigmoid, draw_vthresh
plt.style.use('./deeplearning.mplstyle')

4.2 数据集

X = np.array([[0.5, 1.5], [1,1], [1.5, 0.5], [3, 0.5], [2, 2], [1, 2.5]])
y = np.array([0, 0, 0, 1, 1, 1]).reshape(-1,1) 
fig,ax = plt.subplots(1,1,figsize=(4,4))
plot_data(X, y, ax)

ax.axis([0, 4, 0, 3.5])
ax.set_ylabel('$x_1$')
ax.set_xlabel('$x_0$')
plt.show()

在这里插入图片描述

4.3 模型应用

  • 假设我们的决策边界直线形如

    f ( x ) = g ( w 0 x 0 + w 1 x 1 + b ) f(x) = g(w_0x_0+w_1x_1 + b) f(x)=g(w0x0+w1x1+b)

    where g ( z ) = 1 1 + e − z g(z) = \frac{1}{1+e^{-z}} g(z)=1+ez1, which is the sigmoid function

  • 假设我们拟合,得出的最优解 b = − 3 , w 0 = 1 , w 1 = 1 b = -3, w_0 = 1, w_1 = 1 b=3,w0=1,w1=1.,拟合的方法我们后面会谈到

    f ( x ) = g ( x 0 + x 1 − 3 ) f(x) = g(x_0+x_1-3) f(x)=g(x0+x13)

    (You’ll learn how to fit these parameters to the data further in the course)

Refresher on logistic regression and decision boundary

  • Recall that for logistic regression, the model is represented as

    f w , b ( x ( i ) ) = g ( w ⋅ x ( i ) + b ) (1) f_{\mathbf{w},b}(\mathbf{x}^{(i)}) = g(\mathbf{w} \cdot \mathbf{x}^{(i)} + b) \tag{1} fw,b(x(i))=g(wx(i)+b)(1)

    where g ( z ) g(z) g(z) is known as the sigmoid function and it maps all input values to values between 0 and 1:

    g ( z ) = 1 1 + e − z (2) g(z) = \frac{1}{1+e^{-z}}\tag{2} g(z)=1+ez1(2)
    and w ⋅ x \mathbf{w} \cdot \mathbf{x} wx is the vector dot product:

    w ⋅ x = w 0 x 0 + w 1 x 1 \mathbf{w} \cdot \mathbf{x} = w_0 x_0 + w_1 x_1 wx=w0x0+w1x1

  • We interpret the output of the model ( f w , b ( x ) f_{\mathbf{w},b}(x) fw,b(x)) as the probability that y = 1 y=1 y=1 given x x x and parameterized by w w w and b b b.

  • Therefore, to get a final prediction ( y = 0 y=0 y=0 or y = 1 y=1 y=1) from the logistic regression model, we can use the following heuristic -

    if f w , b ( x ) > = 0.5 f_{\mathbf{w},b}(x) >= 0.5 fw,b(x)>=0.5, predict y = 1 y=1 y=1

    if f w , b ( x ) < 0.5 f_{\mathbf{w},b}(x) < 0.5 fw,b(x)<0.5, predict y = 0 y=0 y=0

  • Let’s plot the sigmoid function to see where g ( z ) > = 0.5 g(z) >= 0.5 g(z)>=0.5

# Plot sigmoid(z) over a range of values from -10 to 10
z = np.arange(-10,11)

fig,ax = plt.subplots(1,1,figsize=(5,3))
# Plot z vs sigmoid(z)
ax.plot(z, sigmoid(z), c="b")

ax.set_title("Sigmoid function")
ax.set_ylabel('sigmoid(z)')
ax.set_xlabel('z')
draw_vthresh(ax,0)

在这里插入图片描述

  • As you can see, g ( z ) > = 0.5 g(z) >= 0.5 g(z)>=0.5 for z > = 0 z >=0 z>=0

  • For a logistic regression model, z = w ⋅ x + b z = \mathbf{w} \cdot \mathbf{x} + b z=wx+b. Therefore,

    if w ⋅ x + b > = 0 \mathbf{w} \cdot \mathbf{x} + b >= 0 wx+b>=0, the model predicts y = 1 y=1 y=1

    if w ⋅ x + b < 0 \mathbf{w} \cdot \mathbf{x} + b < 0 wx+b<0, the model predicts y = 0 y=0 y=0

Plotting decision boundary

  • Our logistic regression model has the form

    f ( x ) = g ( − 3 + x 0 + x 1 ) f(x) = g(-3 + x_0+x_1) f(x)=g(3+x0+x1)

  • From what you’ve learnt above, you can see that this model predicts y = 1 y=1 y=1 if − 3 + x 0 + x 1 > = 0 -3 + x_0+x_1 >= 0 3+x0+x1>=0

Let’s see what this looks like graphically. We’ll start by plotting − 3 + x 0 + x 1 = 0 -3 + x_0+x_1 = 0 3+x0+x1=0, which is equivalent to x 1 = 3 − x 0 x_1 = 3 - x_0 x1=3x0.

# Choose values between 0 and 6
x0 = np.arange(0,6)

x1 = 3 - x0
fig,ax = plt.subplots(1,1,figsize=(5,4))
# Plot the decision boundary
ax.plot(x0,x1, c="b")
ax.axis([0, 4, 0, 3.5])

# Fill the region below the line
ax.fill_between(x0,x1, alpha=0.2)

# Plot the original data
plot_data(X,y,ax)
ax.set_ylabel(r'$x_1$')
ax.set_xlabel(r'$x_0$')
plt.show()

在这里插入图片描述
后面使用高次多项式,会看到非线性边界

相关文章:

  • ZYNQ之路--带你弄明白Vivado设计流程
  • 最大子数组和-前缀和/动态规划/分治/暴力-Java/c++
  • [JS]JavaScript 注释 输入输出语句
  • 网络笔记大全(超详细)
  • 网课题库接口使用
  • 【计算方法】python实现高斯消去、列主元高斯消去,LU分解分别求解线性方程组
  • 回归预测 | MATLAB实现GA-BP多输入单输出回归预测
  • Nmap的API和库文件
  • Linux命令 -文件权限配置的深入(chown/chmod/setfacl)
  • ubuntu安装selenium
  • React脚手架工具创建项目的详细介绍
  • 26_TokenMongodb
  • 【工具】使用 sealos 部署 k8s 集群
  • LeetCode 每日一题 2022/9/19-2022/9/25
  • T1046判断一个数能否同时被3和5整除 (信息学一本通C++)
  • 2018天猫双11|这就是阿里云!不止有新技术,更有温暖的社会力量
  • C# 免费离线人脸识别 2.0 Demo
  • C++类的相互关联
  • Django 博客开发教程 8 - 博客文章详情页
  • DOM的那些事
  • JavaScript新鲜事·第5期
  • React组件设计模式(一)
  • scrapy学习之路4(itemloder的使用)
  • 爱情 北京女病人
  • 大型网站性能监测、分析与优化常见问题QA
  • 湖南卫视:中国白领因网络偷菜成当代最寂寞的人?
  • 将回调地狱按在地上摩擦的Promise
  • 数据仓库的几种建模方法
  • 延迟脚本的方式
  • 400多位云计算专家和开发者,加入了同一个组织 ...
  • 微龛半导体获数千万Pre-A轮融资,投资方为国中创投 ...
  • ​HTTP与HTTPS:网络通信的安全卫士
  • ​第20课 在Android Native开发中加入新的C++类
  • ​如何防止网络攻击?
  • ​如何在iOS手机上查看应用日志
  • #13 yum、编译安装与sed命令的使用
  • #ifdef 的技巧用法
  • #Ubuntu(修改root信息)
  • $().each和$.each的区别
  • (01)ORB-SLAM2源码无死角解析-(66) BA优化(g2o)→闭环线程:Optimizer::GlobalBundleAdjustemnt→全局优化
  • (Redis使用系列) Springboot 整合Redisson 实现分布式锁 七
  • (九)信息融合方式简介
  • (力扣记录)235. 二叉搜索树的最近公共祖先
  • .NET Core引入性能分析引导优化
  • .net 前台table如何加一列下拉框_如何用Word编辑参考文献
  • .net6+aspose.words导出word并转pdf
  • .NET8.0 AOT 经验分享 FreeSql/FreeRedis/FreeScheduler 均已通过测试
  • .NET分布式缓存Memcached从入门到实战
  • .NET中的十进制浮点类型,徐汇区网站设计
  • [ vulhub漏洞复现篇 ] JBOSS AS 4.x以下反序列化远程代码执行漏洞CVE-2017-7504
  • [8-23]知识梳理:文件系统、Bash基础特性、目录管理、文件管理、文本查看编辑处理...
  • [Android]如何调试Native memory crash issue
  • [C]整形提升(转载)
  • [CSS]CSS 字体属性
  • [Flutter]WindowsPlatform上运行遇到的问题总结