Spaces:
Runtime error
Runtime error
File size: 2,188 Bytes
c97655d aabad01 c97655d aabad01 c97655d 0040742 c97655d 0040742 c97655d 0040742 c97655d 0040742 c97655d 0040742 c97655d 0040742 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
import pandas as pd
from dateutil.relativedelta import relativedelta
import matplotlib.pyplot as plt
from prophet.serialize import model_from_json
def get_forecast(serie: str, periods, percent_change: int):
if serie == 'SOM':
model_file, data_file = 'som_model.json', 'som_data.csv'
elif serie == 'Volumen':
model_file, data_file = 'volumen_model.json', 'volumen_data.csv'
else:
raise ValueError('Input not valid')
# load model files
with open('models/' + model_file, 'r') as fin:
model = model_from_json(fin.read())
history = pd.read_csv('data/' + data_file, encoding = 'utf-8-sig', sep = ';')
history['ds'] = pd.to_datetime(history['ds'])
# make future dataframe
future = model.make_future_dataframe(periods = periods, freq = 'MS')
future = future.tail(periods)
# filter prices last year
last_year_dates = future.ds.apply(lambda x: x - relativedelta(years=1))
past_prices = history[history['ds'].isin(last_year_dates)]['precio_hl'].values
# generate new_prices
percent_change = percent_change / 100
new_prices = past_prices * (1 + percent_change)
future['precio_hl'] = new_prices
# prediction
forecast = model.predict(future)
future_values = forecast[['ds', 'yhat']]
# arrange dataframe
df_future = future_values.rename(columns = {'ds': 'Date', 'yhat': serie}).copy()
df_future['Date'] = df_future['Date'].apply(lambda x: x.date())
df_future[serie] = df_future[serie].apply(lambda x: round(x, 4))
# aux to plot
last_obs = history.iloc[-1:][['ds', 'y']].rename(columns = {'y': 'yhat'})
future_aux = pd.concat([last_obs, future_values])
# plot
fig = plt.figure()
#plt.plot(history['ds'], history['y'], label = 'Historic data', marker = '.', color = 'C0')
plt.plot(future_aux['ds'], future_aux['yhat'], label = 'Forecast', marker = '.', color = 'C1')
plt.plot(history['ds'], history['y'], label = 'Historic data', marker = '.', color = 'C0')
plt.xticks(rotation = 45)
#plt.xlabel('Date')
plt.ylabel(serie)
plt.tight_layout()
plt.legend()
return fig, df_future |