You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a 2D array and there is one standard deviation that describes this array, as calculated by np.std(arr). However, UncertainQuantity requires the uncertainty to be the same size as the array. I guess this makes sense since the uncertainty applies to every element, and some arrays may have different uncertainties with different elements. Still it would be convenient to be able to code like this:
x = np.array([1,2,3]) * pq.m
y = pq.UncertainQuantity( x, pq.m, np.std(x))
print y
That doesn't work because np.std(x) isn't the same size as x. My solution is to code this:
y = pq.UncertainQuantity( x, x.units, np.zeros_like(x)+np.std(x))
Now I perform some operations on the array y:
x = np.array([1,3,1,3,1,3]) * pq.m
y = pq.UncertainQuantity( x, x.units, np.zeros_like(x)+np.std(x))
print "\ny: ", y
print "\nsum(y): ", np.sum(y)
print "\nmean(y): ", np.mean(y)
print "\nnanmean(y): ", np.nanmean(y)
print "\nmymean(y): ", np.sum(y)/np.size(y)
#+END_SRC
y: [ 1. 3. 1. 3. 1. 3.] m
+/-[ 1. 1. 1. 1. 1. 1.] m (1 sigma)
sum(y): 12.0 m
+/-2.44948974278 m (1 sigma)
mean(y): 2.0 m
nanmean(y): 2.0 m
+/-2.44948974278 m (1 sigma)
mymean(y): 2.0 m
+/-0.408248290464 m (1 sigma)
Issues seem to be
np.mean drops the uncertainty (but np.nanmean does not)
np.nanmean returns an incorrect result. The result from np.sum(y)/np.size(y) is correct.
Now I'm going to look into the code to see if I can easily fix this...
Patch for np.mean in #79 but I'm not sure how to handle np.nanmean. Although this would be very useful. Hints appreciated. I'll send another pull if I can code it up.
Activity
mankoff commentedon Mar 23, 2014
Patch for
np.meanin #79 but I'm not sure how to handlenp.nanmean. Although this would be very useful. Hints appreciated. I'll send another pull if I can code it up.mankoff commentedon Mar 24, 2014
I've implemented
nanmeanfor uncertain quantities, andnanstdfor regular quantities. They are included in the pull.