Python - How to output a numpy array of probabilities with certain precision and remain its sum
I have a numpy array of 6 probabilities which come from pytorch softmax function.
[0.055709425,0.04365404,0.008613999,0.0022386343,0.0037478858,0.88603604]  

I want to convert all 6 float numbers to string to represent a score output,
and all of them need to be rounded to a certain precision, say 4.
I used the following code to get the output text:
','.join(f'{x:.4f}' for x in scores)  # scores is the array above

and the output is
0.0557,0.0437,0.0086,0.0022,0.0037,0.8860

which sums up to 0.9999 instead of 1.0. And I have a bunch of arrays like this one but sums up to either 0.9999 or 1.0001.
So my question is, how do I get the output that sums up exactly to 1.0?
I know it's a floating point computation problem. What am I missing, some rounding operation or some adjustment?
Thank you very much.

You can round off to 2 decimal places, to reduce that error:
For example:
import numpy as np
a = np.array([0.055709425,0.04365404,0.008613999,0.0022386343,0.0037478858,0.88603604])
print(sum(a))

Output:
1.0000000241

Now:
new_array = [round(x,2) for x in a]
print(sum(new_array))

Output:
1.0

