Ejemplo n.º 1
0
def train_arima_model(data_train, date_init, date_fin, op_red, type_day, transform='decompose-Fourier'
                      , type_decompose='additive', n_decompose=1, n_coeff_fourier=4, filter_decompose=None):
    num_cluster = data_train.name
    data_train = np.array(data_train)[~np.isnan(np.array(data_train))]
    type_model = 'arima'

    if transform == 'decompose-Fourier' or transform == 'decompose-Fourier-log':
        print('n_decompose: ', n_decompose, 'n_coeff_fourier: ', n_coeff_fourier)
        forecast_seasonal, trend_residual, n_diffs, periods_decompose, m_f, k_f = get_transform_model(data_train, transform=transform
                                                                                        , type_decompose=type_decompose
                                                                                        , n_decompose=n_decompose
                                                                                        , n_coeff_fourier=n_coeff_fourier)
        pipeline_trend_residual = Pipeline([
            ('fourier', ppc.FourierFeaturizer(m=m_f, k=k_f))
            , ("model", pm.AutoARIMA(d=n_diffs, seasonal=False, trace=True, error_action='ignore'
                                     , maxiter=30, max_p=4, max_q=4, suppress_warnings=True, with_intercept=True))])
        print('\t\t\t training model...')
        pipeline_trend_residual.fit(trend_residual)
        print(pipeline_trend_residual.summary())
        # aic_model = pipeline_trend_residual.steps[-1][1].model_.aic()
        print('\t\t\t saving model...')
        save_model_dir(pipeline_trend_residual, transform, num_cluster, op_red, type_day, type_model, date_init
                       , date_fin, periods_decompose, str(n_decompose), type_decompose)
        print('\t\t\t finish save model...')
    elif transform == 'Fourier':

        n_diffs, m_f, k_f = get_transform_model(data_train, transform=transform, n_coeff_fourier=n_coeff_fourier)
        pipeline = Pipeline([
            ('fourier', ppc.FourierFeaturizer(m=m_f, k=k_f))
            , ("model", pm.AutoARIMA(d=n_diffs, seasonal=False, trace=True, error_action='ignore'
                                     , maxiter=30, max_p=4, max_q=4, suppress_warnings=True, with_intercept=True))])
        pipeline.fit(data_train)
        save_model_dir(pipeline, transform, num_cluster, op_red, type_day, type_model, date_init, date_fin)

    elif transform == 'decompose':
        forecast_seasonal, trend_residual, n_diffs, ns_diffs, periods_decompose, m_f = get_transform_model(data_train
                                                                                             , transform=transform
                                                                                             , type_decompose=type_decompose
                                                                                             , n_decompose=n_decompose)
        pipeline_trend_residual = Pipeline(
            [("model", pm.AutoARIMA(d=n_diffs, D=ns_diffs, seasonal=True, m=m_f, trace=True, error_action='ignore'
                                    , maxiter=30, max_p=4, max_q=4, suppress_warnings=True, with_intercept=True))])
        pipeline_trend_residual.fit(trend_residual)
        save_model_dir(pipeline_trend_residual, transform, num_cluster, op_red, type_day, type_model, date_init
                       , date_fin, periods_decompose, str(n_decompose), type_decompose)
    elif transform == 'normal':
        n_diffs, ns_diffs, m_f = get_transform_model(data_train, transform=transform)
        pipeline = Pipeline(
            [("model", pm.AutoARIMA(d=n_diffs, D=ns_diffs, seasonal=True, m=m_f, trace=True, error_action='ignore'
                                    , maxiter=30, max_p=4, max_q=4, suppress_warnings=True, with_intercept=True))])
        pipeline.fit(data_train)
        save_model_dir(pipeline, transform, num_cluster, op_red, type_day, type_model, date_init, date_fin)
    else:
        raise ValueError('invalid variable transform {}.'.format(transform))
Ejemplo n.º 2
0
    vector_rmse_train.append(vector_combinaciones.iloc[0, 4])
    vector_rmse_test.append(vector_combinaciones.iloc[0, 3])
    algoritmo.append('Holt_winters')
    vector_serie.append(vector[s])

    ##########################################################33
    ########## MODELO AUTOARIMA

    errores_proceso_ar = []
    try:
        # Let's create a pipeline with multiple stages... the Wineind dataset is
        # seasonal, so we'll include a FourierFeaturizer so we can fit it without
        # seasonality
        pipe = pipeline.Pipeline([
            ("fourier", ppc.FourierFeaturizer(
                m=5)),  #modela la estacionalidad con periodicidad 5
            (
                "arima",
                arima.AutoARIMA(
                    stepwise=True,
                    trace=1,
                    error_action="ignore",
                    seasonal=False,  # because we use Fourier
                    suppress_warnings=True))
        ])

        pipe.fit(train_arima)
        print("Model fit:")
        print(pipe)

        # We can compute predictions the same way we would on a normal ARIMA object:
Ejemplo n.º 3
0
# Author: Taylor Smith <*****@*****.**>

import numpy as np
import pmdarima as pm
from pmdarima import pipeline, preprocessing as ppc, arima
from matplotlib import pyplot as plt

# Load the data and split it into separate pieces
data = pm.datasets.load_wineind()
train, test = data[:150], data[150:]

# Let's create a pipeline with multiple stages... the Wineind dataset is
# seasonal, so we'll include a FourierFeaturizer so we can fit it without
# seasonality
pipe = pipeline.Pipeline([
    ("fourier", ppc.FourierFeaturizer(m=12, k=4)),
    ("arima", arima.AutoARIMA(stepwise=True, trace=1, error_action="ignore",
                              seasonal=False,  # because we use Fourier
                              transparams=False,
                              suppress_warnings=True))
])

pipe.fit(train)
print("Model fit:")
print(pipe)

# We can compute predictions the same way we would on a normal ARIMA object:
preds, conf_int = pipe.predict(n_periods=10, return_conf_int=True)
print("\nForecasts:")
print(preds)
Ejemplo n.º 4
0
# Author: Taylor Smith <*****@*****.**>

import numpy as np
import pmdarima as pm
from pmdarima import pipeline, preprocessing as ppc, arima
from matplotlib import pyplot as plt

# Load the data and split it into separate pieces
data = pm.datasets.load_wineind()
train, test = data[:150], data[150:]

# Let's create a pipeline with multiple stages... the Wineind dataset is
# seasonal, so we'll include a FourierFeaturizer so we can fit it without
# seasonality
pipe = pipeline.Pipeline([
    ("fourier", ppc.FourierFeaturizer(m=12)),
    (
        "arima",
        arima.AutoARIMA(
            stepwise=True,
            trace=1,
            error_action="ignore",
            seasonal=False,  # because we use Fourier
            transparams=False,
            suppress_warnings=True))
])

pipe.fit(train)
print("Model fit:")
print(pipe)
def Arima(df, dataset, months, var):
    #     if dataset=='iig_maitri' or dataset=='iig_bharati':
    #         data2=pd.read_csv('datasets/'+dataset+'.csv')
    #         # print(df)
    #     elif dataset=='dcwis':
    #         data2=pd.read_csv('datasets/'+dataset+'.csv', names=['obstime', 'tempr', 'ap', 'ws', 'rh', 'dew'])
    df['obstime'] = pd.to_datetime(df['obstime'])
    df = df.set_index('obstime')
    data2 = df[var]
    # print(data2)
    # train2, test2= data2[:23423], data2[23423:]
    # train =train2.resample('M').mean()
    #     ds_temp=df[var].resample('M').mean()
    if var == 'rh':
        data2 = data2[data2 > 10]
    if var == 'ws':
        data2 = data2[data2 >= 0]
    if var == 'ap':
        data2 = data2[data2 > -10]
    data = data2.resample('M').mean()
    # print(data)
    # data.dropna(inplace=True)
    # train.dropna(inplace=True)
    # test.dropna(inplace=True)
    #     data['Date']=data.index.strftime('%B')
    #     data['Date']=data['Date']+' '+data.index.strftime('%Y')
    # data.set_index('Date', inplace=True)
    #     datum=data
    data.dropna(inplace=True)
    # print(data.size)
    ind = list()
    for i in range(int(data.size)):
        ind.append(i)
    # print(data.Date)
    # print(ind)
#     Q1 = data.quantile(0.25)
#     Q3 = data.quantile(0.75)
#     IQR = Q3 - Q1
#     # print(IQR)
#     data = data[~((data < (Q1 - 1.5 * IQR)) |(data > (Q3 + 1.5 * IQR)))]
# data.shape
# Let's create a pipeline with multiple stages... the Wineind dataset is
# seasonal, so we'll include a FourierFeaturizer so we can fit it without
# seasonality
    pipe = pipeline.Pipeline([
        ("fourier", ppc.FourierFeaturizer(m=12)),
        (
            "arima",
            arima.AutoARIMA(
                stepwise=True,
                trace=1,
                error_action="ignore",
                seasonal=False,  # because we use Fourier
                transparams=False,
                suppress_warnings=True))
    ])

    pipe.fit(data)
    # print("Model fit:")
    #     # print(pipe)
    #     months=12
    dates = []
    for year in range(2012, 2016 + int(months / 6)):
        for month in range(1, 13):
            dates.append(dt.datetime(year=year, month=month, day=28))
    preds, conf_int = pipe.predict(n_periods=months, return_conf_int=True)
    datum = data
    data3 = data2.resample('M').mean()
    # print(dates)
    # print(data3[dt()'2012-01-31'])
    # for i, j in zip(data3.index, range(data3.size)):
    #     if math.isnan(data3[i])==True:
    #        del dates[j]
    # print(datum)
    temp = np.append(data3, preds)
    # dates=np.array(dates)
    # print(np.array(dates).shape)
    # # print
    # print(temp.shape)
    # plt.subplot(211)
    plt.plot(dates[:temp.size], temp)
    plt.plot(datum)
Ejemplo n.º 6
0
import matplotlib.pyplot as plt

data3 = pm.datasets.load_wineind()
train, test = data3[:150], data[150:]
lenSeq = 10000
subSamp = 40
f0SamplesSS = 10
f0Samples = 400

data.shape
data = data[:lenSeq]
dataSS = data[::subSamp]

with StepwiseContext(max_steps=2):
  pipe = pipeline.Pipeline([
      ("fourier", ppc.FourierFeaturizer(m=f0Samples)),
      ("arima", arima.AutoARIMA(stepwise=True, maxiter=20, with_intercept = False, start_p=5, start_q=4,  max_p= 6, max_q= 6,  trace=1, error_action="ignore",
                              seasonal=False,  # because we use Fourier
                              suppress_warnings=True))
  ])

  pipe.fit(data)
  yhat = pipe.predict(n_periods=1000)

#from pyramid.arima import auto_arima
#f0Samples = 10 # fs = 44100, f0 = 110 (A2), therefore f0 in samples is approx 400
#thissa = pm.auto_arima(train, error_action='ignore', seasonal=True, m=12)
#thissarimaSS =  pm.auto_arima(dataSS, with_intercept = False, d = 0, D = 0, start_p=0, start_q=0,test='adf',  max_p= 3, max_q= 3,     m=f0SamplesSS,start_P=0, start_Q= 0, max_Q=3, max_P=3, trace=True,error_action='ignore', suppress_warnings=True)

#thissarimaS1 =  pm.auto_arima(data, with_intercept = False, d = 0, D = 0, start_p=0, start_q=0,test='adf',  max_p= 3, max_q= 3,     m=f0Samples,start_P=0, start_Q= 0, max_Q=3, max_P=3, trace=True,error_action='ignore', suppress_warnings=True)
#thissarima =  pm.auto_arima(data, with_intercept = False, d = 0, start_p=0, start_q=0,test='adf',  max_p= 5, max_q= 5,  seasonal=False, trace=True,error_action='ignore', suppress_warnings=True)