python - NumPy right_shift 给出全零
问题描述
我有
a = np.array([300, 320, 616], dtype=np.uint16)
b = np.right_shift(a, 8, dtype=np.uint8)
这导致全零b
。你能解释一下这种行为吗?
解决方案
您看到此行为的原因是因为您使用的是 uint8。将其更改为 uint16 以获得所需的结果。
https://docs.scipy.org/doc/numpy-1.13.0/user/basics.types.html
基本上,在 uint8 中,数字被视为最大数字 256 的模数。所以 300 变为 300%256=44
In: np.right_shift(a, 0, dtype=np.uint8)
Out: array([ 44, 64, 104], dtype=uint8)
for i in range(9):
print(np.right_shift(a, i, dtype=np.uint8))
will print the below
[ 44 64 104]
[22 32 52]
[11 16 26]
[ 5 8 13]
[2 4 6]
[1 2 3]
[0 1 1]
[0 0 0]
[0 0 0]
推荐阅读
- python - 如何将大于 9 的数字传递给 psycopg2 选择语句
- python - 具有不必要特征的图像中的细胞计数
- c++ - 将对 main() 中的局部变量的引用传递给其他线程/函数是不好的做法吗?
- api - 如何从 API 检索沃尔玛产品
- api - 无法使用邮递员创建史诗
- python - 如果我已经打乱了一个数组`a`来获得一个数组`b`,我怎样才能获得一个索引数组'`i`使得`b [i] == a`?
- python-3.x - 我们如何从多字典创建 pyspark 数据框
- wordpress - WooCommerce/Elementor 分页更改整个页面
- javascript - 如何将项目添加到承诺之外的数组
- java - 在 AWS Lambda 中创建数据库服务