См. ниже код Python и выходные данные
Код: Выделить всё
from statsmodels.tsa.statespace.sarimax import SARIMAX
model = SARIMAX(quarterly_sales, order=order, seasonal_order=seasonal_order)
results = model. Fit()
forecast = results.get_forecast(steps=4)
forecast_index = pd.date_range(start='2013-01-01', periods=4, freq='Q')
forecast_series = pd.Series(forecast.predicted_mean, index=forecast_index)
print(forecast_series)
# Plot the historical quarterly sales data
quarterly_sales.plot(kind='bar', figsize=(10, 6), label='Historical Quarterly Sales')
# Check if the forecast index aligns with the expected future quarters
print(forecast_series.index)
# Overlay the forecasted sales with more visibility
plt.plot(forecast_series.index, forecast_series, color='red', marker='o', linestyle='dashed', linewidth=2, label='Forecasted Quarterly Sales')
plt.ylim(0, max(quarterly_sales.max(), forecast_series.max()) * 1.1)
plt.title('Quarterly Sales with Forecast')
plt.xlabel('Quarter')
plt.ylabel('Sales')
plt.xticks(rotation=45)
plt.legend()
plt.show()
new_index = [f"Q{date.quarter} {str(date.year)[-2:]}" for date in forecast_series.index]
forecast_series.index = new_index
import matplotlib.pyplot as plt
historical_index = [f"Q{date.quarter} {str(date.year)[-2:]}" for date in quarterly_sales.index]
quarterly_sales.index = historical_index
quarterly_sales.plot(kind='bar', figsize=(10, 6), label='Historical Quarterly Sales')
plt.plot(forecast_series.index, forecast_series, color='red', marker='o', linestyle='dashed', label='Forecasted Quarterly Sales')
plt.title('Quarterly Sales with Forecast')
plt.xlabel('Quarter')
plt.ylabel('Sales')
plt.xticks(rotation=45)
plt.legend()
plt.show()
Подробнее здесь: https://stackoverflow.com/questions/783 ... hon-sarima