我需要使用 ARIMA 模型来预测图形的行为。我在互联网上找到了这段代码,但在我的情况下它没有给出预期的结果:
y = pm.datasets.load_wineind()
train, test = train_test_split(y, train_size=150)
# Fit your model
model = pm.auto_arima(train, seasonal=True, m=12)
# make your forecasts
forecasts = model.predict(test.shape[0]) # predict N steps into the future
# Visualize the forecasts (blue=train, green=forecasts)
x = np.arange(y.shape[0])
plt.plot(x[:150], train, c='blue')
plt.plot(x[150:], forecasts, c='green')
plt.show()
任务是使用 ARIMA 模型基于坐标(144 个坐标 x 和 y)构建预测图,并将其写入文件,然后用于预测。我发送了任务一般概念的所有代码:
import matplotlib
import random
import numpy as np
import matplotlib.pyplot as plt
import pmdarima as pm
import pandas as pd
from pmdarima.model_selection import train_test_split
import csv
from pandas import DataFrame
class Graph():
def __init__(self):
self.y = ""
self.x = ""
def Fill(self):
n = 144
xlist = []
with open("x.txt", "w") as xlist:
for i in range(n):
xlist.writelines(str(i) + '\n')
xlist.close()
ylist = []
with open("y.txt", "w") as ylist:
for i in range(n):
ylist.writelines(str(random.randint(1, 99)) + '\n')
ylist.close()
def Screen(self):
n = 144
x = np.array([])
with open("x.txt") as xlist:
for line in xlist:
x = np.loadtxt(xlist.readlines(int()))
self.fx = x
y = np.array([])
with open("y.txt") as ylist:
for line in ylist:
y = np.loadtxt(ylist.readlines(int()))
self.fy = y
plt.plot(x, y)
plt.show()
def Arima(self):
with open('y.txt', 'r') as infile, open('y.csv', 'w') as outfile:
stripped = (line.strip() for line in infile)
lines = (line.split(",") for line in stripped if line)
writer = csv.writer(outfile)
writer.writerows(lines)
y = []
with open('y.csv') as f:
for line in f:
y = np.loadtxt(f.readlines(int()))
train, test = train_test_split(y, train_size=140)
model = pm.auto_arima(train, seasonal=True, m=12)
forecasts = model.predict(test.shape[0])
x = np.arange(y.shape[0])
plt.plot(x[:140], train, c='blue')
plt.plot(x[140:], forecasts, c='green')
plt.show()
gra = Graph()
gra.Fill()
gra.Screen()
gra.Arima()
我假设我需要制作自己的数据集,用于预测的数据来自哪里,但我不知道该怎么做。
这个程序制作这些图表:
和这个: