I am trying to forecast my data by Support Vector Regressor, Here is my code:

df['ds'] = pd.DatetimeIndex(df['ds'])

df.loc[(df['ds'] == '2015-03'),'y'] = None
df.loc[(df['ds'] == '2015-10'),'y'] = None
df.loc[(df['ds'] == '2016-08'),'y'] = None

df["y"] = df["y"].fillna(df["y"].max())

max_training_data = df.tail(15).iloc[0]["ds"]

df_train = df.loc[df["ds"] <= max_training_data, :]
df_test = df.loc[df["ds"] > max_training_data, :]


df_train.set_index("ds", inplace=True)
df_test.set_index("ds", inplace=True)

train = df_train.copy()
test = df_test.copy()

scaler = MinMaxScaler()
train.loc[:,"y"] = scaler.fit_transform(train[["y"]])
test.loc[:,"y"] = scaler.fit_transform(test[["y"]])

train_data = train.values
test_data = test.values

timesteps = 3

train_data_timesteps=np.array([[j for j in train_data[i:i+timesteps]] for
                        i in range(0,len(train_data)-timesteps+1)])[:,:,0]

test_data_timesteps=np.array([[j for j in test_data[i:i+timesteps]] for
                        i in range(0,len(test_data)-timesteps+1)])[:,:,0]

x_train, y_train = train_data_timesteps[:,:timesteps-1], train_data_timesteps[:, 
x_test, y_test = test_data_timesteps[:,:timesteps-1], test_data_timesteps[:, 

model = SVR(kernel="rbf",C=10,epsilon=0.1)

model.fit(x_train, y_train[:,0])

y_train_pred = model.predict(x_train).reshape(-1,1)
y_test_pred = model.predict(x_test).reshape(-1,1)

y_train_pred = scaler.inverse_transform(y_train_pred)
y_test_pred = scaler.inverse_transform(y_test_pred)

y_train = scaler.inverse_transform(y_train)
y_test = scaler.inverse_transform(y_test)

train_timestamps = df_train.index[timesteps - 1:]
test_timestamps = df_test.index[timesteps - 1:]

# Visualizing data 
fig, ax = plt.subplots(figsize = (12, 6))

plt.plot(train_timestamps, y_train, color="blue", label="Historical Sales")
plt.plot(train_timestamps, y_train_pred, color="orange", label="Training Result")
plt.plot(test_timestamps, y_test, color="dodgerblue", label="Actual Sales")
plt.plot(test_timestamps, y_test_pred, color="green", label="Test Result")


I gave the last 2 observations as feature of the the model and predicted the third value, here is the result that I achieved:

enter image description here

In my test result, there is a shift to the right, which I think the model tries to predict the value close to the last observation. When I give higher timestamps, the training result is better, but the test result is very bad, how can I improve my model? Are there other regressor models that I can use? I already used SARIMAX, and the result was ok, but SARIMAX could not predict the spike in my test set.

  • $\begingroup$ there are so many options its ridiculous if using pythin you can use darts unit8co.github.io/darts or nixtla, if you are using R you can use modeltime (recommend this!) or fable or loads more. R is great for time series. Also this ebook on analysis is great nicolarighetti.github.io/Time-Series-Analysis-With-R but nothig beats forecasting principles and practice by Hyndman. $\endgroup$
    – Comte
    Commented Apr 12 at 15:38


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.