Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python - Why exactly does numpy.sum() gives different results on the same matrix with different order, where sum() does not?

When I initialize two random matrices (here of shape (6,2)), and re-order the rows, np.sum() will give me different results. I understand this is likely due to numerical errors, as the differences are small, but how exactly is np.sum() summing the elements? How can I replicate the results for each matrix order?

x1 = np.array([[-0.31381854, -0.05944943],
               [ 0.3848904 , -0.36534384],
               [ 1.1122322 ,  1.2480698 ],
               [-1.4493011 ,  0.5094067 ],
               [ 0.00905334,  0.77591574],
               [ 0.25694364, -2.108599  ]], dtype=np.float32)

x2 = np.array([[-0.31381854, -0.05944943],
               [ 1.1122322 ,  1.2480698 ],
               [ 0.00905334,  0.77591574],
               [ 0.3848904 , -0.36534384],
               [-1.4493011 ,  0.5094067 ],
               [ 0.25694364, -2.108599  ]], dtype=np.float32)
    

print(np.sum(x1))
print(np.sum(x2))
0.0
-2.3841858e-07

Although they have the exact same elements per column and row, the sums are different.

If I sum all elements using the sum() function, the results do not disagree:

print(sum(sum(x1)))
print(sum(sum(x2)))
-5.960464477539063e-08
-5.960464477539063e-08

When I sum the columns invidividually, using the python sum() function, I get the same resulting sum:

print(sum(x1[:,0]))
print(sum(x2[:,0]))
-6.705522537231445e-08
-6.705522537231445e-08

print(sum(x1[:,1]))
print(sum(x2[:,1]))
-2.9802322387695312e-08
-2.9802322387695312e-08

But again, if I sum the columns separately, this time using np.sum( axis=1), the results are different:

print(np.sum(x1),1)
print(np.sum(x2),1)
-2.3841858e-07 1
0.0 1

This is an issue for large matrices with thousands of elements, where the numerical inaccuracies sum up to be massive differences.

I just don't understand how np.sum() is operating, in a way that a simple sum is giving such different results, where the python canonical sum() function does not!

question from:https://stackoverflow.com/questions/65906908/why-exactly-does-numpy-sum-gives-different-results-on-the-same-matrix-with-dif

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

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...