Hello coders!! In this article, we will be discussing autocorrelation in Python. We use autocorrelation to measure a set of current values against past values to see if they correlate. It is primarily used to do time series analysis and forecasting. Let us learn about this topic in detail.

## What is autocorrelation plot in Python?

**Autocorrelation plots** are a common tool used to check the randomness in a given data set. It is primarily used to do time series analysis and forecasting. It is used to summarize a relationship’s strength with observation in a time series with observations at prior time steps graphically.

**Characteristics Of Autocorrelation Plot **in Python:

- Varies from +1 to -1.
**+1:**if the time series one increases in value the time series 2 also increases**-1:**If the time series one increases in value the time series 2 decreases

## Syntax:

matplotlib.pyplot.acorr(x, *, data=None, **kwargs)

## Parameters:

**x:**a**detrend:**optional parameter.**Default value:**mlab.detrend_none.

**normed:**optional parameter having bool value.**Default value:**True.

**usevlines:**optional parameter having bool value.**Default value:**True.

**maxlags:**optional parameter having an integer value.**Default value:**10

**linestyle:**optional parameter used to plot the data points when usevlines is False.**marker:**optional parameter having string value.**Default value:**‘o’

## Example:

```
import matplotlib.pyplot as plt
import numpy as np
data = np.array([1.0,20,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0,15.0,16.0,17.0,18.0,19.0,20.0])
plt.title("Autocorrelation Plot")
plt.xlabel("Lags")
plt.acorr(data, maxlags = 10)
plt.show()
```

**Output:**

In this example, we have first created our data as an array of elements using the NumPy module of Python. We then specified the title and the label of our graph. Lastly, we used the acorr() method of the matplotlib module to draw the python autocorrelation plot of the defined data.

```
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(30)
data = np.random.randn(20)
plt.title("Autocorrelation Plot")
plt.xlabel("Lags")
plt.acorr(data, maxlags = 10)
plt.show()
```

**Output:**

In this example, we used the random function to get the data and used that data to plot our autocorrelation plot in Python.

## Python autocorrelation of time series:

For this, we will be using the minimum daily temperatures dataset.

```
from pandas import read_csv
from matplotlib import pyplot
data = read_csv('data.csv', header=0, index_col=0)
data.plot()
pyplot.show()
```

When we execute the above code, it creates a line plot of the time series.

Now, we will use the plot_acf() to calculate and plot the autocorrelation plot for the Minimum Daily Temperature.

```
from statsmodels.graphics.tsaplots import plot_acf
plot_acf(data)
pyplot.show()
```

Executing the above code will create a 2D plot showing the lag value along the x-axis and the correlation on the y-axis between -1 and 1.

## Fastest way to autocorrelation large arrays python:

** numpy.correlate()** can be used to determine the cross correlation between two 1D sequences.

#### Syntax:

numpy.correlate(a, v, mode = ‘valid’)

#### Parameters:

**a,v:**Input sequences**mode:**convolve docstring

#### Return Value:

Discrete cross-correlation of a and v

```
import numpy as np
a = [1,2,3]
v = [4,5,6]
print(np.correlate(a, v, "same"))
```

**Output:**

[17 32 23]

In this example we have used the correlate() method to compute the correlation which is generally defined in signal processing texts:** c_{av}[k] = sum_n a[n+k] * conj(v[n])**

## Application of Python Autocorrelation:

- Pattern recognition
- Estimating pitch
- Signal detection
- Technical analysis of stocks
- Signal processing

## Must Read

- Fibonacci series in Python
- Using Pandas to CSV() with Perfection
- Sep in Python
- cPickle in Python Explained With Examples

## Conclusion:

With this, we come to an end with this article. We learned about the python autocorrelation plot in detail. We learned its requirement, syntax, and also the applications of the same.

However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

*Happy Pythoning!*