Time Series Forecasting Using ARIMA | In terms of code

import pandas as pd
from statsmodels.tsa.arima.model import ARIMA

# 示例使用
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# 将数据转换为时间序列对象
time_series = pd.Series(data)

# 拟合ARIMA模型
model = ARIMA(time_series, order=(1, 0, 0))
model_fit = model.fit()

# 进行预测
forecast = model_fit.predict(start=len(time_series), end=len(time_series)+4)
print(forecast)

The meaning of model.fit().predict() function parameters

model.fit()The function is used to fit the ARIMA model, which estimates the parameters of the model based on the provided time series data. In this function, there is no need to specify additional parameters.

model.predict()The function is used to predict the time series, and it can be predicted on the fitted model. When making a forecast, it is necessary to specify the start time step and the end time step of the forecast. In predict()the function, there are two ways to specify the time step of the forecast:

  1. Specifies the index position of the start and end time steps of the forecast. For example, start=len(time_series)it means to start forecasting from the last time step of the time series, and end=len(time_series)+4it means to forecast to the 4th time step after the last time step of the time series.
  2. Specify the starting time step for the forecast and the number of steps for the forecast. For example, start=len(time_series)it means to predict from the last time step of the time series, steps=5which means to predict 5 time steps.

According to the specific needs, you can choose one of the methods to specify the time step of the prediction. In the sample code, we use the first method, which specifies the index position of the start time step and end time step of the prediction.

Hope this clarifies model.fit().predict()the meaning of function parameters. If you still have any questions, please feel free to ask.

To measure the forecasting accuracy of an ARIMA model, metrics such as root mean square error (RMSE) or mean absolute error (MAE) can be used. Here is sample code using root mean square error:

import pandas as pd
from statsmodels.tsa.arima.model import ARIMA
from sklearn.metrics import mean_squared_error

# 示例使用
# data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# 
data = [1.310, 1.307, 1.307, 1.307]

# 将数据转换为时间序列对象
time_series = pd.Series(data)

# 拟合ARIMA模型
model = ARIMA(time_series, order=(1, 0, 0))
model_fit = model.fit()

# 进行预测
forecast = model_fit.predict(start=len(time_series), end=len(time_series)+1)

# 计算均方根误差
actual_values = [1.307, 1.307]  # 实际值
mse = mean_squared_error(actual_values, forecast)
rmse = np.sqrt(mse)
print("均方根误差 (RMSE):", rmse)

Reference

OpenAI-ChatGPT

Guess you like

Origin blog.csdn.net/weixin_43332715/article/details/131817048