Numpy DeprecationWarning on boolean indexing

十年热恋 提交于 2019-12-10 14:18:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!