import pandas as pd
import numpy as np
import warnings
import matplotlib.pyplot as plt
warnings.simplefilter('ignore')
# Common code for display result
def show_graph(df1,df2,title):
data = pd.concat([df1, df2])
data.reset_index(inplace=True, drop=True)
for col in data.columns:
if col.lower().startswith('pred'):
data[col].plot(label=col,linestyle="dotted")
else:
data[col].plot(label=col)
plt.title(title)
plt.legend()
plt.show()
from statsmodels.tsa.ar_model import AutoReg
from random import random
def AR_model(train,test):
# fit model
model = AutoReg(train['Act'], lags=1)
model_fit = model.fit()
# make prediction
yhat=model_fit.predict(len(train), len(train) + len(test) - 1)
res=pd.DataFrame({"Pred":yhat, "Act":test["Act"].values})
return res
df_train = pd.DataFrame([x + random()*10 for x in range(1, 100)], columns=['Act']) <========
df_test = pd.DataFrame([x + random()*10 for x in range(101, 200)], columns=['Act']) <=========
df_ret = AR_model(df_train, df_test)
show_graph(df_train, df_ret, "Autoregression (AR)")
================
In the code above,
df_train = pd.DataFrame([x + random()*10 for x in range(1, 100)], columns=[‘Act’])
df_test = pd.DataFrame([x + random()*10 for x in range(101, 200)], columns=[‘Act’])
===> I want to remove random() from A code and input it as
Date Value
2022-07-24, -1.875071
2022-07-25, -3.015379
2022-07-26, -4.944132
2022-07-27, 12.053447
2022-07-28, -1.097842
To be honest, I am not familiar with the pandas module package. Someone else on here that is familiar with it can help you with that.
However, for the dates that you state, you can potentially define them as a dictionary since it appears that they are provided in date and value pair (or key / value pair) as:
Do you know how to prompt the user to input some text?
Do you understand how to convert text that represents a date, into the corresponding date object, according to some format?
If you put these pieces together, does it not solve the problem?