I think I would do this in two steps:
a = np.genfromtxt('meanvals2.txt').T[1]
b = np.std(a)
c = np.mean(a)
#step 1, values lower than 1 std from mean
ok = a > (c - b)
a[~ok] = c - b
#step 2, values higher than 1 std from mean
ok = a < (c + b)
a[~ok] = c + b
print a
of course, if you really want a separate array h
, you could do h = a.copy()
and then work with h
instead of a
.
Using your data as an example:
>>> a = np.array([1,2,3,4,5,20],dtype=np.float32)
>>> b = np.std(a)
>>> c = np.mean(a)
>>> print b
6.46572151487
>>> print c
5.83333333333
>>> ok = a > (c - b)
>>> a[~ok] = c - b
>>> ok = a < (c + b)
>>> a[~ok] = c + b
>>> print a
[ 1. 2. 3. 4. 5. 12.2990551]