python关于numpy矩阵的赋值错误

我写了一个循环,里面有个if判断,根据判断结果给矩阵赋值data_old[i,j] = 266,结果每次赋值都矩阵那个位置的值不是266,而是10,这是为什么?

阅读 3k
1 个回答

估计你定义的数据类型的原因,如果是 uint8,则会溢出:

>>> import numpy as np
>>> a = np.array([1, 2, 3], dtype = np.uint8) 
>>> a
array([1, 2, 3], dtype=uint8)
>>> a[0]=266
>>> a
array([10,  2,  3], dtype=uint8)
>>> a.itemsize
1

uint8的取值范围是0~255, 266-256=10.
itemsize属性会返回类型字节数。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进