Python 2.7 布尔索引上的Numpy不推荐警告

Python 2.7 布尔索引上的Numpy不推荐警告,python-2.7,numpy,deprecation-warning,Python 2.7,Numpy,Deprecation Warning,此代码 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 inst

此代码

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

(484L,)[('target','threshold
。但是,
y
可能有多个列,甚至不同的列名,有没有办法灵活地使用结构化数组来实现这一点?

y
不是numpy数组,因此
y>threshold
在使用s时不会返回布尔数组,而是返回单个
True
False
单个布尔值为数组编制索引时,它将转换为
0
1
。这种行为在将来会发生变化:单个布尔值或单个浮点值将引发错误,它们将不会转换为整数。抱歉,忘了指定,
y
实际上是一个numpy数组,请参见编辑。好的,我需要指定列名。但是
y
可以有多个列,有没有办法灵活地做到这一点?标量索引角情况的一个解决方法是,在它们最终被真正修复之前,对它们调用
np.asarray
。实际上在1.9中。这应该已经起作用了,因为这是一个错误(因为c也是0-d)啊,pff,nvm。你有一个结构化类型,所以比较根本不起作用。在1.9中,你应该得到一个警告,它在将来会出错。使用
y['target']>threshold