[PYTHON] How to calculate the autocorrelation coefficient

1 This article

Explain how to calculate the autocorrelation coefficient with python.

2 methods

Automatically calculate the autocorrelation coefficient of data with a 3-day cycle. (Use plot_acf)

sample.py


#Define data.
from statsmodels.graphics.tsaplots import plot_acf
import pandas as pd
import numpy as np

dat = [
    ['07-01',1],
    ['07-02',10],
    ['07-03',20],
    ['07-04',2],
    ['07-05',11],
    ['07-06',21],
    ['07-07',3],
    ['07-08',22],
    ['07-09',32],
    ['07-10',4],
    ['07-11',23],
    ['07-12',33],
]

#Store dat in DataFrame type variable df.
df = pd.DataFrame(dat,columns=["A","B"])

print("Show df","\n",df)

fig, ax = plt.subplots(ncols=2, figsize=(15, 4))
sns.lineplot(x="A", y="B", data=df,ax=ax[0])
plot_acf(df["B"].dropna(), lags=10, zero=False,ax=ax[1]) #When calculating the autocorrelation, it cannot be calculated correctly if there is NA.

147.JPG

Recommended Posts

How to calculate the autocorrelation coefficient
How to calculate the volatility of a brand
How to use the generator
How to use the decorator
How to increase the axis
How to start the program
How to use the zip function
How to use the optparse module
How to read the SNLI dataset
How to get the Python version
How to calculate the amount of calculation learned from ABC134-D
[Python] How to import the library
How to overwrite the output to the console
How to calculate date with python
How to use the ConfigParser module
[AtCoder] How to find the binomial coefficient nCk mod.p [D --Knight]
How to use the Spark ML pipeline
How to check the version of Django
How to solve the bin packing problem
How to set the server time to Japanese time
How to manually update the AMP cache
[Linux] How to use the echo command
How to calculate Use% of df command
How to use the Linux grep command
How to get colored output to the console
How to operate Linux from the console
[Python] How to calculate MAE and RMSE
How to access the Datastore from the outside
How to use the IPython debugger (ipdb)
How to assign multiple values to the Matplotlib colorbar
How to use the C library in Python
How to specify the launch browser for JupyterLab 3.0.0
How to find the area of the Voronoi diagram
How to use MkDocs for the first time
How to specify the NIC to scan with amazon-dash
[Python] How to change the date format (display format)
How to try the friends-of-friends algorithm with pyfof
How to use the graph drawing library Bokeh
How to print debug messages to the Django console
How to use the Google Cloud Translation API
How to operate Linux from the outside Procedure
How to use the NHK program guide API
[Algorithm x Python] How to use the list
How to erase the characters output by Python
How to measure line speed from the terminal
How to get the files in the [Python] folder
How to identify the system call number ausyscall
How to Learn Kaldi with the JUST Corpus
How to find the correlation for categorical variables
How to plot autocorrelation and partial autocorrelation in python
[Numpy, scipy] How to calculate the square root of a semi-fixed definite matrix
How to find the coefficient of the trendline that passes through the vertices in Python
Read the Python-Markdown source: How to create a parser
How to use xml.etree.ElementTree
[Python] How to remove duplicate values from the list
How to create an article from the command line
How to retrieve the nth largest value in Python
How to know the port number of the xinetd service
How to use tf.data
How to get the variable name itself in python
How to use virtualenv