所以我正在尝试使用numpy.ma.where
为我创建一个数组,就像numpy.where
函数一样。该where
函数广播我的列数组,然后用零替换一些元素。我得到以下信息:
>>> import numpy
>>> condition = numpy.array([True,False, True, True, False, True]).reshape((3,2))
>>> print (condition)
[[ True False]
[ True True]
[False True]]
>>> broadcast_column = numpy.array([1,2,3]).reshape((-1,1)) # Column to be broadcast
>>> print (broadcast_column)
[[1]
[2]
[3]]
>>> numpy.where(condition, broadcast_column, 0) \
... # Yields the expected output, column is broadcast then condition applied
array([[1, 0],
[2, 2],
[0, 3]])
>>> numpy.ma.where(condition, broadcast_column, 0).data \
... # using the ma.where function yields a *different* array! Why?
array([[1, 0],
[3, 1],
[0, 3]], dtype=int32)
>>> numpy.ma.where(condition, broadcast_column.repeat(2,axis=1), 0).data \
... # The problem doesn't occur if broadcasting isnt used
array([[1, 0],
[2, 2],
[0, 3]], dtype=int32)
非常感谢您的帮助!
我的 numpy 版本是 1.6.2