Вот код подгонки модели:
Код: Выделить всё
import pandas as pd
import matplotlib.pyplot as plt
from pmdarima import auto_arima
from sklearn.metrics import mean_squared_error
import numpy as np
# Assuming your DataFrame is 'df', which contains a 'demand' column
df['demand'] = df['demand'].interpolate(method='linear') # Use interpolation to fill NaN values
# Set the frequency (adjust as needed: 'D' for daily, 'M' for monthly, etc.)
df = df.asfreq('D')
# Split the dataset into train and test sets (80% training, 20% testing)
train_size = int(len(df) * 0.8)
train, test = df[:train_size], df[train_size:]
# Use Auto ARIMA to find the best model
model = auto_arima(train['demand'],
seasonal=True, # If seasonality is present
m=12, # Set to 7 for weekly seasonality (if daily data)
d=1, # Set non-seasonal differencing
D=1, # Set seasonal differencing
trace=True, # Prints the progress
suppress_warnings=True,
stepwise=True) # Use a stepwise search
# Print the best model summary
print(model.summary())
Код: Выделить всё
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import mean_squared_error
# Calculate RMSE
rmse = np.sqrt(mean_squared_error(test['demand'], forecast))
# Create the plot
plt.figure(figsize=(10, 6))
# Plot the test (actual) demand
plt.plot(test.index, test['demand'], label='Actual Demand', color='blue')
# Plot the forecasted demand
plt.plot(forecast_index, forecast, label='Forecasted Demand', color='red')
# Add labels and title
plt.title('Actual vs Forecasted Demand (Zoomed In)')
plt.xlabel('Date')
plt.ylabel('Demand')
plt.legend()
# Display RMSE on the plot
plt.text(forecast_index[-1], test['demand'].max(), f'RMSE: {rmse:.2f}', fontsize=12, ha='right', color='black')
plt.show()
# Print the forecasted values for inspection
print(forecast)
введите здесь описание изображения.
Я пытался изменить параметр m в auto ARIMA с разными значениями, но это было плодотворно!
Подробнее здесь: https://stackoverflow.com/questions/790 ... -data-with