import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestRegressor
from skforecast.ForecasterAutoreg import ForecasterAutoreg

plt.rcParams['lines.linewidth'] = 1.5
plt.rcParams['font.size'] = 10

url='https://btlr.com/code/predict/cyclic_data.csv'
data = pd.read_csv(url, sep=',')
data['date'] = pd.to_datetime(data['date'], format='%Y-%m-%d')
data = data.set_index('date')
data = data.asfreq('MS') #month start
print(data)

regressor = RandomForestRegressor(max_depth=5, n_estimators=30, random_state=123)
# max depth of each tree, number of trees, random seed
forecaster = ForecasterAutoreg(regressor = regressor,lags = 16)
#lags - observations
forecaster.fit(y=data['money'])
predictions = forecaster.predict(12) #months

fig, ax = plt.subplots(figsize=(12, 5))
data['money'].plot(ax=ax, label='Training')
predictions.plot(ax=ax, label='Prediction')
ax.legend()

plt.grid()
plt.show()
plt.savefig('/var/www/html/predict.png')