Pandas

Pandas中怎么进行ARIMA模型拟合

小亿
94
2024-05-13 10:46:52
栏目: 编程语言

Pandas本身并不提供ARIMA模型的实现,但可以使用statsmodels库来进行ARIMA模型的拟合。下面是一个简单的示例代码:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from statsmodels.tsa.arima.model import ARIMA

# 生成时间序列数据
np.random.seed(0)
data = np.random.randn(100)
ts = pd.Series(data, index=pd.date_range('20210101', periods=100))

# 拟合ARIMA模型
model = ARIMA(ts, order=(1, 1, 1))
result = model.fit()

# 绘制原始数据和拟合结果
plt.figure(figsize=(12, 6))
plt.plot(ts, label='Original Data')
plt.plot(result.fittedvalues, color='red', label='ARIMA Fit')
plt.legend()
plt.show()

在上面的代码中,我们首先生成了一个随机的时间序列数据,然后使用statsmodels库中的ARIMA模型进行拟合,并将拟合结果和原始数据进行对比绘图展示。您可以根据自己的数据和需求来调整ARIMA模型的参数和拟合方法。

0
看了该问题的人还看了