NumPy相当于Keras函数utils.to_categorical [英] NumPy equivalent to Keras function utils.to_categorical

查看:475
本文介绍了NumPy相当于Keras函数utils.to_categorical的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Keras进行机器学习的Python脚本.我正在建立X和Y分别是功能和标签.

I have a Python script which uses Keras for machine learning. I am building X and Y which are features and labels respectively.

标签的构建方式如下:

def main=():

   depth = 10
   nclass = 101
   skip = True
   output = "True"
   videos = 'sensor'
   img_rows, img_cols, frames = 8, 8, depth
   channel = 1 
   fname_npz = 'dataset_{}_{}_{}.npz'.format(
    nclass, depth, skip)

   vid3d = videoto3d.Videoto3D(img_rows, img_cols, frames)
   nb_classes = nclass

   x, y = loaddata(videos, vid3d, nclass,
                    output, skip)

   X = x.reshape((x.shape[0], img_rows, img_cols, frames, channel))
   Y = np_utils.to_categorical(y, nb_classes) # This needs to be changed

在Keras中使用的函数"to_categorical"的解释如下:

The used function "to_categorical" in Keras is explain as follows:

to_categorical

to_categorical

keras.utils.to_categorical(y,num_classes = None)

keras.utils.to_categorical(y, num_classes=None)

将类向量(整数)转换为二进制类矩阵.

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

现在我正在使用NumPy.您能否让我知道如何构建相同的代码行才能工作?换句话说,我正在NumPy中寻找"to_categorical"功能的等效项.

Now I am using NumPy. May you let me know how the build the same line of code in order to work? In other words, I am looking for the equivalent of the "to_categorical" function in NumPy.

推荐答案

这是一种简单的方法:

np.eye(nb_classes)[y]

这篇关于NumPy相当于Keras函数utils.to_categorical的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆