stinoco commited on
Commit
0040742
·
1 Parent(s): 54df5db

added dataframe to demo

Browse files
Files changed (2) hide show
  1. app.py +4 -1
  2. get_forecast.py +10 -5
app.py CHANGED
@@ -21,12 +21,15 @@ with gr.Blocks() as demo:
21
  periods = gr.Slider(minimum = 1, maximum = 12, step = 1, value = 3, label = 'Months')
22
  percent_change = gr.Slider(minimum = -100, maximum = 100, step = 5, value = -5, label = '% Change vs Last Year')
23
 
24
- plot = gr.Plot()
 
 
25
 
26
  serie.change(get_forecast, [serie, periods, percent_change], plot, queue=False)
27
  periods.change(get_forecast, [serie, periods, percent_change], plot, queue=False)
28
  percent_change.change(get_forecast, [serie, periods, percent_change], plot, queue=False)
29
  plot.change(get_forecast, [serie, periods, percent_change], plot, queue=False)
 
30
  demo.load(get_forecast, [serie, periods, percent_change], plot, queue=False)
31
 
32
  demo.launch(debug = False)
 
21
  periods = gr.Slider(minimum = 1, maximum = 12, step = 1, value = 3, label = 'Months')
22
  percent_change = gr.Slider(minimum = -100, maximum = 100, step = 5, value = -5, label = '% Change vs Last Year')
23
 
24
+ with gr.Row():
25
+ dataframe = gr.Dataframe(label = 'Predicted values')
26
+ plot = gr.Plot()
27
 
28
  serie.change(get_forecast, [serie, periods, percent_change], plot, queue=False)
29
  periods.change(get_forecast, [serie, periods, percent_change], plot, queue=False)
30
  percent_change.change(get_forecast, [serie, periods, percent_change], plot, queue=False)
31
  plot.change(get_forecast, [serie, periods, percent_change], plot, queue=False)
32
+ dataframe.change(get_forecast, [serie, periods, percent_change], [plot, dataframe], queue=False)
33
  demo.load(get_forecast, [serie, periods, percent_change], plot, queue=False)
34
 
35
  demo.launch(debug = False)
get_forecast.py CHANGED
@@ -27,20 +27,25 @@ def get_forecast(serie: str, periods, percent_change: int):
27
  past_prices = history[history['ds'].isin(last_year_dates)]['precio_hl'].values
28
 
29
  # generate new_prices
30
-
31
- percent_change = percent_change / 100
32
-
33
  new_prices = past_prices * (1 + percent_change)
34
-
35
  future['precio_hl'] = new_prices
36
 
 
37
  forecast = model.predict(future)
38
 
39
  future_values = forecast[['ds', 'yhat']]
 
 
 
 
 
40
 
 
41
  last_obs = history.iloc[-1:][['ds', 'y']].rename(columns = {'y': 'yhat'})
42
  future_aux = pd.concat([last_obs, future_values])
43
 
 
44
  fig = plt.figure()
45
  #plt.plot(history['ds'], history['y'], label = 'Historic data', marker = '.', color = 'C0')
46
  plt.plot(future_aux['ds'], future_aux['yhat'], label = 'Forecast', marker = '.', color = 'C1')
@@ -51,4 +56,4 @@ def get_forecast(serie: str, periods, percent_change: int):
51
  plt.tight_layout()
52
  plt.legend()
53
 
54
- return fig
 
27
  past_prices = history[history['ds'].isin(last_year_dates)]['precio_hl'].values
28
 
29
  # generate new_prices
30
+ percent_change = percent_change / 100
 
 
31
  new_prices = past_prices * (1 + percent_change)
 
32
  future['precio_hl'] = new_prices
33
 
34
+ # prediction
35
  forecast = model.predict(future)
36
 
37
  future_values = forecast[['ds', 'yhat']]
38
+
39
+ # arrange dataframe
40
+ df_future = future_values.rename(columns = {'ds': 'Date', 'yhat': serie}).copy()
41
+ df_future['Date'] = df_future['Date'].apply(lambda x: x.date())
42
+ df_future[serie] = df_future[serie].apply(lambda x: round(x, 4))
43
 
44
+ # aux to plot
45
  last_obs = history.iloc[-1:][['ds', 'y']].rename(columns = {'y': 'yhat'})
46
  future_aux = pd.concat([last_obs, future_values])
47
 
48
+ # plot
49
  fig = plt.figure()
50
  #plt.plot(history['ds'], history['y'], label = 'Historic data', marker = '.', color = 'C0')
51
  plt.plot(future_aux['ds'], future_aux['yhat'], label = 'Forecast', marker = '.', color = 'C1')
 
56
  plt.tight_layout()
57
  plt.legend()
58
 
59
+ return fig, df_future