Search code examples
numpyfill

filling numpy array with mean values form data


Why does this produce an array of zeros for q, instead of the actualy value or err which is 0.0159?

errors=np.loadtxt('data').T[2]
err=np.mean(errors)
q=np.empty(99)
q.fill(err)

Solution

  • You haven't shown us enough code to reproduce the problem. Normally, q.fill would work as expected:

    import numpy as np
    
    err = 0.0159
    q = np.empty(9)
    q.fill(err)
    print(q)
    # [ 0.0159  0.0159  0.0159  0.0159  0.0159  0.0159  0.0159  0.0159  0.0159]
    

    However, (taking a stab in the dark), if q has an integer dtype, then q.fill(0.0159) would floor 0.0159 down to the nearest int and thus q would become filled with zeros:

    err = 0.0159
    q = np.empty(9, dtype='int')
    q.fill(err)
    print(q)
    # [0 0 0 0 0 0 0 0 0]