File size: 986 Bytes
03804e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# arima_forecast.py
import pandas as pd
from statsmodels.tsa.arima.model import ARIMAResults
from sklearn.metrics import mean_absolute_percentage_error

def load_model(path="best_arima_model.pkl"):
    return ARIMAResults.load(path)

def load_timeseries(df, comm_code):
    monthly_cols = [col for col in df.columns if col.startswith('INDX')]
    df_filtered = df[df['COMM_CODE'] == comm_code]
    ts_values = df_filtered[monthly_cols].values.flatten()
    ts = pd.Series(ts_values, index=pd.date_range(start='2012-04-01', periods=len(monthly_cols), freq='MS'))
    return ts

def forecast(comm_code, forecast_months=6, csv_path="data.csv"):
    df = pd.read_csv(csv_path)
    ts = load_timeseries(df, comm_code)
    train = ts[:-forecast_months]
    test = ts[-forecast_months:]
    
    model = load_model()
    forecast = model.forecast(steps=forecast_months)
    forecast.index = test.index

    mape = mean_absolute_percentage_error(test, forecast)
    return forecast, test, mape