0

Hello I'd like to use this autocorrelation script, I found here:

https://stackoverflow.com/a/20463466/8238271

import numpy
def acf(series):
    n = len(series)
    data = numpy.asarray(series)
    mean = numpy.mean(data)
    c0 = numpy.sum((data - mean) ** 2) / float(n)

    def r(h):
        acf_lag = ((data[:n - h] - mean) * (data[h:] - mean)).sum() / float(n) / c0
        return round(acf_lag, 3)
    x = numpy.arange(n) # Avoiding lag 0 calculation
    acf_coeffs = map(r, x)
    return acf_coeffs

I am using this numpy array:

a=numpy.array([1, 2, 3,7,2,5,1,6,7,2,1,1,1,6,7,2])

And I am calling this code like this:

z2=acf(a)
print (z2)

Then the terminal returns:

<map object at 0x ....> (not sure what the number is so better not write down here)

How can I call this function for autocorrelation lags 1,2,3,4 and such correctly?

Please help!

Jason
  • 3
  • 2

1 Answers1

3

I'm assuming you want a list returned instead.

To do that, change this line:

acf_coeffs = map(r, x)

To this:

acf_coeffs = list(map(r, x))

Explanation: The code you copied was probably written for Python 2. The map function was changed in Python 3 to return a map object instead of a list. Thus, you wrap it in a list call to get a list.

Remolten
  • 2,614
  • 2
  • 25
  • 29