简体   繁体   English

numpy数组的元素总和与总数不同

[英]Sum of elements of numpy array not same as total

I'm trying to count number of pairs and save them in two different histograms, one saves the pair in an array where the parent objects are split and the other one just saves the total, that means I have a loop that looks like this: 我试图计算对的数量并将其保存在两个不同的直方图中,一个将对保存在拆分父对象的数组中,另一个仅保存总数,这意味着我有一个如下所示的循环:

for k in range(N_parents):
    pair_hist[k, bin] +=1
    total_pair_hist[bin] +=1

where both pair_hist and total_pair as defined as, pair_hist和total_pair分别定义为

pair_hist = np.zeros((N_parents, bins.shape[0]), dtype = np.uint64)
total_pair_hist = np.zeros(bins.shape[0], dtype = np.uint64)

I'd expect that summing the elements of pair_hist across all parents (axis=0), I'd get the total histogram. 我希望将所有父母的pair_hist元素相加(轴= 0),得到总的直方图。 The funny thing is, if I take the sum of pair_hist: 有趣的是,如果我取pair_hist的总和:

onehalo_sum_ind = np.sum(pair_hist, axis = 0)

I don't get exactly total_pair_hist, but something slightly different: 我没有得到完全total_pair_hist,但有些不同:

total_pair_hist = [  287248245   448773033   695820015  1070797576  1634146741  2466680801
  3667159080  5334307986  7524739978 10206208064 13237161068 16466436715
 19231751113 20949333183 21254336387 19497450101 16459529579 13038604111
  9783826702  7006904025  4813946458  3207605915  2097437543  1355158303
   869077173   555036759   353732683   225171870   143179912           0]

pair_hist = [  287267022   448887401   696415932  1073435699  1644677789  2503693266
  3784008845  5665555755  8380564635 12201977310 17382403650 23929909625
 31103373709 36859534246 38146287402 33454446858 25689430007 18142721164
 12224099624  8035266046  5211441720  3353187036  2147027818  1370663213
   873519714   556182465   353995293   225224668   143189173           0]

Any idea of what's going on? 有什么想法吗? Thank you in advance :) 先感谢您 :)

Sorry for the late reply, but I didn't have time to work on it before. 抱歉,回复晚了,但我之前没有时间来做。 The problem was caused by numba. 问题是由numba引起的。 I was using it with the parallel=True flag to parallelise one of the loops and that caused the error. 我将它与parallel = True标志一起使用来并行化其中一个循环,这导致了错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM