Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Keras np_utils.to_范畴的问题_Python_Keras - Fatal编程技术网

Python 使用Keras np_utils.to_范畴的问题

Python 使用Keras np_utils.to_范畴的问题,python,keras,Python,Keras,我正在尝试将一个整数热向量数组转换成一个keras可以用来拟合我的模型的热向量数组。以下是代码的相关部分: Y_train = np.hstack(np.asarray(dataframe.output_vector)).reshape(len(dataframe),len(output_cols)) dummy_y = np_utils.to_categorical(Y_train) 下图显示了Y\u列车和dummy\u列车实际上是什么 我找不到任何可以帮助我的to_category文档

我正在尝试将一个整数热向量数组转换成一个keras可以用来拟合我的模型的热向量数组。以下是代码的相关部分:

Y_train = np.hstack(np.asarray(dataframe.output_vector)).reshape(len(dataframe),len(output_cols))
dummy_y = np_utils.to_categorical(Y_train)
下图显示了
Y\u列车
dummy\u列车
实际上是什么

我找不到任何可以帮助我的
to_category
文档


提前感谢。

np\u utils.to\u category
用于将标记数据数组(从
0
nb\u classes-1
)转换为一个热向量

官方文件中有一个例子

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.utils.np_utils
在更新版本中不起作用;如果是,请使用:

from tensorflow.keras.utils import to_categorical
在这两种情况下

to_categorical(0, max_value_of_array)
它假设类值是字符串,您将对它们进行标签编码,因此每次从0到n个类开始。

for the same example:- consider an array of {1,2,3,4,2}

The output will be [zero value, one value, two value, three value, four value]

array([[ 0.,  1.,  0., 0., 0.],
       [ 0.,  0.,  1., 0., 0.],
       [ 0.,  0.,  0., 1., 0.],
       [ 0.,  0.,  0., 0., 1.],
       [ 0.,  0.,  1., 0., 0.]],
让我们看另一个例子:-

Again, for an array having 3 classes, Y = {4, 8, 9, 4, 9}

to_categorical(Y) will output

array([[0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0. ],
       [0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0. ],
       [0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1. ],
       [0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0. ],
       [0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1. ]]

Y_train已经是一个热门向量,您可以直接使用它,而无需使用它来分类,实际问题是什么?
nb_类
应该是
num_类
根据这里的文档:在公共API中,您无法访问np_util(它是内部的)。您应该通过utils模块访问实用程序,例如从keras import utils utils。要查看此问题:@ArayanSingh感谢您的指点,我在评论中添加了导入。
Again, for an array having 3 classes, Y = {4, 8, 9, 4, 9}

to_categorical(Y) will output

array([[0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0. ],
       [0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0. ],
       [0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1. ],
       [0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0. ],
       [0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1. ]]