RandomForestClassifier prediction_proba(X) 只输出一个类别的结果

RandomForestClassifier prediction_proba(X) outputs results only for one category

我运行一些模拟使用RandomForestClassifier在2个类别之间进行分类,我正在使用下面的函数进行调试:

def predict_proba(self, X):

    print(X.shape)

    pred = self.clf.predict_proba(X)

    print(pred.shape)

    pred = pred.T[1]

    print(pred.shape)

    return pred

这个例程运行了几次,因为我已经拆分了我的数据,我得到了打印 .shape 我在上面编码的例程的后续输出。

(62, 93)
(62, 2)
(62,)

(62, 93)
(62, 2)
(62,)

(62, 93)
(62, 2)
(62,)

(62, 93)
(62, 1)
IndexError: index 1 is out of bounds for axis 0 with size 1

我的问题是为什么 prediction_proba(X) 大多数时候输出 (62, 2) 的形状(如预期的那样)而其他时间输出 (62, 1) 的形状?

编辑

我想我明白了,好像那个分类器只训练了一个类别,因为我没有使用 Stratified KFold 或类似的东西。

我已经解决了这个问题。发生这种情况是因为我将数据分成块,其中一些块在可能的 2 个类别中只有 1 个类别,因此 RandomForestClassifier.fit() 仅在 1 个类别中。

我用sklearn.model_selection.StratifiedKFold解决了它。