浅谈keras中的keras.utils.to_categorical用法


Posted in Python onJuly 02, 2020

如下所示:

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.]]
"""

该部分keras源码如下:

def to_categorical(y, num_classes=None, dtype='float32'):
  """Converts a class vector (integers) to binary class matrix.

  E.g. for use with categorical_crossentropy.

  # Arguments
    y: class vector to be converted into a matrix
      (integers from 0 to num_classes).
    num_classes: total number of classes.
    dtype: The data type expected by the input, as a string
      (`float32`, `float64`, `int32`...)

  # Returns
    A binary matrix representation of the input. The classes axis
    is placed last.
  """
  y = np.array(y, dtype='int')
  input_shape = y.shape
  if input_shape and input_shape[-1] == 1 and len(input_shape) > 1:
    input_shape = tuple(input_shape[:-1])
  y = y.ravel()
  if not num_classes:
    num_classes = np.max(y) + 1
  n = y.shape[0]
  categorical = np.zeros((n, num_classes), dtype=dtype)
  categorical[np.arange(n), y] = 1
  output_shape = input_shape + (num_classes,)
  categorical = np.reshape(categorical, output_shape)
  return categorical

补充知识:keras笔记——keras.utils.to_categoracal()函数

keras.utils.to_categoracal (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。

以上这篇浅谈keras中的keras.utils.to_categorical用法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中处理字符串之endswith()方法的使用简介
May 18 Python
Python脚本实现虾米网签到功能
Apr 12 Python
对numpy中shape的深入理解
Jun 15 Python
python 运用Django 开发后台接口的实例
Dec 11 Python
浅析Python+OpenCV使用摄像头追踪人脸面部血液变化实现脉搏评估
Oct 17 Python
python读取word 中指定位置的表格及表格数据
Oct 23 Python
python实现超市管理系统(后台管理)
Oct 25 Python
python3实现往mysql中插入datetime类型的数据
Mar 02 Python
python列表的逆序遍历实现
Apr 20 Python
Python 爬虫性能相关总结
Aug 03 Python
Python可以用来做什么
Nov 23 Python
python库Tsmoothie模块数据平滑化异常点抓取
Jun 10 Python
Python使用OpenPyXL处理Excel表格
Jul 02 #Python
解决keras GAN训练是loss不发生变化,accuracy一直为0.5的问题
Jul 02 #Python
解决keras,val_categorical_accuracy:,0.0000e+00问题
Jul 02 #Python
如何基于Python爬取隐秘的角落评论
Jul 02 #Python
keras中epoch,batch,loss,val_loss用法说明
Jul 02 #Python
Python使用tkinter实现摇骰子小游戏功能的代码
Jul 02 #Python
浅谈keras使用预训练模型vgg16分类,损失和准确度不变
Jul 02 #Python
You might like
评分9.0以上的动画电影,剧情除了经典还很燃
2020/03/04 日漫
PHP Session 变量的使用方法详解与实例代码
2013/09/11 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(三)
2014/06/23 PHP
PHP使用CURL实现对带有验证码的网站进行模拟登录的方法
2014/07/23 PHP
使用php实现网站验证码功能【推荐】
2017/02/09 PHP
yii2利用自带UploadedFile实现上传图片的示例
2017/02/16 PHP
JS 判断undefined的实现代码
2009/11/26 Javascript
JavaScript控制Session操作方法
2013/01/17 Javascript
Javascript的时间戳和php的时间戳转换注意事项
2013/04/12 Javascript
Js操作Select大全(取值、设置选中等等)
2013/10/29 Javascript
Node.js中的事件驱动编程详解
2014/08/16 Javascript
引用jquery框架后出错的解决方法
2016/08/09 Javascript
Flask中获取小程序Request数据的两种方法
2017/05/12 Javascript
react-router中的属性详解
2017/06/01 Javascript
jquery与js实现全选功能的区别
2017/06/11 jQuery
Echarts之悬浮框中的数据排序问题
2018/11/08 Javascript
微信小程序自定义键盘 内部虚拟支付
2018/12/20 Javascript
vue 组件中使用 transition 和 transition-group实现过渡动画
2019/07/09 Javascript
[01:16:01]VGJ.S vs Mski Supermajor小组赛C组 BO3 第一场 6.3
2018/06/04 DOTA
python使用scrapy解析js示例
2014/01/23 Python
Python实现二叉搜索树
2016/02/03 Python
Python3 适合初学者学习的银行账户登录系统实例
2017/08/08 Python
对python打乱数据集中X,y标签对的方法详解
2018/12/14 Python
python实现二维数组的对角线遍历
2019/03/02 Python
python3中property使用方法详解
2019/04/23 Python
基于Python打造账号共享浏览器功能
2019/05/30 Python
Python数据分析pandas模块用法实例详解
2019/11/20 Python
解决pycharm 安装numpy失败的问题
2019/12/05 Python
python 通过 pybind11 使用Eigen加速代码的步骤
2020/12/07 Python
Java面试题汇总
2015/12/06 面试题
大学生毕业求职简历的自我评价
2013/10/24 职场文书
军人违纪检讨书
2014/02/04 职场文书
啦啦队口号大全
2014/06/16 职场文书
值班管理制度范本
2015/08/06 职场文书
Ajax是什么?Ajax高级用法之Axios技术
2021/04/21 Javascript
详解JAVA中的OPTIONAL
2021/06/14 Java/Android