【问题标题】:sklearn.cross_validation raises IndexError: arrays used as indices must be of integer (or boolean) typesklearn.cross_validation 引发 IndexError:用作索引的数组必须是整数(或布尔)类型
【发布时间】:2016-03-17 13:28:38
【问题描述】:

我正在努力处理从sklearn.cross_validation 提出的IndexError。我解决了这个问题,似乎是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

如何解决?

【问题讨论】:

    标签: python scikit-learn


    【解决方案1】:

    我终于解决了这个问题。

    只需添加.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
    

    【讨论】:

      猜你喜欢
      • 2017-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-13
      • 2021-08-31
      相关资源
      最近更新 更多