问题
This code
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
Results in
<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
I can't find anything on this in Google nor in the numpy release notes. I'm assuming this is about indexing by booleans? I don't understand how this could possibly result in an error in the future, how can I fix this?
Python 2.7.6 (default, Nov 10 2013, 19:24:24) [MSC v.1500 64 bit (AMD64)] on win32
Numpy version: 1.8.0
EDIT: Added the print statement to the code to show the array is an ndarray
EDIT2: From the comments it became clear that this happens because y
is a struct array and the correct way to do the check is y['target'] > threshold
. However, y
could have multiple columns, or even different column names, is there a way to do this flexibly with structured arrays?
来源:https://stackoverflow.com/questions/26282266/numpy-deprecationwarning-on-boolean-indexing