def load_model(self): """ Compute the coefficients of the AutoReg models in model_dir_path Returns: df_coefs_all: Coefficients arrays of the AutoReg models as a DataFrame """ coefs_all = [] col_names = [] for fname in os.listdir(self.model_dir_path): if fname.endswith(".pkl"): trace_id, coord = os.path.splitext(fname)[0].split('-') if trace_id == os.path.splitext(self.train_trace)[0]: col_names.append(coord) file = open(os.path.join(self.model_dir_path, fname), "rb") model = AutoRegResults.load(file) coefs = model.params coefs_all.append(coefs) coefs_all = np.array(coefs_all).T df_coefs_all = pd.DataFrame(coefs_all, columns=col_names) coords = self.coords df_coefs_all = df_coefs_all.reindex(columns=coords) return df_coefs_all
def AutoRegPredict(): #load the saved model model = AutoRegResults.load('models/ar_model.pkl') data = np.load('models/ar_data.npy') last_observation = np.load('models/ar_observation.npy') #make the prediction predictions = model.predict(start=len(data), end=len(data)) #predict the next step out of the sample #transform the prediction yhat = predictions[0] + last_observation[0] #print(f'Prediction: {yhat}') updateDataSet(yhat) return yhat
def predict(): data=pd.read_csv('wind_data.csv') model = AutoRegResults.load('var_model.pkl') data['dt']=pd.to_datetime(data['dt']) data.set_index('dt', inplace=True) data['month'] = data.index.month data['hour'] = data.index.hour data = cycle_encode(data, ['month','hour']) data.drop(['month','hour'], axis=1, inplace=True) period = 24 data_diff = data.diff(period).dropna() final_pred = retrive_prediction(model, data_diff, data, 72) wind_speed=np.power(final_pred[:,3],3) pressure=final_pred[:,1] temp=final_pred[:,0]+273 R=2.90515100 air_density=np.multiply(temp,R).astype(float) air_density=np.divide(pressure,air_density).astype(float) A=3.14*58.5*58.5 constant=A*0.5*1.173*0.5 power_op=np.multiply(wind_speed,constant) power_data=pd.DataFrame(list(zip(power_op,final_pred[:,3])), columns=['Calc_power','Wind_speed']) power_dict=power_data.to_dict() power_dict = {'Points': power_dict} with open("db.json", "w") as wf: json.dump(power_dict,wf) return
X = difference(series.values) # fit model model = AutoReg(X, lags=6) model_fit = model.fit() # save model to file model_fit.save('petrol_model.pkl') # save the differenced dataset numpy.save('petrol_data.npy', X) # save the last ob numpy.save('petrol_obs.npy', [series.values[-1]]) # load AR model from file and make a one-step prediction # load model model = AutoRegResults.load('petrol_model.pkl') data = numpy.load('petrol_data.npy') last_ob = numpy.load('petrol_obs.npy') # make prediction predictions = model.predict(start=len(data), end=len(data)) # transform prediction yhat = predictions[0] + last_ob[0] print('Prediction for next week: %f' % yhat) # # update the data for the manual model with a new observation once available # import numpy # # get real observation # observation = 48 # # update and save differenced observation # lag = numpy.load('man_data.npy') # last_ob = numpy.load('man_obs.npy')
# load the AR model from file from statsmodels.tsa.ar_model import AutoRegResults import numpy loaded = AutoRegResults.load('ar_model.pkl') print(loaded.params) data = numpy.load('ar_data.npy') last_ob = numpy.load('ar_obs.npy') print(last_ob)
def pred_forecast(sales): # values, dates, tmp = tuple_to_list(sales) csv_file, csv_columns, values = sales_to_csv(sales) try: with open(csv_file, 'w') as csvfile: writer = csv.DictWriter(csvfile, fieldnames=csv_columns) writer.writeheader() for data in values: writer.writerow(data) except IOError: print("I/O error") series = read_csv(csv_file, header=0, index_col=0) # print(series.head()) # series.plot() # pyplot.show() x = difference(series.values) size = int(len(x) * 0.66) train, test = x[0:size], x[size:] # train autoregression # AR window = 7 model_ar = AutoReg(train, lags=window) model_fit_ar = model_ar.fit() # save model to file model_fit_ar.save("manast_site/static/predictions/ar_model.pkl") # save the differenced dataset numpy.save("manast_site/static/predictions/ar_data.npy", x) # save the last ob numpy.save("manast_site/static/predictions/ar_obs.npy", [series.values[-1]]) # save coefficients coef = model_fit_ar.params numpy.save("manast_site/static/predictions/ar_man_model.npy", coef) # save lag lag = x[-window:] numpy.save("manast_site/static/predictions/ar_man_data.npy", lag) # save the last ob numpy.save("manast_site/static/predictions/ar_man_obs.npy", [series.values[-1]]) # load model model = AutoRegResults.load("manast_site/static/predictions/ar_model.pkl") data = numpy.load("manast_site/static/predictions/ar_data.npy") last_ob = numpy.load("manast_site/static/predictions/ar_obs.npy") # make prediction predictions = model.predict(start=len(data), end=len(data)) # transform prediction yhat_ar = predictions[0] + last_ob[0] rmse_ar = sqrt(mean_squared_error(test, predictions[:len(predictions) - 1])) direction = "manast_site/static/predictions/predictionAR.png" direction_ar = "predictions/predictionAR.png" pyplot.close() pyplot.plot(test, color='blue', label=_("Results")) pyplot.plot(predictions, color='red', label=_("Predictions")) pyplot.legend() pyplot.savefig(direction) # MA model_ma = ARMA(train, order=(0, 0)) model_fit_ma = model_ma.fit(disp=False) # save model to file model_fit_ma.save("manast_site/static/predictions/ma_model.pkl") # save the differenced dataset numpy.save("manast_site/static/predictions/ma_data.npy", x) # save the last ob numpy.save("manast_site/static/predictions/ma_obs.npy", [series.values[-1]]) # save coefficients coef = model_fit_ma.params numpy.save("manast_site/static/predictions/ma_man_model.npy", coef) # save lag lag = x[-window:] numpy.save("manast_site/static/predictions/ma_man_data.npy", lag) # save the last ob numpy.save("manast_site/static/predictions/ma_man_obs.npy", [series.values[-1]]) # load model model = AutoRegResults.load("manast_site/static/predictions/ma_model.pkl") data = numpy.load("manast_site/static/predictions/ma_data.npy") last_ob = numpy.load("manast_site/static/predictions/ma_obs.npy") # make prediction predictions = model.predict(start=len(data), end=len(data)) # transform prediction yhat_ma = predictions[0] + last_ob[0] rmse_ma = sqrt(mean_squared_error(test, predictions[:len(predictions) - 1])) # ARMA model_arma = ARMA(train, order=(window, 0)) model_fit_arma = model_arma.fit(disp=False) # save model to file model_fit_arma.save("manast_site/static/predictions/arma_model.pkl") # save the differenced dataset numpy.save("manast_site/static/predictions/arma_data.npy", x) # save the last ob numpy.save("manast_site/static/predictions/arma_obs.npy", [series.values[-1]]) # save coefficients coef = model_fit_arma.params numpy.save("manast_site/static/predictions/arma_man_model.npy", coef) # save lag lag = x[-window:] numpy.save("manast_site/static/predictions/arma_man_data.npy", lag) # save the last ob numpy.save("manast_site/static/predictions/arma_man_obs.npy", [series.values[-1]]) # load model model = AutoRegResults.load( "manast_site/static/predictions/arma_model.pkl") data = numpy.load("manast_site/static/predictions/arma_data.npy") last_ob = numpy.load("manast_site/static/predictions/arma_obs.npy") # make prediction predictions = model.predict(start=len(data), end=len(data)) # transform prediction yhat_arma = predictions[0] + last_ob[0] rmse_arma = sqrt( mean_squared_error(test, predictions[:len(predictions) - 1])) prev_week = [] error_prev_week = 0 actual_week = [] for v in range(6, len(series.values)): prev_week.append(float(series.values[v - 7])) actual_week.append(float(series.values[v])) error = float(series.values[v]) - float(series.values[v - 7]) error_prev_week += abs(error) epd_week = error_prev_week / (len(series.values) - 7) # print(epd_week) return direction_ar, yhat_ar, rmse_ar, yhat_ma, rmse_ma, yhat_arma, rmse_arma, prev_week, actual_week, error_prev_week, epd_week