使用Keras的问题np_utils.to_categorical [英] Issues using Keras np_utils.to_categorical
本文介绍了使用Keras的问题np_utils.to_categorical的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将一个整数的单热点向量数组转换为一个单热点向量数组,keras可以使用该数组来拟合我的模型.这是代码的相关部分:
I'm trying to make an array of one-hot vector of integers into an array of one-hot vector that keras will be able to use to fit my model. Here's the relevant part of the code:
Y_train = np.hstack(np.asarray(dataframe.output_vector)).reshape(len(dataframe),len(output_cols))
dummy_y = np_utils.to_categorical(Y_train)
下面是显示Y_train
和dummy_y
实际上是什么的图像.
Below is an image showing what Y_train
and dummy_y
actually are.
我找不到任何可以帮助我的to_categorical
文档.
I couldn't find any documentation for to_categorical
that could help me.
谢谢.
推荐答案
np_utils.to_categorical
用于将标记数据的数组(从0
到nb_classes - 1
)转换为一键矢量.
np_utils.to_categorical
is used to convert array of labeled data(from 0
to nb_classes - 1
) to one-hot vector.
带有示例的官方文档.
In [1]: from keras.utils import np_utils # from keras import utils as np_utils
Using Theano backend.
In [2]: np_utils.to_categorical?
Signature: np_utils.to_categorical(y, num_classes=None)
Docstring:
Convert class vector (integers from 0 to nb_classes) to binary class matrix, for use with categorical_crossentropy.
# Arguments
y: class vector to be converted into a matrix
nb_classes: total number of classes
# Returns
A binary matrix representation of the input.
File: /usr/local/lib/python3.5/dist-packages/keras/utils/np_utils.py
Type: function
In [3]: y_train = [1, 0, 3, 4, 5, 0, 2, 1]
In [4]: """ Assuming the labeled dataset has total six classes (0 to 5), y_train is the true label array """
In [5]: np_utils.to_categorical(y_train, num_classes=6)
Out[5]:
array([[ 0., 1., 0., 0., 0., 0.],
[ 1., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 1., 0., 0.],
[ 0., 0., 0., 0., 1., 0.],
[ 0., 0., 0., 0., 0., 1.],
[ 1., 0., 0., 0., 0., 0.],
[ 0., 0., 1., 0., 0., 0.],
[ 0., 1., 0., 0., 0., 0.]])
这篇关于使用Keras的问题np_utils.to_categorical的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文