我正在用python开发一个预测模型。我想用一张图来绘制这个图,但我用的是一张图。在
绘图代码
from pandas import read_csv
from pandas.core import datetools
from matplotlib import pyplot
from statsmodels.tsa.arima_model import ARIMA
from sklearn.metrics import mean_squared_error
def parser(x):
return datetools('190' + x, '%Y-%m')
series = read_csv('shampoo-sales.csv', header=0, parse_dates=[0], index_col=0, squeeze=True, date_parser=parser())
x = series.values
size = int(len(x) * 0.66)
train, test = x[0:size], x[size:len(x)]
history = [x for x in train]
predictions = list()
for t in range(len(test)):
model = ARIMA(history, order=(5, 1, 0))
model_fit = model.fit(disp=0)
output = model_fit.forecast()
yhat = output[0]
predictions.append(yhat)
obs = test[t]
history.append(obs)
print('predicted=%f, expected=%f' % (yhat, obs))
error = mean_squared_error(test, predictions)
print('Test MSE: %.3f' % error)
# plot
pyplot.plot(test)
pyplot.plot(predictions, color='red')
pyplot.show()
错误
^{pr2}$CSV
"Month","Sales of shampoo over a three year period"
"1-01",266.0
"1-02",145.9
"2-05",191.4
"2-06",287.0
Sales of shampoo over a three year period
也许你应该像这样加上x参数
当您编写
read_csv(...., date_parser=parser())
时,parser
函数被调用。由于parser
需要一个参数x
,因此得到了TypeError。在您需要传递函数,而不调用它:
相关问题 更多 >
编程相关推荐