keras.utils.to_categorical和one hot格式解析

作者:gdl3463315 时间:2023-10-03 18:27:12 

keras.utils.to_categorical这个方法,源码中,它是这样写的:

Converts a class vector (integers) to binary class matrix.

E.g. for use with categorical_crossentropy.

也就是说它是对于一个类型的容器(整型)的转化为二元类型矩阵。比如用来计算多类别交叉熵来使用的。

其参数也很简单:


def to_categorical(y, num_classes=None):
Arguments
y: class vector to be converted into a matrix
(integers from 0 to num_classes).
num_classes: total number of classes.

说的很明白了,y就是待转换容器(其类型为从0到类型数目),而num_classes则是类型的总数。

这样这一句就比较容易理解了:

先通过np生成一个1000*1维的其值为0-9的矩阵,然后再通过keras.utils.to_categorical方法获取成一个1000*10维的二元矩阵。

y_train = keras.utils.to_categorical(np.random.randint(10, size=(1000, 1)), num_classes=10)

说了这么多,其实就是使用onehot对类型标签进行编码。下面的也都是这样解释。

one hot编码是将类别变量转换为机器学习算法易于利用的一种形式的过程。

通过例子可能更容易理解这个概念。

假设我们有一个迷你数据集:

公司名 类别值 价格
VW 1 20000
Acura 2 10011
Honda 3 50000
Honda 3 10000

其中,类别值是分配给数据集中条目的数值编号。比如,如果我们在数据集中新加入一个公司,那么我们会给这家公司一个新类别值4。当独特的条目增加时,类别值将成比例增加。

在上面的表格中,类别值从1开始,更符合日常生活中的习惯。实际项目中,类别值从0开始(因为大多数计算机系统计数),所以,如果有N个类别,类别值为0至N-1.

sklear的LabelEncoder可以帮我们完成这一类别值分配工作。

现在让我们继续讨论one hot编码,将以上数据集one hot编码后,我们得到的表示如下:

VW Acura Honda 价格
1 0 0 20000
0 1 0 10011
0 0 1 50000
0 0 1 10000

简单来说:**keras.utils.to_categorical函数是把类别标签转换为onehot编码(categorical就是类别标签的意思,表示现实世界中你分类的各类别),

而onehot编码是一种方便计算机处理的二元编码。**

补充知识:序列预处理:序列填充之pad_sequences()和one-hot转化之keras.utils.to_categorical()

tensorflow文本处理中,经常会将 padding 和 one-hot 操作共同出现,所以以下两种方法为有效且常用的方法:

一、keras.preprocessing.sequence.pad_sequences()

keras.utils.to_categorical和one hot格式解析

实例:


>>>list_1 = [[2,3,4]]
>>>keras.preprocessing.sequence.pad_sequences(list_1, maxlen=10)
array([[0, 0, 0, 0, 0, 0, 0, 2, 3, 4]], dtype=int32)

>>>list_2 = [[1,2,3,4,5]]
>>>keras.preprocessing.sequence.pad_sequences(list_2, maxlen=10)
array([[0, 0, 0, 0, 0, 1, 2, 3, 4, 5]], dtype=int32)

二、keras.utils.to_categorical()

to_categorical(y, num_classes=None, dtype='float32')

将整型标签转为onehot。y为int数组,num_classes为标签类别总数,大于max(y)(标签从0开始的)。

返回:如果num_classes=None,返回len(y) * [max(y)+1](维度,m*n表示m行n列矩阵,下同),否则为len(y) * num_classes。说出来显得复杂,请看下面实例。


import keras

ohl=keras.utils.to_categorical([1,3])
# ohl=keras.utils.to_categorical([[1],[3]])
print(ohl)
"""
[[0. 1. 0. 0.]
[0. 0. 0. 1.]]
"""
ohl=keras.utils.to_categorical([1,3],num_classes=5)
print(ohl)
"""
[[0. 1. 0. 0. 0.]
[0. 0. 0. 1. 0.]]
"""

来源:https://blog.csdn.net/gdl3463315/article/details/82659378

标签:keras,utils.to,categorical,one,hot
0
投稿

猜你喜欢

  • python中列表的常见操作梳理总结(一)

    2022-08-25 10:57:03
  • python 怎样将dataframe中的字符串日期转化为日期的方法

    2022-09-14 16:21:49
  • Perl中的控制结构学习笔记

    2023-05-17 20:08:18
  • Asp 操作Cookies(包括设置[赋值]、读取、删除[设置过期时间])

    2011-03-10 11:06:00
  • 常用的JavaScript表单验证正则表达式收集

    2008-02-29 08:34:00
  • python client使用http post 到server端的代码

    2021-09-03 14:33:54
  • Oracle数据库及应用程序优化开发者网络Oracle

    2010-07-18 13:02:00
  • python3 map函数和filter函数详解

    2023-05-02 02:17:32
  • ASP Framework_1_简介

    2009-10-12 11:35:00
  • python生成lmdb格式的文件实例

    2021-07-18 21:57:05
  • pandas分别写入excel的不同sheet方法

    2022-08-27 04:15:03
  • 使用pandas忽略行列索引,纵向拼接多个dataframe

    2022-05-23 08:52:42
  • Python的pycurl包用法简介

    2023-11-27 14:38:21
  • Go语言中日期包(time包)的具体使用

    2024-05-08 10:52:33
  • Python使用pyodbc访问数据库操作方法详解

    2021-08-02 19:12:29
  • SQL Server 2005日志文件损坏的处理方法

    2008-12-02 14:36:00
  • python批量复制图片到另一个文件夹

    2021-04-30 09:36:50
  • 手把手教你用Hexo+Github搭建属于自己的博客(详细图文)

    2022-11-29 10:34:21
  • Mysql数据库之索引优化

    2024-01-23 19:27:40
  • Python Pandas聚合函数的应用示例

    2022-12-13 22:39:31
  • asp之家 网络编程 m.aspxhome.com