I'm using python 2.7 and the module called "uncertainties" to analyse data from an experiment. I have two arrays, polycoeffs and cov, that were generated by the numpy function polyfit. I've managed to pull out the leading diagonal from the cov array and I'm trying to match these values with the appropriate coefficients in a list called uncert_coeffs with the uncertainties function "ufloat". Here's the code:
polycoeffs,cov=polyfit(wav,trans,6,cov=True) #wav and trans are themselves, arrays.
print "Polycoeffs= ",polycoeffs
print "Cov= ",cov
cov_diag=[]
for element in diag(cov):
cov_diag.append(str(element))
print "The diagonal of the covariant matrix= ",cov_diag
ord_counter=6
uncert_coeffs=[]
cov_index=0
for i in polycoeffs:
uncert=(cov_diag[cov_index])
print "uncert: ",uncert
temp=ufloat("(i+/-uncert)") #error here
uncert_coeffs.append(temp)
cov_index+=1
print "The polynomial coefficients with uncertainties, are: ",uncert_coeffs
This produces the error:
ValueError: Cannot parse (i+/-uncert): was expecting a number like 1.23+/-0.1
So my question is: in these circumstances in which it would be a royal pain to combine the polycoeffs and their uncertainties by hand, how can I make ufloat unpack the variable uncert? Additionally, the values of uncert are mostly scientific notation.
You're passing in the literal string "i+/-uncert"
instead of the variable values.
Assuming you're using a recent version of uncertainties, just do:
temp = ufloat(i, uncert)
Alternately, you could do format the numerical values as their string representations:
temp = ufloat('{}+/-{}'.format(i, uncert))
However, there's no reason not to just pass in the values to ufloat
directly.