5

这段代码

import numpy as np

def some_method(y, threshold):
    print type(y), y.shape, y.dtype 
    c = np.zeros(y.shape)
    c[y > threshold] = 1

结果是

<type 'numpy.ndarray'> (484L,) [('target', '<f8')]
DeprecationWarning: using a boolean instead of an integer will result in an error in the future
  c[y > threshold] = 1

我在 Google 和 numpy 发行说明中都找不到这方面的任何内容。我假设这是关于布尔索引?我不明白这怎么可能导致将来出现错误,我该如何解决这个问题?

Python 2.7.6(默认,2013 年 11 月 10 日,19:24:24)[MSC v.1500 64 位 (AMD64)] 在 win32 上

numpy 版本:1.8.0

编辑:将打印语句添加到代码中以显示数组是 ndarray

EDIT2:从评论中可以清楚地看出,发生这种情况是因为y它是一个结构数组,并且进行检查的正确方法是y['target'] > threshold. 但是,y可能有多个列,甚至不同的列名,有没有办法使用结构化数组灵活地做到这一点?

4

0 回答 0