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

【Machine Learning】13.逻辑回归小结and练习

逻辑回归小结

  • 1.导入
  • 2.逻辑回归
    • 2.1 问题陈述
    • 2.2 数据加载和可视化
    • 2.3 Sigmoid函数
      • Exercise 1
    • 2.4 逻辑回归的代价函数 Cost function for logistic regression
      • Exercise 2
      • 2.5 逻辑回归的梯度下降

这节回顾一下之前的逻辑回归、正则化的理论知识并实践(光看代码不敲代码可不行啊)

1.导入

import numpy as np
import matplotlib.pyplot as plt
from utils import *
import copy
import math

%matplotlib inline

2.逻辑回归

在这部分练习中,你将建立一个逻辑回归模型来预测学生是否被大学录取。

2.1 问题陈述

假设你是一所大学系的管理员,你想根据两次考试的结果来确定每个申请人的入学机会。

  • 您有以前申请者的历史数据,可以用作逻辑回归的培训集。

  • 对于每个培训示例,您都有申请人在两次考试中的分数以及录取决定。

  • 你的任务是建立一个分类模型,根据这两门考试的分数估计申请者的录取概率。

2.2 数据加载和可视化

您将从加载此任务的数据集开始。

  • 下面显示的load_dataset()函数将数据加载到变量X_trainy_train中
  • X_train包含学生两次考试的成绩
  • “y_train”是录取决定
  • y_train=1如果学生被录取
  • y_train=0如果学生未被录取
  • X_train和`y_train’都是numpy数组。

加载数据

# load dataset
X_train, y_train = load_data("data/ex2data1.txt")

熟悉一下数据集

print("First five elements in X_train are:\n", X_train[:5]) #查看前五条数据
print("Type of X_train:",type(X_train))


First five elements in X_train are:
 [[34.62365962 78.02469282]
 [30.28671077 43.89499752]
 [35.84740877 72.90219803]
 [60.18259939 86.3085521 ]
 [79.03273605 75.34437644]]
Type of X_train: <class 'numpy.ndarray'>
print("First five elements in y_train are:\n", y_train[:5])
print("Type of y_train:",type(y_train))

First five elements in y_train are:
 [0. 0. 0. 1. 1.]
Type of y_train: <class 'numpy.ndarray'>
print ('The shape of X_train is: ' + str(X_train.shape))
print ('The shape of y_train is: ' + str(y_train.shape))
print ('We have m = %d training examples' % (len(y_train)))

The shape of X_train is: (100, 2)
The shape of y_train is: (100,)
We have m = 100 training examples

数据可视化

# Plot examples
plot_data(X_train, y_train[:], pos_label="Admitted", neg_label="Not admitted")

# Set the y-axis label
plt.ylabel('Exam 2 score') 
# Set the x-axis label
plt.xlabel('Exam 1 score') 
plt.legend(loc="upper right")
plt.show()

在这里插入图片描述

2.3 Sigmoid函数

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

g ( z ) = 1 1 + e − z g(z) = \frac{1}{1+e^{-z}} g(z)=1+ez1

Exercise 1

Please complete the sigmoid function to calculate

g ( z ) = 1 1 + e − z g(z) = \frac{1}{1+e^{-z}} g(z)=1+ez1

Note that

  • z 不一定是一个数,也可以是一个数组
  • 如果输入时数组,要对每个sigmoid函数应用值
  • 记得np.exp()的使用
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

2.4 逻辑回归的代价函数 Cost function for logistic regression

Exercise 2

Please complete the compute_cost function using the equations below.

Recall that for logistic regression, the cost function is of the form

J ( w , b ) = 1 m ∑ i = 0 m − 1 [ l o s s ( f w , b ( x ( i ) ) , y ( i ) ) ] (1) J(\mathbf{w},b) = \frac{1}{m}\sum_{i=0}^{m-1} \left[ loss(f_{\mathbf{w},b}(\mathbf{x}^{(i)}), y^{(i)}) \right] \tag{1} J(w,b)=m1i=0m1[loss(fw,b(x(i)),y(i))](1)

where

  • m is the number of training examples in the dataset

  • l o s s ( f w , b ( x ( i ) ) , y ( i ) ) loss(f_{\mathbf{w},b}(\mathbf{x}^{(i)}), y^{(i)}) loss(fw,b(x(i)),y(i)) is the cost for a single data point, which is -

    l o s s ( f w , b ( x ( i ) ) , y ( i ) ) = ( − y ( i ) log ⁡ ( f w , b ( x ( i ) ) ) − ( 1 − y ( i ) ) log ⁡ ( 1 − f w , b ( x ( i ) ) ) (2) loss(f_{\mathbf{w},b}(\mathbf{x}^{(i)}), y^{(i)}) = (-y^{(i)} \log\left(f_{\mathbf{w},b}\left( \mathbf{x}^{(i)} \right) \right) - \left( 1 - y^{(i)}\right) \log \left( 1 - f_{\mathbf{w},b}\left( \mathbf{x}^{(i)} \right) \right) \tag{2} loss(fw,b(x(i)),y(i))=(y(i)log(fw,b(x(i)))(1y(i))log(1fw,b(x(i)))(2)

  • f w , b ( x ( i ) ) f_{\mathbf{w},b}(\mathbf{x}^{(i)}) fw,b(x(i)) is the model’s prediction, while y ( i ) y^{(i)} y(i), which is the actual label

  • f w , b ( x ( i ) ) = g ( w ⋅ x ( i ) + b ) f_{\mathbf{w},b}(\mathbf{x}^{(i)}) = g(\mathbf{w} \cdot \mathbf{x^{(i)}} + b) fw,b(x(i))=g(wx(i)+b) where function g g g is the sigmoid function.

    • It might be helpful to first calculate an intermediate variable z w , b ( x ( i ) ) = w ⋅ x ( i ) + b = w 0 x 0 ( i ) + . . . + w n − 1 x n − 1 ( i ) + b z_{\mathbf{w},b}(\mathbf{x}^{(i)}) = \mathbf{w} \cdot \mathbf{x^{(i)}} + b = w_0x^{(i)}_0 + ... + w_{n-1}x^{(i)}_{n-1} + b zw,b(x(i))=wx(i)+b=w0x0(i)+...+wn1xn1(i)+b where n n n is the number of features, before calculating f w , b ( x ( i ) ) = g ( z w , b ( x ( i ) ) ) f_{\mathbf{w},b}(\mathbf{x}^{(i)}) = g(z_{\mathbf{w},b}(\mathbf{x}^{(i)})) fw,b(x(i))=g(zw,b(x(i)))

Note:

  • As you are doing this, remember that the variables X_train and y_train are not scalar values but matrices of shape ( m , n m, n m,n) and ( 𝑚 𝑚 m,1) respectively, where 𝑛 𝑛 n is the number of features and 𝑚 𝑚 m is the number of training examples.
  • You can use the sigmoid function that you implemented above for this part.
  • numpy有np.log()
def compute_cost(X, y, w, b, lambda_= 1):
    """
    Computes the cost over all examples
    Args:
      X : (ndarray Shape (m,n)) data, m examples by n features
      y : (array_like Shape (m,)) target value 
      w : (array_like Shape (n,)) Values of parameters of the model      
      b : scalar Values of bias parameter of the model
      lambda_: unused placeholder
    Returns:
      total_cost: (scalar)         cost 
    """

    m, n = X.shape
    loss_sum = 0
    ### START CODE HERE ###
    for i in range(m):
      z_wb = 0
      for j in range(n):
        z_wb_xi = w[j] * X[i][j]
        z_wb += z_wb_xi

      z_wb += b
      f_wb = sigmoid(z_wb)
      loss = -y[i] * np.log(f_wb) - (1-y[i]) * np.log(1-f_wb)
      loss_sum += loss

    total_cost = loss_sum/m
    ### END CODE HERE ### 
    
    return total_cost

代码测试

m, n = X_train.shape

# Compute and display cost with w initialized to zeroes
initial_w = np.zeros(n)
initial_b = 0.
cost = compute_cost(X_train, y_train, initial_w, initial_b)
print('Cost at initial w (zeros): {:.3f}'.format(cost))

Cost at initial w (zeros): 0.693

2.5 逻辑回归的梯度下降

相关文章:

  • Cadence Allegro 过孔通孔盲孔埋孔详细说明及设计举例图文教程
  • Spring boot再来一遍
  • Mathorcup数学建模竞赛第三届-【妈妈杯】B题:关于三维健康评分模型的研究(附带赛题解析获奖论文)(一)
  • 最新版本vscode 真正解决用vscode + unity搭配开发没有代码智能提示 OmniSharp服务启动 vscode调试unity准备
  • T1064 奥运奖牌计数(信息学一本通C++)
  • python爬虫--cookie、防盗链、代理
  • Vue3+TSX开发模式下内置组件的替代方案
  • 燃烧化学平衡判据
  • 05--Django视图层-JsonResponse对象、request对象中的其他方法以及FBV与CBV的书写
  • Spring中Bean的生命周期详解
  • Linux文件之/etc/passwd和/etc/shadow
  • OCR - 微软windows 11系统自带的Windows OCR功能初体验
  • 公众号网课查题系统
  • 关于SELECT...FOR UPDATE到底锁表还是锁行
  • 解决问题的思路很重要,运维领域,结果对,过程就对!
  • [ 一起学React系列 -- 8 ] React中的文件上传
  • Codepen 每日精选(2018-3-25)
  • IndexedDB
  • JavaScript 基础知识 - 入门篇(一)
  • MySQL-事务管理(基础)
  • NLPIR语义挖掘平台推动行业大数据应用服务
  • Perseus-BERT——业内性能极致优化的BERT训练方案
  • Phpstorm怎样批量删除空行?
  • Spring Boot MyBatis配置多种数据库
  • thinkphp5.1 easywechat4 微信第三方开放平台
  • yii2中session跨域名的问题
  • 搭建gitbook 和 访问权限认证
  • 订阅Forge Viewer所有的事件
  • 给第三方使用接口的 URL 签名实现
  • 给新手的新浪微博 SDK 集成教程【一】
  • 基于Mobx的多页面小程序的全局共享状态管理实践
  • 蓝海存储开关机注意事项总结
  • 前端js -- this指向总结。
  • 前端面试之闭包
  • 容器化应用: 在阿里云搭建多节点 Openshift 集群
  • 学习HTTP相关知识笔记
  • ​520就是要宠粉,你的心头书我买单
  • $.ajax()参数及用法
  • ${factoryList }后面有空格不影响
  • (pojstep1.1.2)2654(直叙式模拟)
  • (笔记)Kotlin——Android封装ViewBinding之二 优化
  • (经验分享)作为一名普通本科计算机专业学生,我大学四年到底走了多少弯路
  • (一)80c52学习之旅-起始篇
  • (转)如何上传第三方jar包至Maven私服让maven项目可以使用第三方jar包
  • ******之网络***——物理***
  • *_zh_CN.properties 国际化资源文件 struts 防乱码等
  • .bat批处理(七):PC端从手机内复制文件到本地
  • .NET Core IdentityServer4实战-开篇介绍与规划
  • .NET Framework与.NET Framework SDK有什么不同?
  • .NET Standard、.NET Framework 、.NET Core三者的关系与区别?
  • .NET 服务 ServiceController
  • .NET/C# 利用 Walterlv.WeakEvents 高性能地定义和使用弱事件
  • .NET6实现破解Modbus poll点表配置文件
  • .net的socket示例
  • .NET多线程执行函数