1

对于我当前的分类任务,我对访问单个类的输入特征很感兴趣,这样每个类都只在其输入特征上进行训练(弱分类器),然后对它们进行集成。

我在访问这些功能时遇到了挑战。承认,我总是对多维数组感到困惑。我将举例说明我如何尝试在以下 MWE 中访问类功能。

import keras
import numpy as np
from sklearn.model_selection import train_test_split

Data = np.random.randn(20, 1, 5, 4)
x,y,z = np.repeat(0, 7), np.repeat(1, 7), np.repeat(2, 6)
labels = np.hstack((x,y,z))

LABELS= list(set(np.ndarray.flatten(labels)))
Class_num = len(LABELS)

trainX, testX, trainY, testY = train_test_split(Data, 
                      labels, test_size=0.20, random_state=42)

#...to categorical
trainY = keras.utils.to_categorical(trainY, num_classes=Class_num)
testY = keras.utils.to_categorical(testY, num_classes=Class_num)

ensemble = []
for i in range(len(LABELS)):
    print('Train on class ' ,LABELS[i])
    sub_train = trainX[trainY == i]
    sub_test = testX[testY == i]

    #model fit follows...

错误:

Train on class  0

---------------------------------------------------------------------------

IndexError                                Traceback (most recent call last)

<ipython-input-11-52ceeb9a1011> in <module>()
     20 for i in range(len(LABELS)):
     21     print('Train on class ' ,LABELS[i])
---> 22     sub_train = trainX[trainY == i]
     23     sub_test = testX[testY == i]
     24 

IndexError: boolean index did not match indexed array along dimension 1; dimension is 1 but corresponding boolean dimension is 3

显然,我做错了数组索引。注意 的形状trainX/testX

4

1 回答 1

1

使用argmax(axis=1).

在您的代码中,您调用函数to_categoricalon trainY。这为您提供了一个形状数组,(16, 3)其中3是类的数量:

[[0. 1. 0.]
 [1. 0. 0.]
 [0. 1. 0.]
 [1. 0. 0.]
 [0. 0. 1.]

使用argmax(axis=1)为您提供此转换后的类 ID [1 0 1 0 2 2 1 0 1 2 0 1 1 1 2 0]:.

您在这里需要做的就是将第 22 行和第 23 行更改为:

    sub_train = trainX[trainY.argmax(axis=1) == i]
    sub_test = testX[testY.argmax(axis=1) == i]
于 2020-07-18T02:05:47.113 回答