Skip to content

Instantly share code, notes, and snippets.

@geniusnhu
Created March 5, 2020 09:09
Show Gist options
  • Save geniusnhu/26e6440ba881b8542b7bcc906ac4ec1f to your computer and use it in GitHub Desktop.
Save geniusnhu/26e6440ba881b8542b7bcc906ac4ec1f to your computer and use it in GitHub Desktop.
Time Series plot
auto_cor = sales.groupby("Date")["Weekly_Sales"].sum()
auto_cor = pd.DataFrame(auto_cor)
auto_cor.columns = ["y"]
# Adding the lag of the target variable from 1 steps back up to 52 (due to a seasonality at the end of the year)
for i in range(1, 53):
auto_cor["lag_{}".format(i)] = auto_cor.y.shift(i)
# Compute autocorrelation of the series and its lags
lag_corr = auto_cor.corr()
lag_corr = lag_corr.iloc[1:,0]
lag_corr.columns = ["corr"]
order = lag_corr.abs().sort_values(ascending = False)
lag_corr = lag_corr[order.index]
# Plot the Autocorrelation
plt.figure(figsize=(12, 6))
lag_corr.plot(kind='bar')
plt.grid(True, axis='y')
plt.title("Autocorrelation")
plt.hlines(y=0, xmin=0, xmax=len(lag_corr), linestyles='dashed')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment