淺談keras中的keras.utils.to_categorical用法
如下所示:
to_categorical(y, num_classes=None, dtype='float32')
將整型標(biāo)簽轉(zhuǎn)為onehot。y為int數(shù)組,num_classes為標(biāo)簽類別總數(shù),大于max(y)(標(biāo)簽從0開(kāi)始的)。
返回:如果num_classes=None,返回len(y) * [max(y)+1](維度,m*n表示m行n列矩陣,下同),否則為len(y) * num_classes。說(shuō)出來(lái)顯得復(fù)雜,請(qǐng)看下面實(shí)例。
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
補(bǔ)充知識(shí):keras筆記——keras.utils.to_categoracal()函數(shù)
keras.utils.to_categoracal (y, num_classes=None, dtype='float32')
將整形標(biāo)簽轉(zhuǎn)為onehot,y為int數(shù)組,num_classes為標(biāo)簽類別總數(shù),大于max (y),(標(biāo)簽從0開(kāi)始的)。
返回:
如果num_classes=None, 返回 len(y)*[max(y)+1] (維度,m*n表示m行n列矩陣),否則為len(y)*num_classes。
以上這篇淺談keras中的keras.utils.to_categorical用法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python實(shí)現(xiàn)unicode轉(zhuǎn)中文及轉(zhuǎn)換默認(rèn)編碼的方法
這篇文章主要介紹了python實(shí)現(xiàn)unicode轉(zhuǎn)中文及轉(zhuǎn)換默認(rèn)編碼的方法,結(jié)合實(shí)例形式分析了Python針對(duì)Unicode編碼操作的相關(guān)技巧及編碼轉(zhuǎn)換中的常見(jiàn)問(wèn)題解決方法,需要的朋友可以參考下2017-04-04
python實(shí)現(xiàn)微信打飛機(jī)游戲
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)微信打飛機(jī)游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03
python利用跳板機(jī)ssh遠(yuǎn)程連接redis的方法
今天小編就為大家分享一篇python利用跳板機(jī)ssh遠(yuǎn)程連接redis的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-02-02
淺析Python中將單詞首字母大寫(xiě)的capitalize()方法
這篇文章主要介紹了淺析Python中將單詞首字母大寫(xiě)的capitalize()方法,是Python入門(mén)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-05-05
pyenv切換不同的python版本的實(shí)現(xiàn)步驟
pyenv是一個(gè)流行的Python版本管理工具,支持在同一臺(tái)機(jī)器上安裝和切換多個(gè)Python版本,本文就來(lái)介紹一下pyenv切換不同的python版本,感興趣的可以了解一下2024-12-12
Python通過(guò)keyboard庫(kù)實(shí)現(xiàn)模擬和監(jiān)聽(tīng)鍵盤(pán)
這篇文章主要為大家詳細(xì)介紹了Python如何通過(guò)keyboard庫(kù)實(shí)現(xiàn)模擬和監(jiān)聽(tīng)鍵盤(pán),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2024-10-10
Python基礎(chǔ)之畫(huà)圖神器matplotlib
這篇文章主要介紹了python基礎(chǔ)之畫(huà)圖神器matplotlib,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們有一定的幫助,需要的朋友可以參考下2021-04-04
詳解Open Folder as PyCharm Project怎么添加的方法
這篇文章主要介紹了詳解Open Folder as PyCharm Project怎么添加的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
python pyg2plot的原理知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理的是一篇關(guān)于python pyg2plot的原理知識(shí)點(diǎn)總結(jié)內(nèi)容,有興趣的朋友們可以參考下。2021-02-02
Python OpenCV學(xué)習(xí)之圖像形態(tài)學(xué)
形態(tài)學(xué)處理方法是基于對(duì)二進(jìn)制圖像進(jìn)行處理的,卷積核決定圖像處理后的效果。本文將為大家詳細(xì)介紹一下OpenCV中的圖像形態(tài)學(xué),感興趣的可以了解一下2022-01-01

