##########################################################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: forecast_arima = pipe.predict(n_periods=int(len(test))) rmse_test_arima = mean_squared_error(test_arima, forecast_arima, squared=False)
def draw_(province, isDaily): # 模型训练 model = arima.AutoARIMA( start_p=0, max_p=4, d=None, start_q=0, max_q=1, start_P=0, max_P=1, D=None, start_Q=0, max_Q=1, m=7, seasonal=True, test="kpss", trace=True, error_action="ignore", suppress_warnings=True, stepwise=True, ) if isDaily: data = df[province].diff().dropna() model.fit(data) else: data = df[province] model.fit(data) # 模型验证 train, test = train_test_split(data, train_size=0.8) pred_test = model.predict_in_sample(start=train.shape[0], dynamic=False) validating = pd.Series(pred_test, index=test.index) r2 = r2_score(test, pred_test) # 开始预测 pred, pred_ci = model.predict(n_periods=14, return_conf_int=True) idx = pd.date_range(data.index.max() + pd.Timedelta("1D"), periods=14, freq="D") forecasting = pd.Series(pred, index=idx) # 绘图呈现 plt.figure(figsize=(24, 6)) plt.plot(data.index, data, label="Actual Value", color="blue") plt.plot(validating.index, validating, label="Check Value", color="orange") plt.plot(forecasting.index, forecasting, label="Predict Value", color="red") # plt.fill_between(forecasting.index, pred_ci[:, 0], pred_ci[:, 1], color="black", alpha=.25) plt.legend() plt.ticklabel_format(style="plain", axis="y") # plt.rcParams["font.sans-serif"] = ["Microsoft YaHei"] if isDaily: plt.title( f"Daily Confirmed Cases Forecasting - {province}\nARIMA {model.model_.order}x{model.model_.seasonal_order} (R2 = {r2:.6f})" ) plt.savefig( os.path.join("figures", f"covid-{adjust_name(province)}-daily.svg"), bbox_inches="tight", ) plt.close() else: plt.title( f"Accumulative Confirmed Cases Forecasting - {province}\nARIMA {model.model_.order}x{model.model_.seasonal_order} (R2 = {r2:.6f})" ) plt.savefig( os.path.join("figures", f"covid-{adjust_name(province)}.svg"), bbox_inches="tight", ) plt.close()
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) #paramsSS = thissarimaSS.get_params([0]) #sos = paramsSS.get('seasonal_order')
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)
# Here's what the featurizer will create for us: date_feat = preprocessing.DateFeaturizer( column_name="date", # the name of the date feature in the exog matrix with_day_of_week=True, with_day_of_month=True) _, X_train_feats = date_feat.fit_transform(y_train, X_train) print("Head of generated exog features:\n%s" % repr(X_train_feats.head())) # We can plug this exog featurizer into a pipeline: pipe = pipeline.Pipeline([ ('date', date_feat), ('arima', arima.AutoARIMA(d=n_diffs, trace=3, stepwise=True, suppress_warnings=True, seasonal=False)) ]) pipe.fit(y_train, X_train) # Plot our forecasts forecasts = pipe.predict(exogenous=X_test) fig = plt.figure(figsize=(16, 8)) ax = fig.add_subplot(1, 1, 1) n_train = y_train.shape[0] x = np.arange(n_train + forecasts.shape[0])