Search code examples
pythonnumpycsvfloating-pointprobability

How can I store float probabilities to a file so exactly that they sum up to 1?


I want to store a numpy array to a file. This array contains thousands of float probabilities which all sum up to 1. But when I store the array to a CSV file and load it back, I realise that the numbers have been approximated, and their sum is now some 0.9999 value. How can I fix it?

(Numpy's random choice method requires probabilities to sum up to 1)


Solution

  • Try using np.savetxt.

    import numpy as np
    
    arr = np.random.random(1000)
    arr /= arr.sum()
    np.savetxt('arr.csv', arr, delimiter=',')
    
    arr = np.loadtxt('arr.csv')
    print(arr.sum())
    # >>> 1.0