Beispiel #1
0
def test_pipeline_behavior():
    wineind = load_wineind()
    train, test = wineind[:125], wineind[125:]

    pipeline = Pipeline([
        ("fourier", FourierFeaturizer(m=12)),
        ("arima", AutoARIMA(seasonal=False, stepwise=True,
                            suppress_warnings=True,
                            maxiter=3, error_action='ignore'))
    ])

    # Quick assertions on indexing
    assert len(pipeline) == 2

    pipeline.fit(train)
    preds = pipeline.predict(5)
    assert preds.shape[0] == 5

    assert pipeline._final_estimator.model_.fit_with_exog_

    # Assert that when the n_periods kwarg is set manually and incorrectly for
    # the fourier transformer, we get a ValueError
    kwargs = {
        "fourier__n_periods": 10
    }

    with pytest.raises(ValueError) as ve:
        pipeline.predict(3, **kwargs)
    assert "'n_periods'" in pytest_error_str(ve)

    # Assert that we can update the model
    pipeline.update(test, maxiter=5)

    # And that the fourier transformer was updated properly...
    assert pipeline.steps_[0][1].n_ == wineind.shape[0]
Beispiel #2
0
def main(datasetid):
  if(datasetid == 'shampoo'):
    series = read_csv('shampoo-sales.csv', header=0, parse_dates=[0], index_col=0, squeeze=True, date_parser=parser)
  else:
    series = load_wineind().astype(np.float64)

  prot05(series)
Beispiel #3
0
hr = load_heartrate(as_series=True)

# > set.seed(123)
# > abc <- rnorm(50, 5, 1)
abc = np.array([
    4.439524, 4.769823, 6.558708, 5.070508, 5.129288, 6.715065, 5.460916,
    3.734939, 4.313147, 4.554338, 6.224082, 5.359814, 5.400771, 5.110683,
    4.444159, 6.786913, 5.497850, 3.033383, 5.701356, 4.527209, 3.932176,
    4.782025, 3.973996, 4.271109, 4.374961, 3.313307, 5.837787, 5.153373,
    3.861863, 6.253815, 5.426464, 4.704929, 5.895126, 5.878133, 5.821581,
    5.688640, 5.553918, 4.938088, 4.694037, 4.619529, 4.305293, 4.792083,
    3.734604, 7.168956, 6.207962, 3.876891, 4.597115, 4.533345, 5.779965,
    4.916631
])

wineind = load_wineind()
lynx = load_lynx()

# Yes, m is ACTUALLY 12... but that takes a LONG time. If we set it to
# 1, we actually get a much, much faster model fit. We can only use this
# if we're NOT testing the output of the model, but just the functionality!
wineind_m = 1

# A random xreg for the wineind array
wineind_xreg = rs.rand(wineind.shape[0], 2)


def _unlink_if_exists(path):
    if os.path.exists(path):
        os.unlink(path)
Beispiel #4
0
####
from statsmodels.tsa.api import ExponentialSmoothing
exp = ExponentialSmoothing(data)
exp_model = exp.fit(smoothing_level=0.1)
result = exp_model.fittedvalues
dir(exp_model)
data
exp_model.predict(30)
result
result.plot()


#%%%%
import pmdarima.datasets as pm
wine = pm.load_wineind(True)
wine.head()
wine.tail()
wine.head().append(wine.tail())
wine.shape
wine
#176 observations
wineTrg = wine[0:108] # Up to December '88
#create model for 108 observations
wineVal = wine[108:] # From January '89 until end
wineTrg
wineVal
#%%%%%
wineTrg.rolling(window=3)
wine_ma3c = wineTrg.rolling(window=3, center=True).mean()
wine_ma3c
Beispiel #5
0
# -*- coding: utf-8 -*-

from pmdarima.preprocessing.exog import base
from pmdarima import datasets
import numpy as np
import pandas as pd

wineind = datasets.load_wineind()


class RandomExogFeaturizer(base.BaseExogFeaturizer):
    """Creates random exog features. This is just used to test base func"""

    def _get_prefix(self):
        return "RND"

    def fit(self, y, X, **_):
        return self

    def transform(self, y, X=None, n_periods=0, **_):
        Xt = np.random.rand(y.shape[0], 4)
        Xt = self._safe_hstack(X, Xt)
        return y, Xt


def test_default_get_feature_names():
    feat = RandomExogFeaturizer()
    y_trans, X = feat.fit_transform(wineind)
    assert y_trans is wineind
    assert X.columns.tolist() == \
        ['RND_0', 'RND_1', 'RND_2', 'RND_3']
Beispiel #6
0
from pmdarima.datasets import load_wineind
import pmdarima as pm
import numpy as np

# this is a dataset from R
wineind = load_wineind().astype(np.float64)

# fit stepwise auto-ARIMA
stepwise_fit = pm.auto_arima(
    wineind,
    start_p=1,
    start_q=1,
    max_p=3,
    max_q=3,
    m=12,
    start_P=0,
    seasonal=True,
    d=1,
    D=1,
    trace=True,
    error_action='ignore',  # don't want to know if an order does not work
    suppress_warnings=True,  # don't want convergence warnings
    stepwise=True)  # set to stepwise

print(stepwise_fit.summary())
print(stepwise_fit.get_params())
Beispiel #7
0
from pmdarima import auto_arima
from pmdarima.datasets import load_wineind
from pmdarima import model_selection


ARTIFACT_PATH = "model"


def calculate_cv_metrics(model, endog, metric, cv):
    cv_metric = model_selection.cross_val_score(model, endog, cv=cv, scoring=metric, verbose=0)
    return cv_metric[~np.isnan(cv_metric)].mean()


with mlflow.start_run():

    data = load_wineind()

    train, test = model_selection.train_test_split(data, train_size=150)

    print("Training AutoARIMA model...")
    arima = auto_arima(
        train,
        error_action="ignore",
        trace=False,
        suppress_warnings=True,
        maxiter=5,
        seasonal=True,
        m=12,
    )

    print("Model trained. \nExtracting parameters...")