sklearn.cross_validation引发IndexError:用作索引的数组必须是整数(或布尔)类型

时间:2016-03-17 13:28:38

标签: python scikit-learn

我在IndexError筹集sklearn.cross_validation时遇到了困难。我解决了这个问题,似乎是sklearn的错误。

我在ipython并行引擎上运行以下代码:

kf = cross_validation.KFold(num_data, k_fold)
for k, (train, test) in enumerate(kf):
    # do something

并发生错误

IndexError: arrays used as indices must be of integer (or boolean) type

发生的地方错误是:

def _iter_test_masks(self):
    """Generates boolean masks corresponding to test sets.

    By default, delegates to _iter_test_indices()
    """
    for test_index in self._iter_test_indices():
        test_mask = self._empty_mask()
        test_mask[test_index] = True # <============= Here error occurs
        yield test_mask

如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

我终于解决了这个问题。

只需添加.astype('int64)

即可
def _iter_test_masks(self):
    """Generates boolean masks corresponding to test sets.

    By default, delegates to _iter_test_indices()
    """
    for test_index in self._iter_test_indices():
        test_mask = self._empty_mask()
        test_mask[test_index.astype('int64')] = True  # convert to int type
        yield test_mask