Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

How to Save an ARIMA Time Series Forecasting Model in Python

$
0
0

The Autoregressive Integrated Moving Average Model, or ARIMA, is a popular linear model for time series analysis and forecasting.

The statsmodels library provides an implementation of ARIMA for use in python. ARIMA models can be saved to file for later use in making predictions on new data. There is a bug in the current version of the statsmodelslibrary that prevents saved models from being loaded.

In this tutorial, you will discover how to diagnose and work around this issue.

Let’s get started.


How to Save an ARIMA Time Series Forecasting Model in Python

How to Save an ARIMA Time Series Forecasting Model in Python

Photo by Les Chatfield , some rights reserved.

Daily Female Births Dataset

First, let’s look at a standard time series dataset we can use to understand the problem with the statsmodels ARIMA implementation.

This Daily Female Births dataset describes the number of daily female births in California in 1959.

The units are a count and there are 365 observations. The source of the dataset is credited to Newton (1988).

You can learn more and download the dataset from the DataMarket website .

Download the dataset and place it in your current working directory with the filename “ daily-total-female-births.csv “.

The code snippet below will load and plot the dataset.

frompandasimportSeries frommatplotlibimportpyplot series = Series.from_csv('daily-total-female-births.csv', header=0) series.plot() pyplot.show()

Running the example loads the dataset as a Pandas Series, then shows a line plot of the data.


How to Save an ARIMA Time Series Forecasting Model in Python

Daily Total Female Births Plot

Stop learning Time Series Forecasting the slow way Sign-up and get a FREE 7-day Time Series Forecasting Mini-Course

You will get:

... onelesson each day delivered to your inbox

... exclusive PDF ebook containing all lessons

...

confidence and skills

to work through your own projects

Download Your FREE Mini-Course

Python Environment

Confirm you are using the latest version of the statsmodels library .

You can do that by running the script below:

importstatsmodels print('statsmodels: %s' % statsmodels.__version__)

Running the script should produce a result showing statsmodels 0.6 or 0.6.1.

statsmodels: 0.6.1

You can use either Python 2 or 3.

Update: I can confirm that the fault still exists in statsmodels 0.8 release candidate 1.

ARIMA Model Save Bug

We can easily train an ARIMA model on the Daily Female Births dataset.

The code snippet below trains an ARIMA(1,1,1) on the dataset.

The model.fit() function returns an ARIMAResults object on which we can call save() to save the model to file and load() to later load it.

frompandasimportSeries fromstatsmodels.tsa.arima_modelimportARIMA fromstatsmodels.tsa.arima_modelimportARIMAResults # load data series = Series.from_csv('daily-total-female-births.csv', header=0) # prepare data X = series.values X = X.astype('float32') # fit model model = ARIMA(X, order=(1,1,1)) model_fit = model.fit() # save model model_fit.save('model.pkl') # load model loaded = ARIMAResults.load('model.pkl')

Running this example will train the model and save it to file without problem.

An error will be reported when you try to load the model from file.

Traceback (most recent call last): File "...", line 16, in <module> loaded = ARIMAResults.load('model.pkl') File ".../site-packages/statsmodels/base/model.py", line 1529, in load return load_pickle(fname) File ".../site-packages/statsmodels/iolib/smpickle.py", line 41, in load_pickle return cPickle.load(fin) TypeError: __new__() takes at least 3 arguments (1 given)

Specifically, note the line:

TypeError: __new__() takes at least 3 arguments (1 given)

So far so good, so how do we fix it?

ARIMA Model Save Bug Workaround

Zae Myung Kim discovered this bug in September 2016 and reported the fault.

You can read all about it here:

BUG: Implemented __getnewargs__() method for unpickling

The bug occurs because a function required by pickle (the library used to serialize Python objects) has not been defined in statsmodels.

A function __getnewargs__ must be defined in the ARIMA model prior to saving that defines the arguments needed to construct the object.

We can work around this issue. The fix involves two things:

Defining an implementation of the __getnewargs__ function suitable for the ARIMA object. Adding the new function to ARIMA.

Thankfully, Zae Myung Kim provided an example of the function in his bug report so we can just use that directly:

def__getnewargs__(self): return ((self.endog),(self.k_lags, self.k_diff, self.k_ma))

Python allows us to monkey patch an object, even one from a library like statsmodels.

We can define a new function on an existing object using assignment.

We can do this for the __getnewargs__ function on the ARIMA object as follows:

ARIMA.__getnewargs__ = __getnewargs__

The complete example of training, saving, and loading an ARIMA model in Python with the monkey patch is listed below:

frompandasimportSeries fromstatsmodels.tsa.arima_modelimportARIMA fromstatsmodels.tsa.arima_modelimportARIMAResults # monkey patch around bug in ARIMA class def__getnewargs__(self): return ((self.endog),(self.k_lags, self.k_diff, self.k_ma)) ARIMA.__getnewargs__ = __getnewargs__ # load data series = Series.from_csv('daily-total-female-births.csv', header=0) # prepare data X = series.values X = X.astype('float32') # fit model model = ARIMA(X, order=(1,1,1)) model_fit = model.fit() # save model model_fit.save('model.pkl') # load model loaded = ARIMAResults.load('model.pkl') Running the example now successfully loads t

Viewing all articles
Browse latest Browse all 9596

Trending Articles