Python Forum
Differencing Time series and Inverse after Training
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Differencing Time series and Inverse after Training
#1
Hello,

I got a non-stationary Time Series and I want to predict the target variable in the future. For simplicity, let's say that the target variable is simply a price and the features are economic factors that have an impact on that price. Currently, I am utilizing Random Forest and so far my code looks like that ( nothing special )

# Load Dataset

df = pd.read_csv("C:FINAL.csv")
df["Date"] = pd.to_datetime(df['Date'])
df = df.set_index('Date')
df.index.freq = 'MS' 

# Lag variables 

z = df
z = z.shift(4)
z = z.iloc[4:]
df = df.iloc[4:]

# Define variables 

y = df["price"].values
X = z.drop(["price"], axis = 1).values

# Train - Test Split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, shuffle=False)

# RF

rf = RandomForestRegressor(bootstrap=False, max_depth=150, max_features="sqrt", 
      min_samples_leaf=2, min_samples_split=8, n_estimators=100)
rf.fit(X_train, y_train)
y_pred = rf.predict(X_test)
R2 = r2_score(y_test, y_pred)
print('Mean Absolute Error:', mean_absolute_error(y_test, y_pred))
print("R2:", R2)
The results are reasonable, but now I want to take the first difference of the target variable, so

y = y_(t+1) - y(t)

in order to remove the trend of the time series. However, I also want to inverse this operation after training, so I have a MEA I can interpret, Because the MEA will be obviously much lower when caluclating with only the differences. Does anyone know how I can do that conveniently? I don't even know if its possible because the predictions result from forecasting and not differencing itself (i.e. there is no "original" time series for inverse differencing). Thanks in advance !
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help: Conversion of Electricity Data into Time Series Data SmallGuy 3 1,159 Oct-04-2023, 03:31 PM
Last Post: deanhystad
  Sample training small model AndrzejB 3 1,164 Mar-22-2023, 07:37 PM
Last Post: jefsummers
  Time Series Production Process Problem Mzarour 1 2,100 Feb-28-2023, 12:25 PM
Last Post: get2sid
  Is it normal so much time training for Training Custom Object Detector?? hobbyist 2 2,725 May-31-2022, 08:55 AM
Last Post: aserikova
  reduce time series based on sum condition amdi40 0 1,079 Apr-06-2022, 09:09 AM
Last Post: amdi40
  How to accumulate volume of time series amdi40 3 2,260 Feb-15-2022, 02:23 PM
Last Post: amdi40
  Recommendations for ML libraries for time-series forecast AndreasPython 0 1,862 Jan-06-2021, 01:03 PM
Last Post: AndreasPython
  Time Series forecating with multiple independent variables Krychol88 1 1,823 Oct-23-2020, 08:11 AM
Last Post: DPaul
  how to handling time series data file with Python? aupres 4 2,927 Aug-10-2020, 12:40 PM
Last Post: MattKahn13
  Changing Time Series from Start to End of Month illmattic 0 1,827 Jul-16-2020, 10:49 AM
Last Post: illmattic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020