def _evaluate_garch_model(self, train, test, split_time): """ Optimize Garch model :param train: train part of the signal :param test: test part of the signal :param split_time: point when you start to predict :return: variance volatility """ warnings.filterwarnings("ignore") best_score = float(10000) best_p = (1) best_q = int(1) p_values = range(1, 3) q_values = range(1, 2) for p in p_values: for q in q_values: try: am = arch_model(train, vol='Garch', p=p, q=q) res = am.fit(last_obs=split_time) aic = SARIMAXResults.aic(res) if abs(aic) < best_score: best_score, best_p, best_q = aic, p, q # print('GARCH : p=%i GARCH : q=%i AIC=%.3f' % (p, q, aic)) except: continue print('Best config GARCH model p=%i q=%i AIC=%.3f' % (best_p, best_q, best_score)) am = arch_model(train, vol='Garch', p=best_p, q=best_q) res = am.fit(last_obs=split_time) forecast_analytic = res.forecast(horizon=len(test), method='analytic') forecast_variance = forecast_analytic.variance[split_time:] forecast_volatility = forecast_analytic.residual_variance[split_time:] forecast_garch = forecast_variance.iloc[:, 0] forecast_vol = forecast_volatility.iloc[:, 0] return forecast_garch, forecast_vol
def preveProximasCincoSemanas(): loaded = SARIMAXResults.load('model.pkl') hoje = date.today() ultimaData = date(2020, 3, 15) medicoesInicio = round((hoje-ultimaData).days/7) numMedicoes=medicoesInicio+5 primeiraData='2020-03-22' predicao=loaded.get_forecast(steps=numMedicoes) predicao.predicted_mean ##index_date = pandas.date_range(primeiraData, periods = numMedicoes, freq = 'W') index_date = pandas.bdate_range(primeiraData, periods = numMedicoes, freq = 'C', weekmask='Mon') ##forecast_series = pandas.Series(list(predicao.predicted_mean), index = index_date) datasStr=numpy.datetime_as_string(index_date, unit='D') print(numpy.column_stack((datasStr,list(predicao.predicted_mean)))) ##conversaoEmLista = pandas.DataFrame(data=forecast_series).tail(5).to_numpy().tolist() conversaoEmLista = pandas.DataFrame(numpy.column_stack((datasStr,list(predicao.predicted_mean)))).tail(5).to_numpy().tolist() response = app.response_class( response=json.dumps(conversaoEmLista), status=200, mimetype='application/json' ) return response
def crearPrediccionMR(fichero_modelo, tipo): loaded = SARIMAXResults.load(fichero_modelo) DIAS_A_PREDECIR = 7 inicio = datetime.today().__format__('%Y-%m-%d') # Hoy final = datetime.today() for i in range(DIAS_A_PREDECIR): final += timedelta(days=1) final = final.__format__('%Y-%m-%d') # x DÍAS EN ADELANTE inicio = datetime(2018, 3, 1, 0, 0, 0) # fuerzo un día en concreto because boobs final = datetime(2018, 3, 7, 0, 0, 0) # fuerzo un día en concreto because boobs predicciones = loaded.predict( start=inicio, end=final, dynamic=False, typ='levels').rename('SARIMA(2,1,1)(2,0,1,24) Predicciones') ristra = pd.date_range(start=inicio, end=final, freq='h') d = {'Fecha': ristra, tipo: predicciones} df = pd.DataFrame(data=d) ruta_fich = settings.MEDIA_ROOT + '\\prediccionesMR\\' ruta_pred = ruta_fich + 'prediccion' + id_random_generator() + '.csv' df.to_csv(ruta_pred) return ruta_pred
def _evaluate_models(self, train, d, d_seasonal, WD): best_score = float(10000) best_cfg_arima = (1, d, 0) best_cfg_seasonal = (1, d_seasonal, 0, WD) p_values = range(1, 3) q_values = range(1, 2) for p in p_values: for q in q_values: for p_s in p_values: for q_s in q_values: order = (p, d, q) seasonal_order = (p_s, d_seasonal, q_s, WD) try: model = SARIMAX(train, order=order, seasonal_order=seasonal_order) md_fit = model.fit() aic = SARIMAXResults.aic(md_fit) if abs(aic) < best_score: best_score, best_cfg_arima, best_cfg_seasonal = aic, order, seasonal_order except: continue print('Best ARIMA%s Best Seasonal%s AIC=%.3f' % (best_cfg_arima, best_cfg_seasonal, best_score)) return best_cfg_arima, best_cfg_seasonal, best_score
def predict_with_sarima(self, order=(1, 0, 0), seasonal_order=(1, 0, 0, 24)): # seasonal_order=(1, 1, 1, 24), saved_model = SARIMAXResults.load('{}{}{}.pkl'.format(self.model_dir, self.name, self.sarima_models_suffix)) data_used_for_prediction = self.train[-48:] model = SARIMAX(endog=data_used_for_prediction, order=order, seasonal_order=seasonal_order) fitted_model = model.filter(params=saved_model.params) self.prediction = fitted_model.predict(start=len(data_used_for_prediction), end=len(data_used_for_prediction) + self.prediction_length - 1) return self.prediction
def load(self, filepath): ''' Loads a model from a pickle file Arguments: filepath - Path to the pickle file containing the model ''' self.model = SARIMAXResults.load(filepath)
def crearPrediccionMRunico(modelo, tipo, rango): """ Crea las predicciones de precios para cada tarifa del mercado regulado. :param modelo: ruta del modelo a partir del cual crear la predicción. :param tipo: tarifa en concreto de la que se creará la predicción :param rango: franja temporal en la que crear la predicción. :return: ruta del fichero que contiene la predicción creada. """ loaded = SARIMAXResults.load(modelo) inicio = pd.to_datetime(rango.get('principio')) final = pd.to_datetime(rango.get('final')) if tipo == 'TPD': predicciones = loaded.predict( start=inicio, end=final, dynamic=False, typ='levels').rename('SARIMA(3,1,1)(2,0,1,24) Predicciones') ristra = pd.date_range(start=inicio, end=final, freq='h') d = {'Fecha': ristra, tipo: predicciones} df = pd.DataFrame(data=d) ruta_fich = settings.MEDIA_ROOT + '\\prediccionesMR\\' ruta_pred = ruta_fich + 'prediccionMR_' + tipo + '_' + id_random_generator( ) + '.csv' df.to_csv(ruta_pred) return ruta_pred elif tipo == 'EDP': predicciones = loaded.predict( start=inicio, end=final, dynamic=False, typ='levels').rename('SARIMA(1,0,0)(2,0,0,24) Predicciones') ristra = pd.date_range(start=inicio, end=final, freq='h') d = {'Fecha': ristra, tipo: predicciones} df = pd.DataFrame(data=d) ruta_fich = settings.MEDIA_ROOT + '\\prediccionesMR\\' ruta_pred = ruta_fich + 'prediccionMR_' + tipo + '_' + id_random_generator( ) + '.csv' df.to_csv(ruta_pred) return ruta_pred elif tipo == 'VE': predicciones = loaded.predict( start=inicio, end=final, dynamic=False, typ='levels').rename('SARIMA(2,0,0)(2,0,0,24) Predicciones') ristra = pd.date_range(start=inicio, end=final, freq='h') d = {'Fecha': ristra, tipo: predicciones} df = pd.DataFrame(data=d) ruta_fich = settings.MEDIA_ROOT + '\\prediccionesMR\\' ruta_pred = ruta_fich + 'prediccionMR_' + tipo + '_' + id_random_generator( ) + '.csv' df.to_csv(ruta_pred) return ruta_pred else: # Creación de modelos para tarifas que no se tienen en cuenta: NOPE. print('Creación de Predicciones unicas mercado regulado: error') return False
def load_model(self, path, filename, version): """ :param path: Full or relative path for the model file :param filename: Name of the file :param version: Version number :return: unpickled instance of the SARIMAX model """ return SARIMAXResults.load( self.__generate_pkl_filename(path, filename, version))
def load(self, model_path, scaler_path=None): ''' Loads the model from disk :param model_path: Path to saved model ''' if os.path.splitext(model_path)[1] != '.pkl': raise Exception('Model file path for ' + self.name + ' must have ".pkl" extension.') self.model = SARIMAXResults.load(model_path) return
def plot_sarima_forecast(idf_df): idf_df[c.EnergyConso.CONSUMPTION] = idf_df[c.EnergyConso.CONSUMPTION].fillna( idf_df[c.EnergyConso.CONSUMPTION].mean()) idf_train = idf_df[md.END_TRAIN_DATE - timedelta(days=365):md.END_TRAIN_DATE] idf_test = idf_df[md.END_TRAIN_DATE:md.END_TRAIN_DATE + timedelta(hours=md.NB_HOURS_PRED)] best_model = SARIMAXResults.load(os.path.join(SARIMA_MODELS_PATH, "best_model.pkl")) plot_sarima(idf_test, idf_train, best_model, md.NB_HOURS_PRED, s=1, d=1)
def price_predict(): product_name = request.args.get("product_name") num_steps = int(request.args.get("num_steps")) loaded = SARIMAXResults.load(product_name + ".pkl") pred_uc = loaded.get_forecast(steps=num_steps) pred_ci = pred_uc.conf_int() print(pred_ci) bytes_obj = get_plot(pred_uc, pred_ci, product_name) #return send_file(bytes_obj,attachment_filename='plot.png',mimetype='image/png') return "Done"
def crearPrediccion(fichero): """ Creación de la predicción de consumo para un Inmueble. :param fichero: ruta del modelo predictivo a emplear. :return: ruta del fichero con la predicción. """ print('Has entrado a la función') # print(File(fichero)) print(fichero) print('Cargamos el fichero') loaded = SARIMAXResults.load(fichero) print('Ya está cargado') DIAS_A_PREDECIR = 7 inicio = datetime.today().__format__('%Y-%m-%d') # Hoy final = datetime.today() for i in range(DIAS_A_PREDECIR): final += timedelta(days=1) final = final.__format__('%Y-%m-%d') # x DÍAS EN ADELANTE inicio = datetime(2018, 3, 1, 0, 0, 0) # fuerzo un día en concreto because boobs final = datetime(2018, 3, 7, 0, 0, 0) # fuerzo un día en concreto because boobs # print(inicio) # print(final) print('Fechas elegidas. Procedo a predecir.') predicciones = loaded.predict( start=inicio, end=final, dynamic=False, typ='levels').rename('SARIMA(1,1,1)(2,0,3,24) Predicciones') print('Predicciones hechas.') #Creo un dataframe con los valores de las predicciones ristra = pd.date_range(start=inicio, end=final, freq='h') d = {'Fecha': ristra, 'Consumo_kWh': predicciones} df = pd.DataFrame(data=d) ruta_fich = settings.MEDIA_ROOT + '\\predicciones\\' ruta_pred = ruta_fich + 'prediccion' + id_random_generator() + '.csv' df.to_csv(ruta_pred) # prediccion = predicciones.to_csv('LaPrediccion.csv') print('Guardado en csv. Te lo mando.') # return predicciones return ruta_pred
def forecast_sarima(df, n_history, pred_horizon): start = len(df[:-pred_horizon]) - 1 end = start + pred_horizon - 1 starttime = time.time() order = (1, 3, 0) seasonal_order = (1, 1, 1, n_history) #last parameter should be 52 for weekly saisonality train_sarima(df, n_history = n_history, pred_horizon=pred_horizon, order=order, seasonal_order=seasonal_order) print("training sarima took ", time.time() - starttime) model = SARIMAXResults.load('sarima.pkl') predictions = model.predict(start=start, end=end, dynamic=False).rename('SARIMAX Predictions' % n_history) df_forecast = pd.DataFrame(predictions.values, columns=['SARIMA Predictions (1, 3, 0)(1, 1, 1, n_history) ' % n_history]) return generate_index_forecast(df, df_forecast, pred_horizon=pred_horizon)
def predict_with_trained_SARIMA_model(self, df, game_id, dep_var, indep_var, X_out_of_sample, target, type, model_type): # might be mistakes here! save_path = util.build_model_save_path(game_id, target, type, model_type) if not util.check_for_model_existence(save_path): print("Cannot predict for {}. No model has been trained yet.".format(dep_var)) return saved_model = SARIMAXResults.load(save_path) Y = df[dep_var] X = df[indep_var] X_out_of_sample.index = range(max(Y.index) + 1, max(Y.index) + 24 + 1) latest_index = max(Y.index) latest_timeslot = df.iloc[latest_index]['timeslot'] if self.seasonal_order is None: model = SARIMAX(endog=Y, exog=X, order=self.order) else: model = SARIMAX(endog=Y, exog=X, order=self.order, seasonal_order=self.seasonal_order) fitted_model = model.filter(params=saved_model.params) prediction = fitted_model.predict(exog=X_out_of_sample, start=latest_index + 1, end=latest_index + 1 + self.forecast_length - 1) df_prediction = pd.DataFrame( {'target_timeslot': range(latest_timeslot + 1, latest_timeslot + 1 + self.forecast_length), 'prediction': prediction}) # set lower and upper bound for prediction tolerance = 0.2 prediction_upper_bound = max(Y) * (1 + tolerance) if max(Y) > 0 else max(Y) * (1 - tolerance) prediction_lower_bound = min(Y) * (1 - tolerance) if min(Y) > 0 else min(Y) * (1 + tolerance) # restrict prediction in lower and upper bound df_prediction['prediction'] = df_prediction['prediction'].apply( lambda x: max([min([x, prediction_upper_bound]), prediction_lower_bound])) df_prediction['prediction_timeslot'] = latest_timeslot df_prediction['proximity'] = df_prediction['target_timeslot'] - df_prediction['prediction_timeslot'] df_prediction['game_id'] = game_id df_prediction['target'] = target df_prediction['type'] = type return df_prediction
def fit_predict(self, df): """ Responsible for the train/inference pipeline of the model Args: df: the dataframe containing the time series data """ logger.info("SARIMAX Modeling") self.df = df self.df.index = pd.date_range(df.index[0], df.index[-1], freq='H') # Log Transform to battle Heteroscedasticity self.data = np.log(self.df['requests']) # Train/Test Split #train, test, exog_train, exog_test = self.train_test_split(self.df, data) self.train_test_split() if not self.trainmodel: logger.info("\tLoading Model from Disk...") self.result = SARIMAXResults.load(self.modelpath) else: self.train_sarimax() self.predict_sarimax()
from statsmodels.tsa.statespace.sarimax import SARIMAXResults import pickle forecast_model_train = SARIMAXResults.load('forecast_model_train.pkl') # Make a forecast for the next 6 periods after the last period in the training set future = forecast_model_train.forecast(steps=5) print('Train forecats...') print(future) # Make a forecast for the next 6 periods after the last period in the test set forecast_model_test = SARIMAXResults.load('forecast_model_test.pkl') future = forecast_model_test.forecast(steps=5) print('\nTest forecasts') print(future)
concentrate_scale=False, trend_offset=1, use_exact_diffuse=False, dates=None, freq=None, missing='none' ) res = mod.fit(disp=False) if SAVE_MODEL: res.save('save/prediction_model.pkl') else: res = SARIMAXResults.load('save/prediction_model.pkl') # ------------------------------ Prediction ----------------------------------- def forecast_two_next_weeks(two_last_weeks, res, length): """Predicts the two next weeks of a time series from the last two weeks using the SARIMAX method above Arguments: two_last_weeks {float tab} -- Two last weeks of the time series from which we predict res {statsmodels.tsa.statespace.sarimax.SARIMAXResults} -- prediction model (defined above) length {int} -- length of the prediction Returns: float np.array -- forecast of the next values of the time series
def sarima_test(): data = pd.read_csv('../data/CGMData.csv', header=None) cgm, bolus = load_data(path=os.path.abspath('..')) data.reindex(index=data.index[::-1]) t=0 for i in range(len(data.columns)): data[i][0] = t t+=5 data.iloc[1] = data.iloc[1].interpolate() data = data.T data = data[:len(bolus)] data.columns=["time","CGMreading"] y = data.set_index(["time"]) results = SARIMAXResults.load(os.path.join('..', 'model', "mc_sarima.pkl")) classification_model = pickle.load(open(os.path.join('..', 'model', 'classifer_model.pkl'), 'rb')) pred = results.get_prediction(start=26255, dynamic=False) pred_ci = pred.conf_int() ax = y.plot(label='observed') pred.predicted_mean.plot(ax=ax, label='One-step ahead Forecast', alpha=.7, figsize=(14, 4)) ax.fill_between(pred_ci.index, pred_ci.iloc[:, 0], pred_ci.iloc[:, 1], color='k', alpha=.2) ax.set_xlabel('Date') ax.set_ylabel('Retail_sold') plt.legend() plt.show() y_forecasted = pred.predicted_mean y_truth = y[131275:]['CGMreading'] mse = ((y_forecasted - y_truth) ** 2).mean() print('The Mean Squared Error is {}'.format(round(mse, 2))) print('The Root Mean Squared Error is {}'.format(round(np.sqrt(mse), 2))) testPredict = [] for i in range(0,(len(pred.predicted_mean)-6)): temp_predict = [] temp_predict.append(pred.predicted_mean.iloc[i]) temp_predict.append(pred.predicted_mean.iloc[i+1]) temp_predict.append(pred.predicted_mean.iloc[i+2]) temp_predict.append(pred.predicted_mean.iloc[i+3]) temp_predict.append(pred.predicted_mean.iloc[i+4]) temp_predict.append(pred.predicted_mean.iloc[i+5]) testPredict.append(temp_predict) testPredict = np.array(testPredict) test = cgm[26249:(len(cgm))] test = test.reshape(-1, 1) test = regression_scaler.fit_transform(test) testX, testY = create_dataset_multi_feature(test, 6) _, _, _, bolus_test, classification_scaler = classification_split(cgm, bolus) bolus_test = bolus_test[6 - 1:testX.shape[0] + 6 - 2] predicted_labels = [] regression_predictions = copy.deepcopy(testPredict) classification_train_data = classification_scaler.transform(regression_predictions) prediction = classification_model.predict(classification_train_data) predicted_labels.extend(prediction) print(classification_report(bolus_test, predicted_labels))
def home(): fcast = "" if request.method == 'POST': method = request.form['forecast'] # Forecasting using saved ARMA model if method == "arma": result = SARIMAXResults.load('model/arma_model.pkl') forecast_values = result.get_forecast(steps=test.shape[0]) forecast_values_mean = forecast_values.predicted_mean conf_interval = forecast_values.conf_int() arma_forecast_df = pd.DataFrame({ 'Date': test.index, 'Views': forecast_values.predicted_mean, 'lower_views': conf_interval['lower Views'].values, 'upper_views': conf_interval['upper Views'].values }) arma_forecast_df = arma_forecast_df.set_index('Date') fig, ax = plt.subplots(figsize=(15, 4)) test.rename(columns={ 'Views': 'Actual value' }).plot(ax=ax, color='blue') arma_forecast_df[['Views']].rename(columns={ 'Views': 'Forecast' }).plot(ax=ax, label='Forecast', color='red') plt.fill_between(arma_forecast_df.index, \ arma_forecast_df.lower_views, \ arma_forecast_df.upper_views, \ color='pink', alpha=0.5) plt.xlabel('Date') plt.ylabel('Views') plt.legend(loc='best') new_arma_plot = "arma_plot_" + str(time.time()) + ".png" for filename in os.listdir('static/'): if filename.startswith('arma_plot_'): os.remove('static/' + filename) plt.savefig('static/' + new_arma_plot) return render_template('index.html', forecast='ARMA', fcast='static/' + new_arma_plot) # Forecasting using saved ARIMA model elif method == "arima": arima_result = SARIMAXResults.load('model/arima_model.pkl') arima_forecast_values = arima_result.get_forecast( steps=test.shape[0]) arima_forecast_mean = arima_forecast_values.predicted_mean arima_conf_interval = arima_forecast_values.conf_int() arima_forecast_df = pd.DataFrame({ 'Date': test.index, 'Views': arima_forecast_values.predicted_mean, 'lower_views': arima_conf_interval['lower Views'].values, 'upper_views': arima_conf_interval['upper Views'].values }) arima_forecast_df = arima_forecast_df.set_index('Date') fig, ax = plt.subplots(figsize=(15, 4)) test.rename(columns={ 'Views': 'Actual value' }).plot(ax=ax, color='blue') arima_forecast_df[['Views']].rename(columns={ 'Views': 'Forecast' }).plot(ax=ax, label='Forecast', color='red') plt.fill_between(arima_forecast_df.index, \ arima_forecast_df.lower_views, \ arima_forecast_df.upper_views, \ color='pink', alpha=0.5) plt.xlabel('Date') plt.ylabel('Views') plt.legend(loc='best') new_arima_plot = "arima_plot_" + str(time.time()) + ".png" for filename in os.listdir('static/'): if filename.startswith('arima_plot_'): os.remove('static/' + filename) plt.savefig('static/' + new_arima_plot) return render_template('index.html', forecast='ARIMA', fcast='static/' + new_arima_plot) # Forecasting using saved Exponential Smoothing model elif method == 'exp': exp_model = pickle.load(open('model/exp_smoothing_model.pkl', 'rb')) exp_smoothing_result = exp_model.fit(smoothing_level=0.5, optimized=True) test.index = pd.DatetimeIndex(test.index) exp_smoothing_forecast = exp_smoothing_result.forecast( test.shape[0]) exp_smoothing_forecast = exp_smoothing_forecast.reset_index( ).rename(columns={ 'index': 'Date', 0: 'Views' }).set_index('Date') fig, ax = plt.subplots(figsize=(15, 4)) test.rename(columns={ 'Views': 'Actual data' }).plot(ax=ax, color='blue') exp_smoothing_forecast.rename(columns={ 'Views': 'Forecast' }).plot(ax=ax, color='red') plt.xlabel('Date') plt.ylabel('Views') plt.legend(loc='best') new_exp_plot = "exp_plot_" + str(time.time()) + ".png" for filename in os.listdir('static/'): if filename.startswith('exp_plot_'): os.remove('static/' + filename) plt.savefig('static/' + new_exp_plot) return render_template('index.html', forecast='Exponential Smoothing', fcast='static/' + new_exp_plot) # Forecasting using saved Prophet model elif method == 'prophet': prophet_model = pickle.load(open('model/prophet_model.pkl', 'rb')) test.index = pd.DatetimeIndex(test.index) future = prophet_model.make_future_dataframe(periods=test.shape[0]) prophet_forecast = prophet_model.predict(future) forecast_prophet = prophet_forecast[[ 'ds', 'yhat_lower', 'yhat_upper', 'yhat' ]][-test.shape[0]:] forecast_prophet = forecast_prophet.set_index('ds') fig, ax = plt.subplots(figsize=(15, 4)) pd.plotting.register_matplotlib_converters() test.rename(columns={ 'Views': 'Actual data' }).plot(ax=ax, color='blue') forecast_prophet.rename(columns={'yhat': 'Forecast'})[[ 'Forecast' ]].plot(ax=ax, color='red') plt.fill_between(forecast_prophet.index, forecast_prophet['yhat_lower'], forecast_prophet['yhat_upper'], color='pink', alpha=0.5) plt.xlabel('Date') plt.ylabel('Views') plt.legend(loc='best') new_prophet_plot = "prophet_plot_" + str(time.time()) + ".png" for filename in os.listdir('static/'): if filename.startswith('prophet_plot_'): os.remove('static/' + filename) plt.savefig('static/' + new_prophet_plot) return render_template('index.html', forecast='Prophet', fcast='static/' + new_prophet_plot) # Forecasting using saved AutoARIMA model elif method == 'auto_arima': auto_arima_result = SARIMAXResults.load( 'model/auto_arima_model.pkl') auto_arima_forecast = auto_arima_result.predict( n_periods=test.shape[0]) auto_arima_forecast = pd.DataFrame(auto_arima_forecast, index=test.index, columns=['Forecast']) fig, ax = plt.subplots(figsize=(15, 4)) test.rename(columns={ 'Views': 'Actual value' }).plot(ax=ax, color='blue') auto_arima_forecast[['Forecast']].plot(ax=ax, label='Forecast', color='red') ax.set_xlabel('Date') ax.set_ylabel('Views') plt.legend(loc='best') new_auto_arima_plot = "auto_arima_plot_" + str( time.time()) + ".png" for filename in os.listdir('static/'): if filename.startswith('auto_arima_plot_'): os.remove('static/' + filename) plt.savefig('static/' + new_auto_arima_plot) return render_template('index.html', forecast='Auto-arima', fcast='static/' + new_auto_arima_plot) return render_template('index.html', fcast=fcast)
def test(csv_file, pkl_file, power_test, amb_test): power_path = str(power_test) # "power.csv" power = pd.read_csv(power_path, index_col=[0], parse_dates=True, names=["dt", "power"]) # Read Ambient Temperature Data temp_path = str(amb_test) # "amb_temp.csv" temp = pd.read_csv(temp_path, index_col=[0], parse_dates=True, names=["dt", "temp"]) df = pd.DataFrame() df = power.resample('15T').mean() df["temp"] = temp.resample('15T').mean() new_predicted = [] output = [] important = pd.read_csv("csv_file") H = important[0] L = important[1] coef = important[2] intercept = important[3] load_ = str(pkl_file) load_model = sresults.load(load_) start = df.index[0] end = df.index[-1] predicted = loaded.predict(start, end).values actual = power.power.values df["pred"] = predicted df["epsilon"] = (df["power"] - df["pred"]) / df["pred"] epsilon = df.epsilon.values power_val = df.power.values temp_val = df.temp.values for i in range(len(epsilon)): new_p = 0 if epsilon[i] > H: output.append("Positive Outlier") new_p = coef * temp_val[i] + intercept new_predicted.append(new_p) elif epsilon[i] < L: output.append("Negetive Outlier") new_p = coef * temp_val[i] + intercept new_predicted.append(new_p) else: outout.append("No Outlier") new_predicted.append(power_val[i]) new_df = pd.DataFrame(new_predicted) new_df.columns = ["Result"] new_df["Remark"] = output new_df.to_csv("Final_result.csv", index=False) return
import matplotlib.pyplot as plt from pandas import datetime import calendar import seaborn as sns from statsmodels.tsa.ar_model import AR from statsmodels.tsa.arima_model import ARMA from statsmodels.tsa.arima_model import ARIMA from statsmodels.tsa.statespace.sarimax import SARIMAX from statsmodels.tsa.statespace.sarimax import SARIMAXResults def parser(x): return datetime.strptime(x, '%d-%m-%Y') loaded = SARIMAXResults.load('model1.pkl') test = pd.read_csv('validation_data.csv', header=0, parse_dates=[1], index_col=1, date_parser=parser) series = pd.read_csv('trenddata.csv', header=0, parse_dates=[1], index_col=1, date_parser=parser) print(test.index) test = test[['Sales']] test = test[:22] print(test) train = series