python - NumPy 点积根据数组 dtype 给出两个不同的结果

标签 python numpy linear-algebra

我有一些灰度图像数据 (0-255)。根据 NumPy dtype,我得到不同的点积结果。例如,x0x1 是同一张图片:

>>> x0
array([0, 0, 0, ..., 0, 0, 0], dtype=uint8)
>>> x1
array([0, 0, 0, ..., 0, 0, 0], dtype=uint8)
>>> (x0 == x1).all()
True
>>> np.dot(x0, x1)
133
>>> np.dot(x0.astype(np.float64), x1.astype(np.float64))
6750341.0

我知道第二个点积是正确的,因为它们是同一幅图像,余弦距离应该为 0:

>>> from scipy.spatial import distance
>>> distance.cosine(x0, x1)
0.99998029729164795
>>> distance.cosine(x0.astype(np.float64), x1.astype(np.float64))
0.0

当然,点积应该适用于整数。对于小型阵列,它会:

>>> v = np.array([1,2,3], dtype=np.uint8)
>>> v
array([1, 2, 3], dtype=uint8)
>>> np.dot(v, v)
14
>>> np.dot(v.astype(np.float64), v.astype(np.float64))
14.0
>>> distance.cosine(v, v)
0.0

发生了什么。为什么点积会根据 dtype 给出不同的答案?

最佳答案

数据类型 uint8 被限制为 8 位,因此它只能表示值 0、1、...、255。您的点积溢出了可用的值范围,因此只有最后一个保留 8 位。最后 8 位包含值 133。您可以验证这一点:

6750341 % (2 ** 8) == 133
# True

关于python - NumPy 点积根据数组 dtype 给出两个不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44039165/

相关文章:

通过 PuTTY 进行 SSH 的 Python 脚本

python - 值错误 : matrices are not aligned

python - 有效地计算转换矩阵 (m*m) * (n*n) 的逐元素乘积以给出 (mn*mn) 矩阵

python - 加速用于计算矩阵余因子的 python 代码

python - 如何在 Django 和 AngularJS 中正确的代码?

python 检查 json.dumps 是否可行

python - 矩阵向量乘法

c++ - 线性代数的CPU指令集?

python - DRF 在覆盖 get_queryset 时抛出 django.core.exceptions.ImproperlyConfigured

python - 有效地计算数组中 N 个最小数字的总和