考虑以下numpy数组:
x = np.array([2]*4, dtype=np.uint8)
这只是一个四点2的数组。
我想对这个数组执行bitwise_and还原:
y = np.bitwise_and.reduce(x)
我希望结果是:
2
因为数组的每个元素是相同的,所以连续和‘s应该产生相同的结果,但是我得到:
0
为什么会有差异?
发布于 2016-12-06 23:50:12
在reduce
文档字符串中,将解释该函数等效于
r = op.identity # op = ufunc
for i in range(len(A)):
r = op(r, A[i])
return r
问题是np.bitwise_and.identity
是1:
In [100]: np.bitwise_and.identity
Out[100]: 1
要使reduce
方法像您所期望的那样工作,标识必须是一个整数,所有位都设置为1。
上面的代码是使用numpy 1.11.2运行的。问题是修正了numpy的开发版本
In [3]: np.__version__
Out[3]: '1.13.0.dev0+87c1dab'
In [4]: np.bitwise_and.identity
Out[4]: -1
In [5]: x = np.array([2]*4, dtype=np.uint8)
In [6]: np.bitwise_and.reduce(x)
Out[6]: 2
https://stackoverflow.com/questions/41006676
复制相似问题