2

I made code that calculates the average value for each element in period p for array y

import numpy as np
p=4

y =np.asarray([146, 96, 59, 133, 192, 127, 79, 186, 272, 155, 98, 219])

c=len(y)/p
print c

a=[]
for i in range(1,c+1):
    s=y[p*(i-1):p*i]/np.mean(y[p*(i-1):p*i])
    a = np.append(a, s)
print a

b=[]
for i in range(c+1):
    s = np.mean(a[i::p])
    b = np.append(b, s)

print b

Is there a more efficient way to do this instead of using append and the for loops? I do not need both arrays just b

5
  • To get rid of the append, allocate two arrays of ints using np.zeros(shape), then assign to the relevant index and you should see a performance boost. Also, what's the end goal of this script? That might help us give you a hand... Commented Jan 1, 2014 at 19:21
  • Probably, second range should be also in range(p), as you iterate modulo p: a[i::p]. I updated answer, last result equals your expected b, but is one item longer, since c=3 but p=4. Commented Jan 1, 2014 at 19:34
  • 1
    @MadisonMay adding Holt-Winters exponential smoothing, this is used for calculating the seasons Commented Jan 1, 2014 at 19:37
  • @user3084006 see updates in my answer Commented Jan 1, 2014 at 19:57
  • 1
    @alko That is what I am working on. But for Statsmodel. I got all the double and single down with all the multiplicative models and dampening variations too. I will submit it after I finish the triple and the prediction bands. Commented Jan 1, 2014 at 20:06

1 Answer 1

2

In fact, you do a 2d operation. Your first array, a, can be evalueated if a second demension added (for example with reshape):

z = y.reshape(-1, p)
w = z/z.mean(axis=1).reshape(-1,1)
print w.flatten()
# [ 1.34562212  0.88479263  0.5437788   1.22580645  1.31506849  0.86986301
#   0.54109589  1.2739726   1.46236559  0.83333333  0.52688172  1.17741935]

Your second, b, is a mean of previous result:

print w.mean(axis=0)
# [ 1.37435207,  0.86266299,  0.53725214,  1.2257328 ]

Update

As you mention exponential smoothing in comments, you might be interested in pandas or statsmodels packages to deal with timeseries. See for example pandas docs with some useful computational tools and this ER in issue tracker for some useful links about exponential smoothing implementation.

Sign up to request clarification or add additional context in comments.

2 Comments

I am basing the calculations method from itl.nist.gov/div898/handbook/pmc/section4/pmc435.htm and using the data from adorio-research.org/wordpress/?p=1230 But it seems like I am getting the wrong numbers for S (first 4 numbers for the seasonal index) or the calculation method at adorio-research might be wrong
if y has an remainder can is the best way to coerce it to work in the reshape function to do an if statement then w = z[:len(y) % p*-1].reshape(-1, p)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.