【发布时间】:2018-09-15 23:39:01
【问题描述】:
我有一个使用 Keras 进行机器学习的 Python 脚本。我正在构建 X 和 Y,它们分别是特征和标签。
标签是这样构建的:
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函数解释如下:
to_categorical
keras.utils.to_categorical(y, num_classes=None)
将类向量(整数)转换为二进制类矩阵。
现在我正在使用 NumPy。您能否让我知道如何构建同一行代码才能工作?换句话说,我正在寻找 NumPy 中“to_categorical”函数的等价物。
【问题讨论】:
-
to_categorical是用纯 NumPy 编写的。您可以从中复制source code。 -
你为什么不直接使用来自github.com/keras-team/keras/blob/master/keras/utils/np_utils.py的代码来引用它的源代码?
标签: python numpy machine-learning keras