python - 为什么 np.add.at() 对大型数组返回错误的答案?
问题描述
我有一个大数据集 ,statistic
我statistic.shape = (1E10,)
想有效地将(总和)合并到一个零数组中,out = np.zeros(1E10)
。中的每个条目statistic
都有一个对应的索引 ,idx
它告诉我out
它属于哪个 bin。索引不是唯一的,所以我不能使用out += statistic[idx]
,因为这只会计算第一次遇到特定索引的时间。因此我使用np.add.at(out, idx, statistic)
. 我的问题是,对于非常大的数组, np.add.at() 返回错误的答案。
下面是显示此行为的示例脚本。该函数check_add()
应返回 1。
import numpy as np
def check_add(N):
N = int(N)
out = np.zeros(N)
np.add.at(out, np.arange(N), np.ones(N))
return np.sum(out)/N
n_arr = [1E3, 1E5, 1E8, 1E10]
for n in n_arr:
print('N = {} (log(N) = {}); output ratio is {}'.format(n, np.log10(n), check_add(n)))
这个例子为我返回:
N = 1000.0 (log(N) = 3.0); output ratio is 1.0
N = 100000.0 (log(N) = 5.0); output ratio is 1.0
N = 100000000.0 (log(N) = 8.0); output ratio is 1.0
N = 10000000000.0 (log(N) = 10.0); output ratio is 0.1410065408
有人可以向我解释为什么该功能会失败N=1E10
吗?
解决方案
这是一个老错误,NumPy 问题 13286。ufunc.at
为循环计数器使用了一个太小的变量。它不久前得到了修复,所以更新你的 NumPy。(该修复程序存在于 1.16.3 及更高版本中。)
推荐阅读
- firebase - Firebase 快照更改检索数据
- c++ - 尝试将我的主要功能分离到 main.cpp 时,我遇到了一个奇怪的错误 LNK2001
- javascript - Uncaught SyntaxError: Unexpected token : (jquery)
- python - Python在所有行中搜索模式,只导出有结果的行
- reactjs - 使用 react-konva 翻转图像
- sparql - 从 SPARQL 中的 COUNT 获取 MAX 结果
- php - jQuery File Upload SELECT在mysql中按画廊ID
- reactjs - Ckeditor disable auto inline 不会禁止在页面加载时选择内联
- magento - Magento 2 SQLSTATE[HY000]:一般错误:1823 无法添加外键约束
- kubernetes - 在 Google HTTPS 负载均衡器上自动生成和续订 Let's Encrypt 证书