Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
420 views
in Technique[技术] by (71.8m points)

python关于numpy矩阵的赋值错误

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

估计你定义的数据类型的原因,如果是 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属性会返回类型字节数。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...