久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

添加零時奇怪的 numpy.sum 行為

Weird numpy.sum behavior when adding zeros(添加零時奇怪的 numpy.sum 行為)
本文介紹了添加零時奇怪的 numpy.sum 行為的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我了解數學上等效的算術運算如何由于數值錯誤(例如,以不同順序求和浮點數)而導致不同的結果.

I understand how mathematically-equivalent arithmentic operations can result in different results due to numerical errors (e.g. summing floats in different orders).

然而,令我驚訝的是,將零添加到 sum 會改變結果.我認為這始終適用于浮點數,無論如何:x + 0. == x.

However, it surprises me that adding zeros to sum can change the result. I thought that this always holds for floats, no matter what: x + 0. == x.

這是一個例子.我希望所有的行都完全為零.誰能解釋一下為什么會這樣?

Here's an example. I expected all the lines to be exactly zero. Can anybody please explain why this happens?

M = 4  # number of random values
Z = 4  # number of additional zeros
for i in range(20):
    a = np.random.rand(M)
    b = np.zeros(M+Z)
    b[:M] = a
    print a.sum() - b.sum()

-4.4408920985e-16
0.0
0.0
0.0
4.4408920985e-16
0.0
-4.4408920985e-16
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
2.22044604925e-16
0.0
4.4408920985e-16
4.4408920985e-16
0.0

MZ 的較小值似乎不會發生這種情況.

It seems not to happen for smaller values of M and Z.

我還確定了 a.dtype==b.dtype.

這里還有一個例子,它也展示了 python 的內置 sum 的行為符合預期:

Here is one more example, which also demonstrates python's builtin sum behaves as expected:

a = np.array([0.1,      1.0/3,      1.0/7,      1.0/13, 1.0/23])
b = np.array([0.1, 0.0, 1.0/3, 0.0, 1.0/7, 0.0, 1.0/13, 1.0/23])
print a.sum() - b.sum()
=> -1.11022302463e-16
print sum(a) - sum(b)
=> 0.0

我正在使用 numpy V1.9.2.

I'm using numpy V1.9.2.

推薦答案

簡答:你看到了兩者的區別

a + b + c + d

(a + b) + (c + d)

因為浮點數不準確所以不一樣.

which because of floating point inaccuracies is not the same.

長答案: Numpy 將成對求和作為速度(它允許更容易矢量化)和舍入誤差的優化.

Long answer: Numpy implements pair-wise summation as an optimization of both speed (it allows for easier vectorization) and rounding error.

numpy sum-implementation 可以在 here(函數pairwise_sum_@TYPE@).它基本上做了以下事情:

The numpy sum-implementation can be found here (function pairwise_sum_@TYPE@). It essentially does the following:

  1. 如果數組的長度小于 8,則執行常規的 for 循環求和.這就是為什么如果 W < 沒有觀察到奇怪的結果.4 在您的情況下 - 在兩種情況下都將使用相同的 for 循環求和.
  2. 如果長度在 8 到 128 之間,則在 8 個 bin r[0]-r[7] 中累加總和,然后通過 ((r[0] + r[1]) + (r[2] + r[3])) + ((r[4] + r[5]) + (r[6] + r[7])).
  3. 否則,它將遞歸地對數組的兩半求和.
  1. If the length of the array is less than 8, a regular for-loop summation is performed. This is why the strange result is not observed if W < 4 in your case - the same for-loop summation will be used in both cases.
  2. If the length is between 8 and 128, it accumulates the sums in 8 bins r[0]-r[7] then sums them by ((r[0] + r[1]) + (r[2] + r[3])) + ((r[4] + r[5]) + (r[6] + r[7])).
  3. Otherwise, it recursively sums two halves of the array.

因此,在第一種情況下,您會得到 a.sum() = a[0] + a[1] + a[2] + a[3] 而在第二種情況下 b.sum() = (a[0] + a[1]) + (a[2] + a[3]) 這導致 a.sum() - b.sum() != 0.

Therefore, in the first case you get a.sum() = a[0] + a[1] + a[2] + a[3] and in the second case b.sum() = (a[0] + a[1]) + (a[2] + a[3]) which leads to a.sum() - b.sum() != 0.

這篇關于添加零時奇怪的 numpy.sum 行為的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 91国语清晰打电话对白 | 久久午夜视频 | 欧美乱操| 玖玖国产| 国产精品久久久久久婷婷天堂 | 最近免费日本视频在线 | 国产精品区一区二 | 狠狠操在线 | 久久久久久久综合色一本 | 999热视频| 一级黄色影片在线观看 | 91精品国产乱码久久久久久 | 日韩毛片播放 | 免费视频一区二区 | 一区二区三区在线 | 午夜视频在线免费观看 | 亚洲日本欧美 | 国产高清视频在线 | 狠狠操电影 | 蜜桃五月天 | 99视频在线免费观看 | 国产精品伦理一区二区三区 | 免费国产视频 | 亚洲一区二区三区久久久 | 亚洲美乳中文字幕 | 二区三区在线观看 | 日韩在线不卡 | 亚洲福利在线视频 | 成人精品在线视频 | 视频在线一区二区 | av网址在线 | 久久久www成人免费精品 | 国产精品高清一区二区三区 | 少妇特黄a一区二区三区88av | 精品伊人| 玖玖在线免费视频 | 男女视频在线免费观看 | 国产黄色在线观看 | 超碰av免费| 一区二区国产精品 | 国产高清在线观看 |